How to install Docker on Amazon Linux 2

⏱ Reading Time: 2 minutes

How do I install docker and docker-compose on Amazon Linux 2 running on the EC2 or Lightsail cloud instance?

Before you read this article, consider using AWS Fargate.

1. The first step is to access the operating system with SSH. This step depends on how your machine is configured. I’ll show two options to get access:

SSH with a private key

ssh -i /path/to/the/key.pem ec2-user@ec2-ip-address-dns-name-here

OR

ssh ec2-user@ec2-ip-address-dns-name-here

2. Apply pending updates using the yum command:

sudo yum update

3. Search for the Docker package:

sudo yum search docker

4. Install docker with the command below:

sudo yum install docker

5. Add group membership for the default “ec2-user” so you can run all docker commands.

sudo usermod -a -G docker ec2-user
id ec2-user

6. Enable docker service at boot time:

sudo systemctl enable docker.service

7. Start the Docker service:

sudo systemctl start docker.service

8. Check the docker service status, run:

sudo systemctl status docker.service

The expected return would be something like this

 docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-09-08 05:03:52 EDT; 18s ago
     Docs: https://docs.docker.com
  Process: 3295 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
  Process: 3289 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
 Main PID: 3312 (dockerd)
    Tasks: 9
   Memory: 39.9M
   CGroup: /system.slice/docker.service
           └─3312 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/c...

Sep 08 05:03:51 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:51 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:51 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:51 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:52 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:52 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:52 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:52 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Sep 08 05:03:52 amazon.example.local systemd[1]: Started Docker Applicatio...
Sep 08 05:03:52 amazon.example.local dockerd[3312]: time="2021-09-08T05:03...
Hint: Some lines were ellipsized, use -l to show in full.

9. Check the Docker version:

docker version

The return would be something like this:

Client:
 Version:           20.10.13
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 31 19:20:32 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.13
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       906f57f
  Built:            Thu Mar 31 19:21:13 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.13
  GitCommit:        9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
 runc:
  Version:          1.0.3
  GitCommit:        f46b6ba2c9314cfc8caae24a32ec5fe9ef1059fe
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Need docker-compose, too?

You have two alternatives to install. See below:

Install using  pip (recommended)

sudo pip3 install docker-compose

OR

Manually install

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

To verify that the installation was successful, can you use the command:

docker-compose version

Should you see something like:

docker-compose version 1.29.2, build unknown
docker-py version: 5.0.3
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017

Bonus: install ctop

ctop provides a concise and condensed overview of real-time metrics for multiple containers.

ctop

$ sudo wget https://github.com/bcicen/ctop/releases/download/v0.7.7/ctop-0.7.7-linux-amd64 -O /usr/local/bin/ctop
$ sudo chmod +x /usr/local/bin/ctop

@michels gave that tip; thanks!