We often use Docker in DevOps workflows, as it's the most widely adopted containerization technology today. In many cases, we also leverage cloud computing platforms like AWS to run these workloads. In this post, we'll walk through how to install Docker on Amazon Linux 2 using an EC2 instance.
Connect to EC2
Launch an Amazon EC2 instance with Amazon Linux 2 as the OS. Once it's running, connect via SSH:
Replace your.pem
with your private key file and <ec2-public-ip>
with your instance's public IP.
$ ssh -i "your.pem" ec2-user@<ec2-public-ip>.compute-1.amazonaws.com
You'll see something like this:
, #_
~\_ ######_ Amazon Linux 2
~~ \_#######\
~~ \####| AL2 End of Life is 2025-06-30.
~~ \#/ ___
~~ V~' '->
~~~ / A newer version of Amazon Linux is available!
~~._. _/
_/ _/ Amazon Linux 2023, GA and supported until 2028-03-15.
_/m/' https://aws.amazon.com/linux/amazon-linux-2023/
Check Yum and Search Docker
We'll use yum
to install Docker. First, update the package list:
$ sudo yum update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No packages marked for update
Search for Docker packages:
$ sudo yum search docker
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
===================================================== N/S matched: docker =====================================================
pcp-pmda-docker.x86_64 : Performance Co-Pilot (PCP) metrics from the Docker daemon
...
...
Check Docker details:
$ sudo yum info docker
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Available Packages
Name : docker
...
...
Install Docker
After checking the Docker information, we can install Docker using yum
.
$ sudo yum install docker
...
Installed:
docker.x86_64 0:20.10.25-1.amzn2.0.4
Dependency Installed:
containerd.x86_64 0:1.7.11-1.amzn2.0.1 libcgroup.x86_64 0:0.41-21.amzn2 pigz.x86_64 0:2.3.4-1.amzn2.0.1
runc.x86_64 0:1.1.11-1.amzn2
Complete!
Once installed, verify the version:
$ sudo docker version
Client:
Version: 20.10.25
...
Server:
Engine:
Version: 20.10.25
...
Start and Enable Docker
Start the Docker service:
$ sudo service docker start
Redirecting to /bin/systemctl start docker.service
Enable Docker to start on boot:
$ sudo systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
Check the service status:
$ sudo systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2024-04-10 02:29:53 UTC; 1min 13s ago
Docs: https://docs.docker.com
Main PID: 3529 (dockerd)
CGroup: /system.slice/docker.service
└─3529 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=32768:65536...
...
Add ec2-user to Docker Group
To avoid using sudo
every time, add your user to the Docker group:
$ sudo usermod -a -G docker ec2-user
Check the group:
$ id ec2-user
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal),992(docker)
Apply group change:
$ newgrp docker
Install docker-compose
We can install docker-compose
to help us managing the Container of Docker through pip3
.
# 1. Get pip3
$ sudo yum install python3-pip
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core | 3.6 kB 00:00:00
Package python3-pip-20.2.2-1.amzn2.0.5.noarch already installed and latest version
Nothing to do
# 2. Then run any one of the following
$ pip3 install --user docker-compose
Collecting docker-compose
Downloading docker_compose-1.29.2-py2.py3-none-any.whl (114 kB)
...
...
Successfully installed ... docker-compose-1.29.2 ...
If you encounter a urllib3
version issue, fix it by running:
pip3 install urllib3==1.26.15
See: Persisting spaCy import error: NotOpenSSLWarning: urllib3 v2.0 only supports OpenSSL 1.1.1+ #12750.
Alternative: Install via wget
:
$ wget https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)
$ sudo mv docker-compose-$(uname -s)-$(uname -m) /usr/local/bin/docker-compose
$ sudo chmod -v +x /usr/local/bin/docker-compose
Check the version:
$ docker-compose version
docker-compose version 1.29.2, build unknown
docker-py version: <module 'docker.version' from '/home/ec2-user/.local/lib/python3.7/site-packages/docker/version.py'>
CPython version: 3.7.16
OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
Then now we could start our workflow with Docker in AWS EC2 Amazon Linux 2 Docker.
Outro
In the last of article, I embed some useful commands that I usually use when I work with docker, and hope you enjoy with Docker in EC2.
# start the service
$ sudo systemctl start docker.service
# stop the service
$ sudo systemctl stop docker.service
# restart the service
$ sudo systemctl restart docker.service
# get the service status
$ sudo systemctl status docker.service