How to Install Docker on Ubuntu 20.04

How to Install Docker on Ubuntu 20.04 – Docker is a set of platform-as-a-service products used to launch and manage container. Developers use Docker containers to develop and deploy applications because they provide an isolated and lightweight virtual environment.

Docker is an open source platform that uses OS-level virtualization to deliver software in packages called container. Container is a unit of software that packages code and all of its dependencies so that applications run quickly and reliably from one computing environment to another.

Technology container Docker was launched in 2013 which started a revolution in application development – ​​by democratizing software containers.

What is needed to Install Docker on Ubuntu 20.04?

  • VPS/Cloud computing/Virtual machines
  • Ubuntu 20.04
  • SSH & Users with sudo privileges

This Docker installation test was tested using AWS (Amazon Web Services) EC2, will also work fine on other cloud services and on VPS or dedicated servers running Ubuntu.

Install Docker on Ubuntu 20.04

Install Docker on Ubuntu
Ubuntu Docker

There are two options for installing Docker on Ubuntu:

  1. Install Docker using Docker Official Repository
  2. Install Docker using Default Repository

Install Docker using Docker Official Repository

Step 1: Update Package index

Before starting the installation, as usual we need access to the server via SSH. After logging into the server via SSH, make sure and make it a habit to update the package index. So that all installed packages are updated to the new version.

sudo apt update & sudo apt upgrade

Step 2: Download Dependencies

Most of the software dependencies may have been installed automatically, to ensure install the software dependencies below.

Grants package manager permission to transfer files and data via https:

sudo apt install apt-transport-https

Give the system permission to check the security certificate:

sudo apt install ca-certificates

Install curl, tools to transfer data:

sudo apt install curl

Install software-properties-common:

sudo apt install software-properties-common

Step 3: Adding Docker GPG Key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Step 4: Adding Docker Repository

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

After successfully adding the Docker Repository, then perform the update:

sudo apt update
sudo apt-cache policy docker-ce
docker-ce:
  Installed: (none)
  Candidate: 5:20.10.10~3-0~ubuntu-focal
  Version table:
     5:20.10.10~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.9~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker policy
docker-ce

Step 5: Install the latest Docker

After performing an update on the new repository then proceed to install Docker:

sudo apt install docker-ce

Step 6: Verify Docker Installation

Check the version of Docker that was successfully installed:

docker --version
Docker version 20.10.10, build b485636

Check if Docker is running automatically on Ubuntu system:

sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enab>
     Active: active (running) since Mon 2021-11-15 15:55:29 U>
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 15159 (dockerd)
      Tasks: 7
     Memory: 39.3M
     CGroup: /system.slice/docker.service
             └─15159 /usr/bin/dockerd -H fd:// --containerd=/>

Nov 15 15:55:29 ip-172-31-31-63 dockerd[15159]: time="2021-11>
Nov 15 15:55:29 ip-172-31-31-63 systemd[1]: Started Docker Ap>
Nov 15 15:55:29 ip-172-31-31-63 dockerd[15159]: time="2021-11>
lines 1-21/21 (END)
docker status
Docker status

If Docker is not running or status Inactive (dead) then we need to run with the command:

sudo systemctl start docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; disa>
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
docker inactive
Docker inactive

If you want Docker to run automatically on system startup or on reboot:

sudo systemctl enable docker

Install Docker using Default Repository

Step 1: Update Package index

Before starting the installation, as usual we need access to the server via SSH. After logging into the server via SSH, make sure and make it a habit to update the package index. So that all installed packages are updated to the new version.

sudo apt update & sudo apt upgrade

Step 2: Install Docker

After performing an update on the new repository then proceed to install Docker:

sudo apt install docker docker.io -y

Step 3: Verify Docker Installation

docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2

Check if Docker is running automatically on Ubuntu system:

sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enab>
     Active: active (running) since Mon 2021-11-15 15:55:29 U>
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 15159 (dockerd)
      Tasks: 7
     Memory: 39.3M
     CGroup: /system.slice/docker.service
             └─15159 /usr/bin/dockerd -H fd:// --containerd=/>

If Docker is not running or status Inactive (dead) then we need to run with the command:

sudo systemctl start docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; disa>
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
docker inactive
Docker inactive

If you want Docker to run automatically on system startup or on reboot:

sudo systemctl enable docker

Use of Docker on Ubuntu 20.04

The basic syntax for the docker command is:

sudo docker [option] [command] [argument]

Running Docker Commands without SUDO

By default, we can only use the docker command with root privileges (using sudo command prefix). For example, if we try to run the hello-world container, it will be rejected.

docker run hello-world 
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
docker permission denied
Permission denied

If we need to run the docker command without sudo, we need to add the username to the Docker group.

sudo usermod -aG docker <username>

After adding users in the Docker group, exit SSH (exit) and login/login back into SSH.

PicsArt 11 15 11.16.37
System restart required

If the system requires restart, then restart the Ubuntu system first with the command: sudo reboot, and wait a few moments to log back into SSH.

Running Docker Command

To find out information about Docker run this command:

docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.10
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.11.0-1021-aws
 Operating System: Ubuntu 20.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 969MiB
 Name: ip-172-31-31-63
 ID: LP3H:FHWS:3U7H:GKRV:RAAU:KVJO:EQ3M:KAVJ:4WW4:Q62Y:ZG3N:HPG6
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Now we can run docker commands without sudo.

Download Docker Images

Docker images are files that contain source code, libraries, dependencies, tools, and other files that the container needs. We can create Docker images with Dockerfiles or use the existing ones available on the Docker Hub.

To download new Docker Images you can use this command:

docker pull [image_name]

If you don’t know the exact name of the image, look it up in the Docker repository with:

docker search ubuntu

After we successfully run the Docker command without sudo, then we run hello-world container with this command:

docker run hello-world

If the output we get is similar to below then we can access and download images from Docker Hub.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest

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/

Run the command below to view the downloaded images:

docker images
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/
docker hello world success
Docker hello world

Working with Docker containers

docker container
Docker containers

To run a container based on an existing Docker image, use the command:

docker run [image_name]

To run the container in interactive mode and change to the container command prompt, run:

docker run -it [image_name]

To see all active containers, use the following command:

docker ps

To see all active and inactive containers, use the following command:

docker ps -a

To view the latest containers run the following command:

docker ps -l

To start a docker container, use the command docker start followed by Container ID or Container Name.

docker start container-id/name

Likewise, to stop the running container we can use the command docker stop followed by Container ID or Container Name.

docker stop container-id/name

For more details about docker command use command docker run help.

docker run help

How to Uninstall Docker

If we want to remove Docker from Ubuntu system run this command:

sudo apt purge docker docker.io
sudo apt autoremove

Conclusion

By following the tutorial/how-to/method above, we can learn How to Install Docker on Ubuntu 20.04, both installation using the Default Repository and Official Repository and also run the docker container and also run commands without sudo.

Hope it is useful.




Leave a comment

Your email address will not be published.