How To Install and Use Docker on Ubuntu 20.04 – DigitalOcean

Introduction

Docker is an application that simplifies the process of managing containerized application processes. Containers allow you to run your applications in resource-isolated processes. They are similar to virtual machines, but containers are more portable, more resource-friendly, and more dependent on the host operating system.

For a detailed introduction to the different components

of a Docker container, see The Docker Ecosystem: An Introduction to Common Components

.

In this tutorial, you will install and use Docker Community Edition (CE) on Ubuntu 20.04. You’ll install Docker itself, work with containers and images, and push an image to a Docker repository.

Prerequisites

To follow this tutorial, you will need

the following: An Ubuntu 20.04 server

  • configured following the Ubuntu 20.04 server initial setup guide, including a non-root sudo user and a firewall.
  • An account in Docker Hub

  • if you want to create your own images and submit them to Docker Hub, as shown in steps 7 and 8.

Step 1 —

Docker Installation

The Docker installation package available in the official Ubuntu repository may not be the latest version. To make sure we get the latest version, we’ll install Docker from the official Docker repository. To do this, we’ll add a new package source, add the Docker GPG key to make sure the downloads are valid, and then install the package.

First, update your

existing package list

: sudo apt update

Next, install some prerequisite packages that allow apt to use packages over HTTPS

: sudo apt

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

Then add the

GPG key for the official

Docker repository to your system:

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

Add the Docker repository

to APT feeds:

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

This will also update our package database with

the Docker packages from the newly added repository.

Make sure you are about to install from the Docker repository instead of the default Ubuntu repository

:

  1. apt-cache Docker-CE policy

You’ll see results like this, although the Docker version number may be different

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

AMD64 Note that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 20.04 (focal).

Finally

, install Docker:

  1. sudo apt install docker-ce

Docker should now be installed, the daemon started, and the process enabled to start at boot. Verify that it

is running:

  1. sudo systemctl status

docker

The output should resemble the following, showing that the service is up and

running: Output● docker.service: Docker application container engine loaded: loaded (/lib/systemd/system/docker.service; enabled; provider preset: enabled) Active: active (running) since Tue 2020-05-19 17:00:41 UTC; 17s TriggeredBy ago: ● docker.socket Documents: https://docs.docker.com Main PID: 24321 (dockerd) Tasks: 8 Memory: 46.4M CGroup: /system.slice/docker.service └─24321 /usr/bin/dockerd -H fd:// -containerd=/run/containerd/containerd.sock Docker installation

now provides you with not only the Docker service (daemon) but also the docker command-line utility or Docker client. We’ll explore how to use the docker command later in this tutorial.

Step 2 — Run the Docker command without sudo (optional)

By default, the docker

command can only be run by the root user or a user in the docker group, which is automatically created during the Docker installation process. If you try to run the docker command without prefixing it with sudo or without being in the docker pool, you will get output like this: Outputdocker:

Unable to connect to the Docker daemon. Is the docker daemon running on this host? See ‘docker run -help’.

If you want to avoid typing sudo every time you run

the docker command, add your user name to the docker group:

  1. sudo usermod -aG docker

${USER} To apply the new group membership, log off and log back on to the

server, or type the following

:

  1. su – ${USER}

You will be prompted to enter the user password to continue.

confirm that the user has been added to the coupler group typing

:

  1. groups

Outputsammy sudo docker If you need to add a user to the Docker group that you are

not logged in to, declare that username explicitly using:

  1. sudo usermod -aG docker username The

remainder of this article assumes that you are running the docker command as a user in the docker group. If you choose not to, prepend the commands with sudo.

Let’s explore the docker command below.

Step 3 — Using the Docker Command

Using docker consists of passing it a string of options and commands followed by arguments. The syntax takes this form

: docker [option] [

  1. command] [arguments]

To view all

available subcommands, type:

  1. docker

Starting with Docker 19, the full list of available subcommands includes:

Outbound connection Attach local input, output, and standard error streams to a running container build Create an image from a Dockerfile commit Create a new image from a cp container of changes Copy files/folders between a container and the local file system create Create a new container difference Inspect file or directory changes to file system events in a container Get real-time events from the server executive Run a command on a running container export Export a container file system as a tar file history Show image history of an image Importing list images Import content from a tarball to create file system image information Display system-wide information Inspect Return low-level information on Docker objects kill Kill Kill the load of one or more running containers Upload an image from a tar file or STDIN login Log in to a Docker registry logout Close session in a Docker log Logs Retrieve logs from a container Pause all processes within one or more container ports List of port assignments or a specific assignment for the PS container Extracting list containers Extract an image or repository from a registry push Push an image or repository to a registry rename Rename a container Restart One or more rm containers Remove one or more rmi containers Delete one or more images run Run a command in a new container Save one or more images to a tar file (streamed to STDOUT by default) Search for images in Docker Center Home Start one or more stopped container statistics Display a live stream of container resource usage statistics. stop Stop one or more running container tags Create a TARGET_IMAGE tag that references SOURCE_IMAGE top Show running processes in an Unpause container Retrieve all processes within one or more containers Update the configuration of one or more container versions Show Docker version information wait Lock until one or more containers stop, Then print your exit codes To see

