Introduction
Containerization has revolutionized modern software development by enabling developers to package applications with all their dependencies. Docker simplifies application deployment, making software consistent across development, testing, and production environments.
In this step-by-step tutorial, we will demonstrate how to install Docker on an AWS EC2 instance and run an Nginx web server inside a Docker container.
What is Docker and Why Use It on AWS EC2?
Docker is an open-source containerization platform that allows you to build, test, and deploy applications quickly.
Running Docker on AWS EC2 provides several key benefits:
- Portability: Run the exact same container environment locally and in the cloud.
- Resource Efficiency: Containers share the host OS kernel, using far fewer resources than full virtual machines.
- Fast Deployment: Launch or replace microservices in seconds.
Prerequisites
Before starting this tutorial, ensure you have:
- An active AWS Account.
- A running Amazon Linux 2023 or Ubuntu EC2 instance.
- An inbound security group rule allowing HTTP (Port 80) and SSH (Port 22) traffic.
- SSH access to your EC2 instance via terminal or AWS Systems Manager.
Step-by-Step Implementation Guide
Step 1: Connect to Your EC2 Instance via SSH
First, open your terminal and establish an SSH connection to your running EC2 instance:
# For Amazon Linux instances
ssh -i "your-key-pair.pem" ec2-user@your-ec2-public-ip
# For Ubuntu instances
ssh -i "your-key-pair.pem" ubuntu@your-ec2-public-ip
Step 2: Install Docker on Host System
Next, update your system packages and install Docker depending on your Linux distribution:
- For Amazon Linux
sudo dnf update -y
sudo dnf install -y docker
- For Ubuntu Linux:
sudo apt-get update -y
sudo apt-get install -y docker.io
Step 3: Start and Enable the Docker Engine
Now, initialize the Docker background service and ensure it automatically starts on system reboots:
# Start the Docker daemon service
sudo systemctl start docker
# Enable Docker to launch on system boot
sudo systemctl enable docker
# Verify the operational status
sudo systemctl status docker
Step 4: Configure Non-Root Docker Permissions (Optional)
By default, executing Docker commands requires sudo administrative rights. To run Docker commands as a non-root user:
# Add Amazon Linux user to the Docker group
sudo usermod -aG docker ec2-user
# Or add Ubuntu user to the Docker group
sudo usermod -aG docker ubuntu
Note: Log out of your SSH session and log back in for group membership changes to take effect.
Step 5: Pull and Run the Nginx Web Server Container
Now, pull the official Nginx web server image from Docker Hub and start the container:
# Pull the official Nginx container image
docker pull nginx
# Launch the Nginx container with port mapping
docker run -d -p 80:80 --name cloudwithyuvi nginx
Command Explanation:
-d: Runs the container in detached mode (background process).-p 80:80: Maps host machine port80to container port80.--name cloudwithyuvi: Assigns a custom name to the running container instance.
Step 6: Verify the Running Container and Test Public
Finally, confirm that your container is active and accessible:
- Verify running containers via CLI:
docker ps
- Open your web browser and navigate to your EC2 instance’s public IP address:
http://YOUR-EC2-PUBLIC-IP
You should see the default “Welcome to nginx!” landing page, indicating successful container execution.
Essential Commands for Managing Docker Containers
- Use these common CLI commands to manage the container lifecycle:
# View all currently active running containers
docker ps
# View all containers including stopped ones
docker ps -a
# Stop a running container
docker stop cloudwithyuvi
# Start a stopped container
docker start cloudwithyuvi
# Remove a container permanently
docker rm cloudwithyuvi
# Inspect live container operational logs
docker logs cloudwithyuvi
Troubleshooting Common Docker Setup Issues
Diagnose and fix frequent errors encountered during EC2 container deployments:
Error 1: Web Page Times Out When Opening Public IP
- The Cause: The EC2 Security Group attached to your instance is blocking inbound traffic on Port 80.
- The Fix: Open the AWS EC2 Console -> Select Security Groups -> Edit Inbound Rules -> Add Rule: Type:
HTTP| Port:80| Source:0.0.0.0/0.
Error 2: Permission Denied Error When Running Docker Commands
- The Cause: The active SSH user account lacks permission to communicate with the Docker daemon socket (
/var/run/docker.sock). - The Fix: Prefix commands with
sudoor add your SSH user to thedockersecurity group and restart your SSH session.




