Installing Docker on an AWS EC2 instance

Sandun Dayananda
2 min readMar 13, 2022

--

Docker engine on AWS EC2 architecture
Docker engine on AWS EC2 architecture

“Let’s see how to install Docker on Amazon Linux 2 AMI”

First we have to create an AWS EC2 instance and log in to it using a tool like Putty. Here I’m using t2.micro type instance which is free tier eligible.

Since the Amazon Linux 2 AMI is a Fedora distro the default package manager is yum

Get root permission

[ec2-user@ip-172-31-16-237 ~]$ sudo -i
[root@ip-172-31-16-237 ~]#

Check for yum package manager

[root@ip-172–31–16–237 ~]# which yum
/bin/yum

Install Docker

yum-config-manager — enable rhui-REGION-rhel-server-extras
yum -y install docker
systemctl start docker
docker version

Verify that Docker Engine is installed correctly by running the command docker run hello-world

This command will download a test image and run it in a container. Here dockeris the docker client. When the container runs, it prints

[root@ip-172–31–16–237 ~]# docker run hello-worldHello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

If we want to run docker commands without root permission we have to add the current user to the list of users who are allowed to run Docker without putting sudo at the beginning

create a group for running Docker commands

[root@ip-172–31–16–237 ~]# groupadd docker
groupadd: group ‘docker’ already exists

It’s OK if it already exists.

Adding current user to that group. $USER here will be resolved by the shell to the user name we’re logged in as right now.

[root@ip-172–31–16–237 ~]# usermod -aG docker $USER
[root@ip-172–31–16–237 ~]#

This allows non-root users to run Docker as well.

Now logout from the shell and log in and try the docker run hello-world

If it prints the previous message, then everything is fine.

--

--

Sandun Dayananda
Sandun Dayananda

Written by Sandun Dayananda

Big Data Engineer with passion for Machine Learning and DevOps | MSc Industrial Analytics at Uppsala University

No responses yet