Docker Fundamentals on Amazon EC2: A Practical Introduction
Source: Dev.to
Let’s 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 an Amazon Linux AMI and connect via Instance Connect.
- Run 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
Verify Docker Installation
docker --version
Exit and reconnect to the EC2 instance, then run:
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 a Redis container:
docker run redis
If the Redis image is not present locally, Docker pulls it from Docker Hub automatically. Subsequent runs use the cached image.
Counting Containers
-
List running containers:
docker ps -
List all containers (including stopped ones):
docker ps -a
Stopping a Container
docker stop <container-id-or-name>
Removing a Container
Note: Containers must be stopped before they can be removed.
docker rm <container-id-or-name>
Counting Images
docker images
Pulling an Image
docker pull <image-name>:<tag>
If no tag is specified, Docker pulls the latest tag by default.
Deleting an Image
First ensure no containers are using the image (stop and remove them). Then run:
docker rmi <image-id-or-name>
Running a Container in Detached Mode
Add the -d flag to run in the background:
docker run -d <image-name>
docker run -d --name=webapp1 nginx:1.14-alpine
Hosting a Local Registry
docker run -d --name my-registry -p 80:5000 --restart always registry:2
Pulling the Nginx Image
docker pull nginx:latest
Tagging an Image with a New Name
docker image tag nginx:latest localhost:80/nginx:latest
Pushing the Image
docker push localhost:80/nginx:latest
Verifying the Image Was Pushed
curl -X GET localhost/v2/_catalog

Port Mapping for Web Applications
Expose the application externally by mapping a host port to the container’s port with the -p option:
docker run -d --name=flask -p 80:80 nandinivijayr/myflaskapplication
The above command maps container port 80 to host port 80 and runs the container in detached mode.
Inspecting Containers and Images
Retrieve detailed JSON configuration for a container:
docker inspect <container-id-or-name>
Retrieve detailed JSON configuration for an image:
docker inspect <image-id-or-name>
Retrieving Container Logs
docker logs <container-id-or-name>
Question Time
Run a MySQL container and give it a name of your choice.