DEV Community

Cover image for Docker Fundamentals on Amazon EC2: A Practical Introduction

Docker Fundamentals on Amazon EC2: A Practical Introduction

Lets Start with a simple story
Imagine this.

A developer builds an application on their laptop. It runs perfectly. Feeling confident, they send it to the testing team.

But then comes the message:

“The app is not working on our server.”

The developer checks everything and replies:

“But it works on my machine!”

If this sounds familiar, you’ve just met one of the biggest problems in software development.

What Actually Went Wrong?

The application depended on:

A specific version of a programming language

Certain libraries and tools

Configuration files set up on the developer’s laptop

The server had a slightly different setup—and that was enough to break the app.
🐳 Enter Docker

Docker steps in like a smart packaging system.

Instead of sending just the application code, Docker packs the app together with everything it needs to run—the runtime, libraries, and configurations—into a single unit called a container.

Now when the app moves:

From laptop → test server

From test server → production

From local system → cloud

…it behaves exactly the same everywhere.

Think of Docker Like This

Imagine you’re sending a homemade dish to a friend.
Instead of sending just the recipe and hoping they have the right ingredients and stove, you send the entire ready-to-eat meal in a sealed box.

That’s Docker


Install Docker on ec2

  • Launch an Ec2 Instance with Amazon AMI and do an instance connect to get connected to terminal
  • Execute the following commands
sudo yum update -y
sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user  # Allow running Docker without sudo

Enter fullscreen mode Exit fullscreen mode

Verify Docker Installation

docker --version
Exit and reconnect to ec2 Instance connect and Run the below command
docker ps # Should return an empty list

Running a Container

The Docker run command creates and starts a container from a specified image. For example, to start an redis container, simply execute:

docker run redis

If the redis image already exists on your host, Docker immediately starts a new container. However, if the image is not present, Docker will pull it from Docker Hub. Once the image is downloaded, any subsequent run command will use the cached image.

To count the total number of containers running on the host

Run the command
docker ps
You can view running containers using the docker ps command. This command provides an overview, including container IDs, image names, statuses, and container names.

Run the command
docker ps -a to list all containers, including those that have stopped or exited

To stop the container

Run the command

docker stop <container_name>

or

docker stop <container_id> (first few letters of container id that can uniquely identify the container would be good)

To remove the container

docker rm <container_id>(first few letters of container id that can uniquely identify the container would be good)
Note: Containers should be stopped before removing them

To count the number of images available on the host

Run the command
docker images

To pull an image

docker pull <image_name>:<tag_name>

Note: if tag not mentioned, image with latest tag will be pulled

To delete an image

To delete an image no longer needed, ensure no container is using it.
Stop and remove all the containers created from that image and then
Run the command.
docker rmi <image_name>
or
docker rmi <image_id>

To run the container in detached mode

To run the web application in detached (background) mode, add the -d option

docker run -d <image_name>
docker run -d --name=webapp1 nginx:1.14-alpine

To host a local registry

`docker run -d --name my-registry -p 80:5000 --restart always registry:2'

To pull ngnix image

docker pull nginx:latest

To tag the image with a new name

docker image tag nginx:latest localhost:80/nginx:latest

To push the image

docker push localhost:80/nginx:latest

To verify image pushed

curl -X GET localhost/v2/_catalog

Port mapping the containers for Webapplications

To expose the application externally, you must map a free host port to the container’s port using the -p parameter.

docker run -d --name=flask -p 80:80 nandinivijayr/myflaskapplication
The above command will map the container port 80 to host port 80 and -d indicates that we are running the container in a detached mode

Inspecting the containers and images

For detailed information about a particular container or an Image the docker inspect command provides comprehensive configuration details in JSON format.
docker inspect container_id/ container_name will give details about the container
docker inspect image_id / image_name will give details about the image

To retrive container logs

docker logs <container_name/ container_id >

Question time?

Run a container with mysql image and name it


Top comments (0)