LogIn
I don't have account.

How to Install and Run Docker on Amazon EC2 by yum

DevSniper

216 Views

Amazon EC2 (Elastic Compute Cloud) is a web service that provides resizable compute capacity in the cloud. Running Docker on Amazon EC2 allows you to fast container deployment and management in a flexible environment.

In this guide, we will walk through, how to install Docker on an EC2 instance using the YUM package manager, step-by-step. We'll start from launching your EC2 instance to running your first Docker container — with real-world insights and best practices to help you in production-ready setups.

What is Docker?

Docker is an open-source containerization platform that allows developers to package applications along with all their dependencies ~ libraries, configuration files and binaries into a single, portable unit called a container. Unlike virtual machines, containers are lightweight and start almost instantly.

Why Install Docker on EC2?

Amazon EC2 (Elastic Compute Cloud) provides scalable virtual servers in the cloud. Installing Docker on an EC2 instance combines the flexibility of virtual infrastructure with the power of containerization. Here’s why this setup is beneficial

  • Run Containerized Applications on Dedicated Infrastructure
  • Avoid vendor lock-in (vs ECS or Fargate)
  • Build custom CI/CD pipelines
  • Self-manage container environments

Real-World Analogy : Think of EC2 as your rented server and Docker as a smart packaging system that runs any app in a neat little box.

Docker Installation Using YUM

1: Launch an EC2 Instance

To run Docker on the cloud, you first need a virtual server. Here’s how to set up an EC2 instance on AWS

  1. Go to AWS Console > EC2 > Launch Instance
  2. Choose Amazon Linux 2 AMI
  3. Select instance type (e.g., t2.micro for free tier)
  4. Configure security group:
    • Allow SSH (port 22)
    • Optional: Allow HTTP (port 80), HTTPS (443) for web containers
  5. Choose or create a key pair (PEM file) to connect later via SSH
  6. Review your settings, then click “Launch”

2: SSH Into Your EC2 Instance

Once your EC2 instance is up and running, you’ll need to connect to it using SSH (Secure Shell).

SSH Command (From Your Terminal or Git Bash)
Copy
ssh -i your-key.pem ec2-user@<your-ec2-public-ip>
  • Replace your-key.pem with your actual key filename
  • Replace <your-ec2-public-ip> with the public IP address of your EC2 instance (e.g., ec2-user@18.212.45.67)
⚠️ Common Issues &amp; Fixes
Issue Solution
Permission denied (publickey) Ensure your .pem file has chmod 400 permissions
Host not reachable Make sure the EC2 instance is running and your IP is allowed in the Security Group (SSH port 22)
Wrong username For Amazon Linux 2, use ec2-user. For Ubuntu AMI, use ubuntu instead

✅ If you see a welcome message or prompt like ec2-user@ip-...$, you’re successfully connected!

3: Update System Packages

Before installing any software, it's best to update the existing packages on your EC2 instance. This ensures you get the latest security patches and dependencies. So, let's first update our machine

sudo yum update -y

Tip : Always run this command before installing Docker or any major software to prevent version conflicts or missing dependencies.

✅ Once the update is complete, your system is ready for Docker installation.

4: Install the Latest Docker on Amazon Linux 2

Now, you can install Docker using yum. Amazon Linux 2 provides Docker directly in its default repositories. To install Docker, use the following command

Copy
sudo yum install docker
it will ask you for installation. enter `y`

This will install Docker and all necessary dependencies on your EC2 instance.

✅ Once completed, Docker will be installed but not yet running. You’ll start and enable it in the next step.

Tip : If you ever need to verify the installation, you can run "docker --version" after Docker is started.

5. Start Docker

After installing Docker, you need to start the Docker service and enable it to run automatically on system boot. This ensures Docker is always available — even after restarts.

To start the Docker service you can use any one of the following service. Both commands start the Docker daemon (dockerd), which is responsible for running your containers.

Copy
sudo service docker start
         OR
sudo systemctl start docker

To start Docker automatically on boot

Copy
sudo systemctl enable docker

This ensures Docker starts on system reboot.

Tip : If you get a permission error running Docker commands later, consider adding your user to the docker group.

6. Check Docker running status

Once Docker has been started, it's good practice to verify that the service is running properly. To check the current status of the Docker service run below command

Copy
sudo service docker status
           OR
sudo systemctl status docker

Commonly Used Docker Commands on EC2

Here’s a handy list of essential Docker commands, those are frequently used when managing containers on EC2 instance

  • docker --version : Shows the currently installed Docker version. Useful to verify installation.
  • sudo usermod -aG docker ec2-user : Adds the ec2-user to the Docker group, allowing Docker commands without needing sudo.
  • docker run <container-name> : This command is used to start a Docker container.
  • docker run -d -p 80:80 nginx
    • -d: Run container in detached (background) mode
    • -p 80:80: Map port 80 of the host to port 80 of the container
    • nginx: The container image to run (official NGINX web server)

    Access it in your browser at http://<your-ec2-public-ip>

  • docker stop <container_id> : Gracefully stops a running container
  • docker rm <container_id> : Deletes a stopped container to free up system resources.
  • docker ps : Displays all currently running containers.
  • docker ps -a : Shows all containers you've ever created, including stopped ones.
  • docker system prune -a : Removes all unused containers, images, networks and volumes.

    ⚠️ Use with caution — it will delete everything not currently in use.

  • docker pull <image> : Download an image from Docker Hub
  • docker images : List all local images.
  • docker rmi <image-id|name> : Remove an image
  • docker exec -it <container-id> /bin/bash : Run a command inside a running container
  • docker kill <container-id> : Force stop a container
  • docker restart <container-id> : Restart a container
  • docker rename <old> <new> : Used to Rename a container
  • docker logs <container-id> : View logs from a container
  • docker top <container-id> : Show running processes inside a container
  • docker history <image-name> : Show image layer history
  • docker logs --tail 100 <container-id> : Show last 100 lines of logs