Technology

How to install Docker on Amazon Linux 2

There is no greater hope in human life than survival. At the same time, it is important to live well. With that in mind, every content on the website is written so that a person can get all the information from here to start his life to make beautifully.

According to that, Technology is one of the topics. It is also a part of life. Read carefully Details of Technology related article

How to install Docker on Amazon Linux 2

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

This page explains how to install and test Docker on Amazon Linux 2 over ssh based session.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements AMI with Docker
Est. reading time 2 minutes

Installing Docker on Amazon Linux 2

The procedure to install Docker on AMI 2 (Amazon Linux 2) running on either EC2 or Lightsail instance is as follows:

  1. Login into remote AWS server using the ssh command:
    ssh ec2-user@ec2-ip-address-dns-name-here
  2. Apply pending updates:
    sudo yum update
  3. Search for Docker package:
    sudo yum search docker
  4. Get version information:
    sudo yum info docker

    Searching for Docker package on Amazon Linux 2 AMI

    Getting Docker version (click to enlarge)

  5. Install docker, run:
    sudo yum install docker

    Amazon Linux 2: Iinstall docker command (click to enlarge)

  6. Need docker-compose too? Try:
    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

     

    Patreon supporters only guides 🤓

      • No ads and tracking

     

     

      • Join my Patreon to support independent content creators and start reading latest guides:

     

    Join Patreon

    How to install docker-compose in Amazon Linux (click to enlarge)

  7. Enable docker service at AMI boot time:
    sudo systemctl enable docker.service
  8. Start the Docker service:
    sudo systemctl start docker.service

Verification

Now that both required software installed, we need to make sure it is working. Hence, type the following commands.

Finding status

Get the docker service status on your AMI instance, run:
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 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.

Getting version info

See docker version:
docker version
Also verify that docker-compose install was a success on AMI 2 by running the following command:
docker-compose version

Getting docker version on AMI using the ssh client

How to control docker service

Use the systemctl command as follows:

sudo systemctl start docker.service #<-- start the service
sudo systemctl stop docker.service #<-- stop the service
sudo systemctl restart docker.service #<-- restart the service
sudo systemctl status docker.service #<-- get the service status

Creating your first Docker project

Make a new project folder using the mkdir command and cd into it using the cd command. For instance:
mkdir static-website-1
cd static-website-1

Use the echo command as follows to create a new index.html for our project:

echo 'Docker Apache static site by nixCraft' > index.html

Make a new Dockerfile using a text editor such as nano command or vim command:
vim Dockerfile
Append the following

FROM centos:8
 
MAINTAINER nixCraft
LABEL Remarks="CentOS 8 test image for installing ng with Apache2"
 
# Install apache2 with less
RUN yum -y update && 
yum -y install httpd && 
yum clean all
 
# Sample index.html for test 
COPY index.html /var/www/html/index.html
 
# Port and set entry point for container 
EXPOSE 80
ENTRYPOINT /usr/sbin/httpd -DFOREGROUND

Build it:
sudo docker build -t staticsite01 .

List images:

sudo docker images

REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
staticsite01   latest    5bb0d40a49e2   38 seconds ago   497MB
centos         8         300e315adb2f   9 months ago     209MB

Run it:

$ sudo docker run -d -p 80:80 --name staticsite01 staticsite01
$ sudo docker ps
$ sudo docker port staticsite01
$ curl 127.0.0.1:80

Click to enlarge

Summing up

That is all for now. You learned how to install Docker on AMI 2 and deploy Apache 2 as the Docker container for a static website. See Amazon Linux 2 home page for more information. Use the following command to get an overview of available commands:
docker help
docker --help

For specific client examples please see the man page for the specific Docker command using the man command. For instance:
man docker-build
man docker-run

 


ADVERTISEMENT
 

 

Did you like this article?
Share it on any of the following social media channels below to give us your vote. Your feedback helps us improve.

Other related Technologies ideas you might enjoy

Related Articles

Leave a Reply

Your email address will not be published.