the options available for a specific command, type

: docker docker-subcommand-help

To view system-wide information about Docker, use:

  1. docker info

Let’s explore some of these commands. We’ll start working with images.

Step 4: Work with

Docker images Docker

containers are created from Docker images. By default, Docker pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anyone can host their Docker images on Docker Hub, so most of the Linux applications and distributions you’ll need will have images hosted there.

To check if you can access and download images from Docker Hub, type

: docker run hello-world

The output will indicate that Docker is working correctly

: OutputUnable to find image ‘hello-world:latest’ locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1 Status: Downloaded latest image for

  1. hello-world

:latest Hello from Docker! This message shows that the installation appears to be working correctly. … Initially, Docker

couldn’t find the hello-world image locally, so it downloaded the image from Docker Hub, which is the default repository. Once the image was downloaded, Docker created a container from the image and the application inside the container ran, displaying the message.

You can search for images available in Docker Hub using the docker command with the search subcommand. For example, to search for the Ubuntu image, type:

  1. docker search ubuntu

The script will crawl Docker Hub and return a list of all images whose name matches the search string. In this case, the output will look like this:

OutputNAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Linux operating system based on Debian… 10908 [OK] dorowu / ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 428 [OK] rastasheep / ubuntu-sshd Dockerized SSH service, built on offi… 244 [OK] consol/ubuntu-xfce-vnc Ubuntu container with “headless” VNC session… 218 [OK] ubuntu-upstart Upstart is an event-based replacement for the … 108 [OK] ansible / ubuntu14.04-ansible Ubuntu 14.04 LTS with …

In the OFFICIAL column, OK indicates an image built and supported by the company behind the project. Once you have identified the image you want to use, you can download it to your computer using the pull subcommand.

Run the following command to download the official ubuntu image to your computer:

  1. docker pull

ubuntu

You will see the following output: OutputUsing the

default tag: last last: Library Extract / ubuntu d51af753c3d3: Full extraction fc878cd0a91c: Full extraction 6154df8ff988: Full extraction fee5db0ff82f: Pull complete Abstract: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7 Status: downloaded image more recent

for ubuntu:

latest docker.io/library/ubuntu:latest

After you download an image, you can run a container using the downloaded image with the run subcommand. As you saw with the hello-world example, if an image has not been downloaded when Docker is run with the run subcommand, the Docker client will first download the image and then run a container using it.

To view the images that

have been downloaded to your computer, type

:

  1. Docker

images

The output will resemble the following:

OutputREPOSITORY LABEL IMAGE ID SIZE CREATED Ubuntu latest 1d622ef86b13 3 weeks ago 73.9MB hello world last bf756fb1ae65 4 months ago 13.3kB

As you will see later in this walkthrough, the images that you use to run containers can be modified and used to generate new images. which can then be uploaded (push is the technical term) to Docker Hub or other Docker registries.

Let’s see how to run containers in more detail.

Step 5: Run a Docker

container

The hello-world container you ran in the previous step is an example of a container that runs and exits after issuing a test message. Containers can be much more useful than that, and they can be interactive. After all, they’re similar to virtual machines, only more resource-friendly.

As an example, let’s run a container using the latest Ubuntu image. The combination of the –i and –t switches gives you interactive access to the shell in the container

:

  1. docker run -it ubuntu

Your command prompt should

change to reflect the fact that you are now working inside the container and should take this form

: Outputroot@d9b100f2f636:/#

Note the container ID at the command prompt. In this example, it is d9b100f2f636. You’ll need that container ID later to identify the container when you want to delete it.

You can now execute any command inside the container. For example, let’s update the package database inside the container. You don’t need to prefix any commands with sudo, because you are operating inside the container as root user:

  1. apt update

Then install any application on it. Let’s

install Node.js:

  1. apt install nodejs

This installs Node.js in the container from the official Ubuntu repository. When the installation is complete, verify that Node.js is installed

:

  1. node -v

You will see the version number displayed in your terminal:

Outputv10.19.0

Any changes you make inside the container only apply to that container

.

To exit the container, type exit at the command prompt

.

Let’s see below the management of containers in our system.

Step 6: Manage

Docker containers After using Docker for a while, you will have many active (running) and inactive containers on your computer. To view assets, use

:

  1. docker ps

You will see output similar to the following:

OutputCONTAINER ID IMAGE COMMAND CREATED

In this tutorial, you launched two containers; one from the hello-world image and one from the ubuntu image. Both containers are no longer running, but still exist on your system.

To view

all containers, active and inactive, run

docker ps with the -a switch:

  1. docker ps

-a

You will see output similar to this:

1c08a7a0d0e4 ubuntu “/bin/bash” 2 minutes ago Exited (0) 8 seconds ago quizzical_mcnulty a707221a5f6c hello-world “/hello” 6 minutes ago Exited (0) 6 minutes ago

youthful_curie To view the last container you created, Pass it the switch

