How to Install Docker on Ubuntu 20.04 LTS

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

We can Create or build Images & push to Docker Hub or Amazon Elastic Registry. We can easily download images from docker hub or public elastic registry repository.

Install the required packages.

apt-get update

apt install apt-transport-https ca-certificates curl software-properties-common

apt-get update
apt install apt-transport-https ca-certificates curl software-properties-common

 

Add GPG Key and install the Docker repository package.

 

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable”

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

 

Update the Docker packages repository.

apt update

apt update

 

Verify the Ubuntu policy for the Docker package installation.

apt-cache policy docker-ce

apt-cache policy docker-ce

 

Here is the command output.

 

Fig. 6

 

Install Docker.

sudo apt install docker-ce

sudo apt install docker-ce

 

Check Docker status.

service docker status

service docker status

 

Here is the command output.

 

Fig 9

 

Configuration :-

  • Once Docker is Installed create a folder where you want all docker file are placed.    
mkdir  project
  • Create a file and provide a name dockerfile.
touch dockerfile
  • Then open dockerfile using vim editor.
  vim dockerfile
  • Type the docker image code.

For example. First provide the OS name & version.

Then type your code. Lets install apache2 on ubuntu 18.04

FROM ubuntu:18.04

Run apt update -y
Run apt install apache2 -y

  • Provide Port Number & apache2 service always start mode.
CMD apache2ctl -D FOREGROUND
EXPOSE 80 443
  • If you are using supervisor then provide the code at dockerfile.
RUN apt-get update -y --fix-missing && apt-get install -y  supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  • If require multiple services are always working  or running mode then create a supervisord.conf file. Supervisor is used when we need to run multiple process within the container.
 vim supervisord.conf

Type the below code:-

[supervisord]

nodaemon=true
[program:apache2]
command=/usr/sbin/apache2ctl -D FOREGROUND
autorestart=true
  • Exit from project folder.
  • Finally Dockerfile is ready for creating image. run the following command:-  
docker build -t image_name

  • Once docker image will be created check the list of available Docker image using the following command.
docker images ls
  • Provide tag on docker image.
docker tag tag_name image_name

 

  • Now create a new container named container1 using the docker run command below.
docker run -d -v -p 80:80 --name container1 image_name 

 

  • After that, check all running containers using the following command.
docker ps
  • Now test access your container with the curl command on the port 8080.

    curl server-ip:80

    curl -I server-ip:80

  • Start container.
docker start continer-id

 

  • Login to the container

 

docker exec -it <conatiner-id>  /bin/bash

Ping Between Containers

  • Create two containers.
docker run -d -v -p 80:80 --name container1 image_name1
docker run -d -v -p 80:80 --name container2 image_name2

 

  • Create a custom network.

docker network create net1
docker network inspect net1

                      or 

docker network create net2
docker network inspect net2

 

  • List the networks.
docker network ls
  • Connect the network with the containers.
docker network connect net1 image_name1
docker network connect  net2 image_name2
  • Before you try to make the ping you should install iputils-ping in the container to make it work. So we enter into the container named image_name1.
docker exec -it  container-id /bin/bash
apt update
apt install iputils-ping

 

  • You’re ready to run a ping command.
docker exec it  container-id1 ping container-id2

             or 

docker exec -it image_name1 ping image_name2

 

 

Leave a Reply

Your email address will not be published.