How To Install and Use Docker on Ubuntu 18.04 – DigitalOcean

An earlier version of this tutorial was written by finid.

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 18.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 18.04 server

  • configured following the Ubuntu 18.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

: sudo

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

Next, update the package database with

the Docker packages from the newly added repository:

  1. sudo apt update

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

:

  1. apt-cache policy

docker-ce

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

: docker-ce: Installed: (none) Candidate: 18.03.1~ce~3-0~ubuntu Version table: 18.03.1~ce~3-0~ubuntu 500 500 https://download.docker.com/linux/ubuntu Bionic/Stable AMD64 Packages

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

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 Mon 2021-08-09 19:42:32 UTC; 33s Documents ago: https://docs.docker.com Main PID: 5231 (dockerd) Tasks: 7 CGroup: /system.slice/docker.service └─5231 /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 group, you will get output like this

: Outputdocker: Permission was denied while trying to connect to the Docker daemon socket in unix:///var/run/docker.sock: Post “http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create”: unix dial /var/run/docker.sock: connect: permission denied. 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

: id -nG 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 20, the full list of available subcommands includes:

Outbound connection Attach local input, output, and standard errors to a running container build Create an image from a Dockerfile commit Create a new image from the changes of a cp container 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 exec server Run a command on a running container export Export a container’s file system as a tar file history Show image history of an image Importing list images Import content from a tarball to create a file system image information Display system-wide information Inspect Return low-level information about Docker objects kill Kill One or more running containers are loaded Upload an image from a tar file or STDIN login Log in to a Docker registry logout Sign out a Docker log Records Retrieve logs from a container pause Pause all processes within one or more container ports List port assignments or a specific assignment for the PS container Extracting list containers Check out 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 a command on 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 stopping usage statistics Resources 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 Display Docker version information Wait Lock until one or more containers stop and, 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 latest locally ‘hello-world:latest’ image: Extracting from library/hello-world b8dfde127a29: Pull complete Summary: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e 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… 7917 [OK] dorowu / ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 193 [OK] rastasheep / ubuntu-sshd Dockerized SSH service, built on offi … 156 [OK] ansible / ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 93 [OK] ubuntu-upstart Upstart is an event-based replacement for the … 87 [OK] neurodebian NeuroDebian provides research in neuroscience … 50 [OK] ubuntu-debootstrap debootstrap -variant=minbase -components=m… 38 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 36 [OK] nuagebec/ubuntu Simple Always Updated Ubuntu docker images w… 23 [OK] tutum/ubuntu Simple Ubuntu docker images with SSH access 18 i386/ubuntu Ubuntu is a Linux operating system based on Debian… 13 ppc64le/ubuntu Ubuntu is a Linux operating system based on Debian… 12 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 10 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 6 [OK] eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 [OK] codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 [OK] darksheer/ubuntu Base Ubuntu Image – Updated every hour 4 [OK] 1and1internet/ubuntu-16-apache ubuntu-16-apache 3 [OK] 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3 [OK] 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK] pivotaldata/ubuntu A quick update to the Ubuntu base document … 1 1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0[OK] pivotaldata/ubuntu-gpdb-dev Ubuntu Images for GPDB Development 0 Smartentry/Ubuntu Ubuntu with Smartentry 0 [OK] ossobv/ubuntu…

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: Extracting from library / ubuntu 16ec32c2132b: Pull complete Summary: sha256:82becede498899ec668628e7cb0ad87b6e1c371cb8a1e597d83a47fac21d6af3 Status: Downloaded latest image

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 should resemble the following:

OutputREPOSITORY LABEL IMAGE ID SIZE CREATED Ubuntu latest 1318b700e415 13 days ago 72.8MB hello world last d1165f221234 5 months ago 13.3kB

As you will see later in this tutorial, 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 STATE PORT NAMES

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:

  1. docker ps –

a switch

You will see output similar to this:

e4dcb273b696 ubuntu “bash” About a minute ago Exited (0) 30 seconds ago suspicious_hopper 79b892f318e9 hello world “/hello” 3 minutes ago Exited (0) 3 minutes ago boring_jang

To view the last container you created, Pass it the switch -l:

  1. Docker PS -L
  1. CONTAINER ID IMAGE COMMAND CREATED STATE PORT NAMES
  2. e4dcb273b696 ubuntu “bash” 2 minutes ago Exited (0) About a minute ago suspicious_hopper

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 e4dcb273b696: docker start e4dcb273b696

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

CREATED CONTAINER ID IMAGE COMMAND STATE PORT NAMES

  1. e4dcb273b696

Ubuntu “Bash” 2 minutes ago Up to 4 seconds suspicious_hopper

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 sharp_volhard: docker

  1. stop suspicious_hopper

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 boring_jang

You can start a new container and name it 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:

  1. docker commit -m “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.

Relisting Docker images 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 SIZE CREATED SAMMY/UBUNTU-NODEJS LATEST 7C1F35226CA6 7 SECONDS AGO 179MB UBUNTU LATEST 113A43FAA138 4 WEEKS AGO 81.2MB HELLO WORLD LATEST E38BC07AC18E 2 MONTHS AGO 1.85KB

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 Docker private registry, see How to Set Up a Docker Private Registry in Ubuntu 14.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 an 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: Wait no authorized: 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.

Contact US