Docker is a software tool created to enable users create, deploy and manage applications using containers. The concept of containers has witnessed tremendous adoption in the past couple of years. As a developer, containers allows you to package up applications with all of its dependencies which include libraries and deploy it as one package. This guide will show you how to install and use Docker CE on Amazon Linux 2.

Applications packaged as containers can run on any other Linux machine regardless of the environment, distribution type or customizations which distinguish source machine and the destination server where containers are run. We will update the OS, install some few dependencies then install Docker CE on Amazon Linux 2 server.

Install Docker CE on Amazon Linux 2

Before we start the update, let’s ensure our system is updated.

sudo yum -y update

Once the update has been done, reboot your system.

sudo systemctl reboot

Wait for reboot to be completed then login back and continue with the installation of Docker CE on Amazon Linux 2.

$ ssh [email protected]

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

Install Docker Dependencies on Amazon Linux 2

Install the basic dependencies required for running Docker on Amazon Linux 2.

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

If running on actual AWS Cloud and not Developer machine install other tools that ensure integration with other AWS services is seemless.

sudo yum -y install curl wget unzip awscli aws-cfn-bootstrap nfs-utils chrony conntrack jq ec2-instance-connect socat

If the ec2-net-utils package is installed remove it as it interferes with the route setup on the instance.

if yum list installed | grep ec2-net-utils; then sudo yum remove ec2-net-utils -y -q; fi

Set correct system timezone and Clock.

sudo yum -y install chrony
sudo systemctl enable --now chronyd
sudo timedatectl set-timezone Africa/Nairobi

Sync NTP time.

$ sudo chronyc sources
210 Number of sources = 8
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 169.254.169.123               0   4     0     -     +0ns[   +0ns] +/-    0ns
^+ time.cloudflare.com           3   6   277    39   -239us[ -637us] +/-  184ms
^+ time.cloudflare.com           3   6   377    39  -1526us[-1923us] +/-  185ms
^* ntp2.icolo.io                 2   6   377    37  -1522us[-1920us] +/-   83ms
^? time.cloudflare.com           0   6     0     -     +0ns[   +0ns] +/-    0ns
^? time.cloudflare.com           0   6     0     -     +0ns[   +0ns] +/-    0ns
^? ntp1.icolo.io                 0   6     0     -     +0ns[   +0ns] +/-    0ns
^? ntp0.icolo.io                 0   6     0     -     +0ns[   +0ns] +/-    0ns

Install Docker CE on Amazon Linux 2

With the dependencies installed we can now install Docker CE on Amazon Linux 2. Ensure the amazon-linux-extras repository is enabled on your system.

sudo amazon-linux-extras enable docker

Install Docker on Amazon Linux.

sudo yum -y install docker

The command above will install the latest stable version of Docker on Amazon Linux 2. If you want a specific version provide the version number. See example below.

DOCKER_VERSION="19.03.6ce-4.amzn2"
sudo yum install -y docker-${DOCKER_VERSION}*

Make your user part of the docker group.

sudo usermod -aG docker $USER
newgrp docker

Create Docker json configuration file.

sudo tee /etc/docker/daemon.json<<EOF
{
  "bridge": "none",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "10"
  },
  "live-restore": true,
  "max-concurrent-downloads": 10
}
EOF

Start and enable docker service.

sudo systemctl daemon-reload
sudo systemctl enable --now docker

Confirm Docker service status shows as running.

$ systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-07-09 11:24:27 EAT; 21s ago
     Docs: https://docs.docker.com
  Process: 5292 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
  Process: 5279 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
 Main PID: 5293 (dockerd)
    Tasks: 8
   Memory: 36.5M
   CGroup: /system.slice/docker.service
           └─5293 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=1024:4096

Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.360887475+03:00" level=info msg="parsed scheme: \"unix\"" module=grpc
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.360904110+03:00" level=info msg="scheme \"unix\" not registered, fallback to def...dule=grpc
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.360916957+03:00" level=info msg="ccResolverWrapper: sending update to cc: {[{uni...dule=grpc
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.360923020+03:00" level=info msg="ClientConn switching balancer to \"pick_first\"...dule=grpc
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.376730087+03:00" level=info msg="Loading containers: start."
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.459055220+03:00" level=info msg="Loading containers: done."
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.472901595+03:00" level=info msg="Docker daemon" commit=369ce74 graphdriver(s)=ov...9.03.6-ce
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.473236887+03:00" level=info msg="Daemon has completed initialization"
Jul 09 11:24:27 amazon-linux systemd[1]: Started Docker Application Container Engine.
Jul 09 11:24:27 amazon-linux dockerd[5293]: time="2020-07-09T11:24:27.498674702+03:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.

Test Docker Installation on Amazon Linux 2

Now that Docker has been installed on Amazon Linux 2 machine, let’s try pull a test image. Remember we added our user to docker group which means we should be able to run docker commands without sudo.

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

Run the image.

$ docker run --rm hello-world

Hello 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/

Our next tutorial will cover the installation and usage of Docker Compose on Amazon Linux. In the meantime check other guides we have on Amazon Linux.

How To Install MariaDB on Amazon Linux 2

Install PHP 7 on Amazon Linux 2

How To Install Podman in Amazon Linux 2

LEAVE A REPLY

Please enter your comment!
Please enter your name here

3 × 5 =