Ravindra BagaleCourses & study guides Track your progress

Guides

How to Install Docker on Amazon Linux 2023 (EC2)

To install Docker on Amazon Linux 2023, connect to the EC2 instance and run sudo yum install docker -y, then sudo service docker start and sudo systemctl enable docker. Add your user to the docker group with sudo usermod -aG docker ec2-user, log out and back in, and test with docker run hello-world. To serve a web page from a container, run docker run -d -p 80:80 nginx and allow HTTP port 80 in the security group.

Come on, friends! Today we install Docker on EC2. Docker packs your app and everything it needs into one box – a container. If it runs on your laptop, it runs the same way on the server. So the very first job is to install Docker, then verify that it has started, and finally run our first container. Don't worry, it is really simple.

Quick answer

On an Amazon Linux 2023 EC2 instance, run these commands, then log out and SSH in again so the group change applies:

sudo yum install docker -y
sudo service docker start
sudo systemctl enable docker
sudo usermod -aG docker ec2-user      # then exit and SSH in again
docker run hello-world                # expect "Hello from Docker!"

What do I need before installing Docker?

What happens when I run a container on EC2?

Docker image becomes a running container on EC2 The docker run command asks the Docker daemon to pull the nginx image from Docker Hub and start a container named web. A browser request on port 80 passes the security group, Docker forwards it to the container, and the 200 OK response goes back. Image is the read-only package; container is the running copy. Docker Hub image registry Browser SG :80 EC2 · Docker daemon $ docker run -d -p 80:80 --name web nginx Imagenginx Container"web" :80 Image = read-only package · Container = running copy run pull image GET / 200 OK

docker run pulls an image (read-only package) from Docker Hub and starts a container (running copy) on EC2. A request on port 80 passes the security group to the container.

An image is a read-only package (for example the official nginx image). A container is a running copy of that image. Docker pulls images from Docker Hub the first time you use them and keeps them on the instance.

How do I install Docker on Amazon Linux 2023?

Step 1 — Update the packages and install Docker

Amazon Linux 2023 has its own docker package in the default repository, so no extra repository is needed:

sudo yum update -y
sudo yum install docker -y
docker --version

Step 2 — Start Docker and enable it at boot

Docker does not start by itself after install. Start it now and enable it for every reboot, then check that it is running:

sudo service docker start
sudo systemctl enable docker
sudo service docker status          # look for "active (running)"

sudo service works on Amazon Linux 2023 and hands over to systemctl, the modern equivalent (sudo systemctl start docker does the same).

Step 3 — Use Docker without sudo

The Docker daemon listens on a socket that only root and members of the docker group can use. Add ec2-user to that group:

sudo usermod -aG docker ec2-user
exit                                # log out, then SSH in again
groups                              # the list must now contain "docker"

Membership in the docker group is as powerful as sudo, so add only trusted users.

Ravindra Bagale's Tip

Many students run usermod and immediately run docker ps – and get "permission denied". A group change applies only to a new login. Run exit, SSH in again, and check that docker appears in groups. Pay attention: the docker group is almost root power – give it only to users you trust.

Step 4 — Run your first container

docker run hello-world              # downloads a tiny test image and prints "Hello from Docker!"
docker ps -a                        # lists all containers, including stopped ones
docker images                       # lists the images on this instance

Step 5 — Run a web server in a container

Run the official Nginx image in the background (-d), map port 80 of the instance to port 80 of the container (-p host:container), and give it a name:

docker run -d -p 80:80 --name web --restart unless-stopped nginx
docker ps                           # STATUS "Up", PORTS 0.0.0.0:80->80/tcp
curl -I http://localhost            # expect HTTP/1.1 200 OK

Allow HTTP (TCP 80) in the instance's security group (How to Open Port 80 and 443 in an EC2 Security Group) and open http://<PUBLIC_IP> — note http, not https. You see the Nginx welcome page, served from inside the container. 🐳

If Nginx or Apache is already installed on the instance itself, port 80 is taken. Stop it (sudo service nginx stop) or map another host port, for example -p 8080:80, and allow 8080 from My IP only.

Step 6 — Everyday Docker commands

docker logs web                     # output of the container
docker exec -it web bash            # open a shell inside the container (type exit to leave)
docker stop web && docker start web # stop and start again
docker rm -f web                    # remove the container
docker image prune                  # delete unused images to free disk space

Step 7 — Optional: install the Docker Compose plugin

The Compose plugin is not in the Amazon Linux 2023 repository. Install it from the official Docker Compose releases on GitHub:

sudo mkdir -p /usr/local/lib/docker/cli-plugins
sudo curl -SL "https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m)" \
  -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
docker compose version

Ravindra Bagale's Tip

The container is running but the page does not open in the browser? First run curl -I http://localhost on the server itself. If you get 200 here, Docker is fine – the problem is in the security group. And if you forget -p 80:80, the container runs inside but nobody outside can see it. Got it? The security group is a wall of fire – a request gets in only if the gate (port) is open.

How do I fix common Docker errors on EC2?

Ghabru naka 😅 — these are the ones everybody meets:

Symptom Likely cause Fix
permission denied while trying to connect to the Docker daemon socket User not in the docker group, or no new login yet sudo usermod -​aG docker ec2-​user, exit, SSH in again, groups
Cannot connect to the Docker daemon ... Is the docker daemon running? Docker service stopped sudo service docker start; sudo systemctl enable docker
bind: address already in use on port 80 Nginx/Apache on the host already uses port 80 sudo ss -​tlnp | grep :​80; stop it, or use -p 8080:80
Browser timeout, curl localhost works Port missing in the security group; typed https:// Add inbound HTTP 80 (or your port); use http://
docker compose is not a docker command Compose plugin not installed Step 7
no space left on device Old images and stopped containers docker system df; docker image prune; grow the EBS volume

Learn it properly

This guide is the short path. These free lessons explain the Linux and networking basics behind it:

Samajla ka? Got it? Install, start, enable, the docker group, and log in again – that's all. Now you can run any app in a minute with docker run. You will get it, slowly you will get it – run one new image every day.

Frequently asked questions

How do I install Docker on Amazon Linux 2023?

Run sudo yum install docker -y, then sudo service docker start and sudo systemctl enable docker. Add your user to the docker group with sudo usermod -aG docker ec2-user and log in again.

Why do I get "permission denied while trying to connect to the Docker daemon socket"?

Your user is not in the docker group yet, or you did not log in again after adding it. Run sudo usermod -aG docker ec2-user, exit the SSH session, connect again and check with groups.

Is Docker the same as docker-ce on Amazon Linux 2023?

Amazon Linux 2023 ships its own docker package in the default repository, so you do not need the docker-ce repository. The commands (docker run, docker ps, docker images) are the same.

How do I install Docker Compose on Amazon Linux 2023?

The Compose plugin is not in the Amazon Linux 2023 repository. Download the docker-compose binary from the official Docker Compose GitHub releases into /usr/local/lib/docker/cli-plugins/, make it executable and check with docker compose version.

Why does my container page not open in the browser?

Usually the security group does not allow the host port, the -p host:container mapping is missing or wrong, or another program (Nginx or Apache on the host) already uses that port. Check with docker ps and sudo ss -tlnp.

Does Docker start again after a reboot?

Only if you enabled it: sudo systemctl enable docker. Containers restart too if you start them with --restart unless-stopped.