-l: docker

  1. ps -l
  1. CREATED CONTAINER ID IMAGE COMMAND STATE PORT NAMES
  2. 1c08a7a0d0e4 ubuntu “/bin/bash” 2 minutes ago Exited (0) 40 seconds ago quizzical_mcnulty To

start

a stopped container, use docker start, followed by the container ID or container name. Let’s start the Ubuntu-based container with the ID of

1c08a7a0d0e4: docker start 1c08a7a0d0e4

The container will start and you can use docker ps to view its status:

OutputCREATED CONTAINER ID IMAGE COMMAND STATE PORT NAMES

  1. 1c08a7a0d0e4

ubuntu “/bin/bash” 3 minutes ago Up to 5 seconds quizzical_mcnulty

To stop a running container, use docker stop, followed by the container ID or name. This time, we’ll use the name Docker assigned to

the container, which is quizzical_mcnulty: docker

  1. stop quizzical_mcnulty

Once you’ve decided you no longer need a container, delete it with the docker rm command, again using the container ID or name. Use the docker ps -a command to find the container ID or container name associated with the hello-world image and delete it.

  1. docker rm youthful_curie

You can start a new container and name it by using the -name switch. You can also use the -rm switch to create a container that is removed only when it is stopped. See the docker run help command for more information about these options and others.

Containers can be converted into images that you can use to create new containers. Let’s see how that works.

Step 7: Commit changes to a container in

a Docker image When you

launch a Docker image, you can create, modify, and delete files just as you can with a virtual machine. Any changes you make will only apply to that container. You can start and stop it, but once you destroy it with the docker rm command, the changes will be lost forever.

This section shows how to save the state of a container as a new Docker image.

After installing Node.js inside the Ubuntu container, you now have a container that runs on an image, but the container is different from the image you used to create it. But you may want to reuse this container .js node as a basis for new images later.

Next, commit the changes to a new Docker image instance using the following command.

docker commit -m “What you did with the image” -a “

  1. Author name” container_idrepository/new_image_name

The -m switch is for the confirmation message that helps you and others know what changes you have made, while -a is used to specify the author. The container_id is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you have created additional repositories in Docker Hub, the repository is usually your Docker Hub username.

For example, for the user sammy, with the container ID of d9b100f2f636, the command would be: docker commit -m “

  1. added Node.js” -a “sammy” d9b100f2f636sammy/ubuntu-nodejs

When you commit an image, the new image is saved locally on your computer. Later in this tutorial, you’ll learn how to push an image into a Docker registry like Docker Hub so that others can access it.

Enumerating the Docker images

again will display the new image, as well as the old one from which it was derived:

  1. Docker images

You will see output like this: OutputREPOSITORY TAG

IMAGE ID CREATED SIZE sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB…

In this example, ubuntu-nodejs is the new image, which was derived from the existing ubuntu image from Docker Hub. The difference in size reflects the changes that were made. And in this example, the change was that NodeJS was installed. So the next time you need to run a container using Ubuntu with NodeJS pre-installed, you can use the new image.

You can also create images from a Dockerfile, allowing you to automate the installation of software on a new image. However, that’s beyond the scope of this tutorial.

Now let’s share the new image with others so they can create containers from it.

Step 8: Push

Docker images to a Docker repository

The next logical step after creating a new image from an existing image is to share it with some of your friends, everyone on Docker Hub, or another Docker registry you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.

This section shows how to push a Docker image into Docker Hub. To learn how to create your own private Docker registry, see How to Set Up a Docker Private Registry in Ubuntu 18.04.

To push the image, first log in to Docker Hub.

  1. docker login -u docker-registry-username

You will be prompted to authenticate with your Docker Hub password. If you specified the correct password, authentication should succeed.

You can then submit

your own image using

: docker push docker-registry-username/docker-image-name To submit the ubuntu-nodejs image to the sammy repository, the command would be:

  1. docker push sammy/ubuntu-nodejs

The process may take some time to complete as you upload the images, but when it completes, the output will look like this:

OutputPush refers to a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Pushed 5f70bf18a086: Pushed a3b5c80a4eba: Pushed 7f18b442972b: Pushed 3ce512daaf78: Pushed 7aae4540b42d: Pushed …

After sending an image to a registry, it should appear in the control panel of your account, as shown in the image below.

New list of Docker images in Docker Hub

If a push attempt

fails with such an error, you are probably not logged in

: OutputPush references a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Preparation 5f70bf18a086: Preparation a3b5c80a4eba: Preparation 7f18b442972b: Preparation 3ce512daaf78: Preparation 7aae4540b42d: Waiting No authorization: Authentication required

Log in with the Docker login and repeat the push attempt. Next, verify that it exists on the Docker Hub repository page.

You can now use docker pull sammy/ubuntu-nodejs to pull the image to a new machine and use it to run a new container.

Conclusion

In this tutorial you installed Docker, worked with images and containers, and submitted a modified image to Docker Hub. Now that you know the basics, explore the other Docker tutorials in the DigitalOcean Community.