How to Install Docker on Ubuntu 20.04

install docker on ubuntu 20.04

Docker is an open-source platform that enables software developers to build, deploy and manage containerized applications. It means the applications are packaged into container which is combination of source codes, libraries and other dependencies required to run in any operating system.

In addition, docker makes it simpler, smoother and more secure when working with container.

In this tutorial, I will go through all steps of installing Docker on Ubuntu 20.04 and learn the fundamental concepts when working with docker containers/images.

Install docker

In the official software repository of Ubuntu 20.04, the docker installation package is available by default. However, it isn't the up-to-date version. It's a recommendation to install the latest version of docker from the official docker repository.

Firstly, you need to update the current packages list and install the prerequisite packages in order to add new repository to your system by running the following commands:

$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent

Next, you have to import the GPG key of Docker repository:

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

Then, add the docker repository to APT sources on your system:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Run the following command to update the newly added repository:

$ sudo apt update

Ensure that you are going to install docker from its repository instead of the Ubuntu repository:

$ apt-cache policy docker-ce

You will see the output is something like this:

docker-ce:
  Installed: (none)
  Candidate: 5:19.03.11~3-0~ubuntu-focal
  Version table:
     5:19.03.11~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.10~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.9~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

The above output indicates that the 'docker-ce' from the Docker repo for Ubuntu 20.04 is going to be installed on your system.

Finally, install docker-ce (community edition):

$ sudo apt install docker-ce

In order to verify whether docker was successfully installed and running properly or not, run the following command:

$ sudo systemctl status docker

The output will show you that the docker service is active (running):

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-06-17 15:29:48 +07; 8min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 136640 (dockerd)
      Tasks: 17
     Memory: 50.8M
     CGroup: /system.slice/docker.service
             └─136640 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
check docker service status

docker service status

You've just installed docker successfully on your Ubuntu machine. We will deep dive into the way of using the docker command in the following sections of this tutorial.

Execute docker without sudo privilege

By default, you have to run docker commands with sudo privilege or by a user in the docker group. If you try to run the docker commands without sudo or with a privilege of a user doesn't in docker group, you'll get the error like this:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

The workaround is adding the user to the 'docker' group:

$ sudo usermod -aG docker <user-name>

Logout the current session then login again to apply the membership. Verifying that the user is in 'docker' group by running the command:

$ groups
ubuntu sudo docker

From now on, you can run the docker command without 'sudo'.

Docker commands

This section will explain how to use docker command. Before going into detail, let's take look at the syntax of 'docker' command:

$ docker [options] [sub-commands] [arguments]

To list all available sub-commands of docker, run:

$ docker

There is a lot of options, sub-commands and arguments need to be remembered. Let's delve into some of them in the following sections of this tutorial.

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command. 

Docker images

Docker images consist of source code, libraries, and all necessary dependencies that the application requires to run as a container. Docker images can be built from scratch, but you can pull these images down from a container registry. The registry is where you can store images and download them using a unique name. By default, the container registry of docker is Docker Hub.

In order to check whether you can pull images from Docker Hub, you can run the following command:

$ docker run hello-world

As you can see in the following output: docker could not find the 'hello-world' image locally and it pulled the image from Docker Hub. After the image has been downloaded, docker creates a container from that image. The application in the container will be executed and display the message:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
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
...

In order to search for an image on the Docker Hub, you can use the 'docker' command with 'search' sub-command. Assuming that you want to search for the 'alpine' image, run:

$ docker search alpine
docker search alpine

docker search command

As you can see, the output returns a list of all images whose name has 'alpine'.

Moreover, you can download a docker image by running command 'docker image pull' follow with the name of the image. For example:

$ docker image pull alpine

Depending on the speed of the Internet, the download may take a minute.

Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete 
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

In addition, if you want to list all the download images, run the following command:

$ docker image ls

The output will show you the detail of the images includes: repository, tag, image id, created time and file size.

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              a24bb4013296        2 weeks ago         5.57MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

Sometimes, you need to delete docker image, you can run the command:

$ docker image rm <image-name>

or

$ docker image rm <image-id>

Docker containers

A container is a running instance of a docker image. A container performs a runtime of single application or process. You can interact with a container and start/stop or remove it by running the 'docker container' sub-command.

For example, in order to start a docker container based on the 'Ubuntu' image, run:

ubuntu@linoxide:~$ docker container run ubuntu

After downloading a newer image for Ubuntu from Docker Hub and starting the Ubuntu container, it returns system command prompt because the Ubuntu container has been stopped immediately after booting up.

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a4a2a29f9ba4: Pull complete 
127c9761dcba: Pull complete 
d13bf203e905: Pull complete 
4039240d2e0b: Pull complete 
Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
Status: Downloaded newer image for ubuntu:latest
ubuntu@linoxide:~$

In order to interact with the Ubuntu container through the command line, you can start the container with -it option:

$ docker container run -it ubuntu

Now, you've gone into the container and accessed to its interactive shell.

root@45cc68d4395e:/# 

So far, you've already run many container on your Ubuntu system. In order to list all of the active (running) containers, run the following command:

$ docker container ls

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

As the result return an empty list, so there is not any container running. If you want to list all of the containers: active and inactive, run:

$ docker container ls -a

The output is similar as:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
45cc68d4395e        ubuntu              "/bin/bash"         14 minutes ago      Exited (0) 11 minutes ago                         stoic_dhawan
fc91e50d223e        ubuntu              "/bin/bash"         26 minutes ago      Exited (0) 26 minutes ago                         sleepy_ritchie
ce0d81682913        alpine              "/bin/bash"         26 minutes ago      Created                                           trusting_kowalevski
4291477f5ac2        alpine              "/bin/sh"           27 minutes ago      Exited (130) 27 minutes ago                       priceless_wozniak
86c0fd44a4f4        alpine              "/bin/sh"           28 minutes ago      Exited (0) 28 minutes ago                         objective_agnesi
3fdcaa196fca        hello-world         "/hello"            31 minutes ago      Exited (0) 31 minutes ago                         eloquent_rhodes

Once you want to delete/remove a container, run:

$ docker container rm <container-id>

Conclusion

In this tutorial, we learned how to install Docker on Ubuntu 20.04 and how to work with images, containers through examples. They are the basics of Docker concepts and be used usually by developers.

Thanks for reading and please leave your suggestion in the below comment section.

Bobbin Zachariah 10:41 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.