How To Install and Use Docker on Debian 9/10

Docker is a great building block for automating distributed systems: large-scale web deployments, database clusters, continuous deployment systems, private PaaS, service-oriented architectures, etc. Docker-compose is a service management software built on top of docker. Define your services and their relationships in a simple YAML file, and let compose handle the rest.

This tutorial will show you how to install and use Docker on an Debian 9 or 10.


To begin with this tutorial, you will need one (physical or virtual) machine installed with Debian 9 or 10 having a non-root user with sudo privileges.

Adding Docker Repository
To ensure installing the latest version, we'll install Docker from the official Docker repository. We'll add a new package source and its GPG key from Docker to ensure the downloads are valid, and then install the package:

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

curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"

sudo apt update

Make sure you are about to install from the Docker repo instead of the default Debian repo:

apt-cache policy docker-ce

You'll see output similiar like below, although the version number for Docker may be different:

  Installed: (none)
  Candidate: 5:18.09.7~3-0~debian-buster
  Version table:
     5:18.09.7~3-0~debian-buster 500
        500 buster/stable amd64 Packages

We did notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Debian 10 (buster).

Installing Docker
Finally, install Docker with the following command:

sudo apt install docker-ce

Docker is now installed, the daemon started, and the process enabled to start on boot. Check that it's running:

sudo systemctl status docker

The output will be similar to the following, showing that the service is active and running:

docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-07-09 12:18:19 UTC; 48s ago
 Main PID: 5709 (dockerd)
    Tasks: 8
   Memory: 31.6M
   CGroup: /system.slice/docker.service
           └─5709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Adding User to Docker Group
By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker's installation process.

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

sudo usermod -aG docker username

To apply the new group membership, log out of the server and back in, or type the following:

su - username

You will be prompted to enter your user's password to continue.

Confirm that your user is now added to the docker group by typing:

id -nG

username sudo docker

Working with Docker Images
Docker containers are built 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 applications and Linux distributions you'll need will have images hosted there.

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

docker run hello-world

The output will indicate that Docker is working correctly:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.

Docker was initially unable to find the hello-world image locally, so it downloaded the image from Docker Hub, which is the default repository. Once the image downloaded, Docker created a container from the image and the application within the container executed, displaying the message.

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

docker search ubuntu

The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:

NAME                                                      DESCRIPTION                                      STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…      9704                [OK]
dorowu/ubuntu-desktop-lxde-vnc                      Docker image to provide HTML5 VNC interface …   319                                    [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…        224                                    [OK]
consol/ubuntu-xfce-vnc                                      Ubuntu container with "headless" VNC session…     183                                    [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…       99                  [OK]
ansible/ubuntu14.04-ansible                              Ubuntu 14.04 LTS with ansible                         97                                      [OK]
neurodebian                                                NeuroDebian provides neuroscience research s…      57                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5            50                                      [OK]

Once you've identified the image that you would like to use, you can download it to your computer using the pull subcommand.

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

docker pull ubuntu

You'll see the following output:

Using default tag: latest
latest: Pulling from library/ubuntu
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Status: Downloaded newer image for ubuntu:latest

To see the images that have been downloaded to your computer, type:

docker images

The output should look similar to the following:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4c108a37151f        2 weeks ago         64.2MB
hello-world         latest            fce289e99eb9        6 months ago        1.84kB

Running Docker Container
For testing, let's run a container using the latest pulled image of Ubuntu.

docker run -it ubuntu

Your command prompt should change to reflect the fact that you're now working inside the container and should take this form:


Note the container id in the command prompt. In this example, it is b9d100f2f636. You'll need that container ID later to identify the container when you want to remove it.

Now you can run any command inside the container. For example, let's update the package database inside the container. You don't need to prefix any command with sudo, because you're operating inside the container as the root user:

root@b9d100f2f636:/# apt update

Then install any application in it. Let's install Node.js:

root@b9d100f2f636:/# apt install nodejs

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

root@b9d100f2f636:/# node -v

You'll see the version number displayed in your terminal:


Any changes you make inside the container only apply to that container. To exit the container, type exit at the prompt.

Managing Docker Containers
After using Docker for a while, you'll have many active (running) and inactive containers on your computer. To view the active ones, use:

docker ps

You will see output similar to the following:

CONTAINER ID        IMAGE               COMMAND             CREATED           

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

To view all containers — active and inactive, run docker ps with the -a switch:

docker ps -a

You'll see output similar to this:

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
d42b0bbfbd35        ubuntu              "/bin/bash"         About a minute ago   Exited (0) 20 seconds ago                   friendly_volhard
0740844d024c        hello-world         "/hello"            3 minutes ago        Exited (0) 3 minutes ago                        elegant_neumann

To view the latest container you created, pass it the -l switch:

docker ps -l

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
d42b0bbfbd35        ubuntu              "/bin/bash"         About a minute ago   Exited (0) 34 seconds ago                       friendly_volhard

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

docker start b42b0bbfbd35

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

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
d42d0bbfbd35        ubuntu              "/bin/bash"         About a minute ago   Up 8 seconds                            friendly_volhard

To stop a running container, use docker stop, followed by the container ID or name. This time, we'll use the name that Docker assigned the container, which is friendly_volhard:

docker stop friendly_volhard

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

docker rm elegant_neumann

You can start a new container and give it a name using the --name switch. You can also use the --rm switch to create a container that removes itself when it's stopped. See the docker run help command for more information on these options and others.

Containers can be turned into images which you can use to build new containers. Let's look at how that works.

Convert Container to a Docker Image
When you start up a Docker image, you can create, modify, and delete files just like you can with a virtual machine. The changes that 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 for good.

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

Commit the changes to a new Docker image instance using the following command.

docker commit -m "installed Node.js" -a "username" d42d0bbfbd35 username/ubuntu-nodejs

Docker images again will show the new image, as well as the old one that it was derived from:

docker images

You'll see output like this:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
username/ubuntu-nodejs   latest              d441b62350b4        10 seconds ago      152MB
ubuntu                latest              4c108a37151f        2 weeks ago         64.2MB
hello-world           latest              fce289e99eb9        6 months ago        1.84kB

Pushing Docker Images to a Docker Repository
At this point, we will show you how to push a Docker image to Docker Hub. You will need an account on Docker Hub if you wish to create your own images and push them to Docker Hub.

docker login -u docker-registry-username

You'll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed.

If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username like below.

docker tag username/ubuntu-nodejs docker-registry-username/ubuntu-nodejs

Then you may push your own image using:

docker push docker-registry-username/docker-image-name

To push the ubuntu-nodejs image to the muhammad repository, the command would be:

docker push muhammad/ubuntu-nodejs

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

The push refers to a repository []
e3fbdfb44187: Pushed
5f70df18a086: Pushed
a3d5c80a4eba: Pushed
7f18d442972b: Pushed
3cb512daaf78: Pushed
7bbe4540b42d: Pushed

After pushing an image to a registry, it should be listed on your account's dashboard.

If a push attempt results in an error of this sort, then you likely did not log in:

The push refers to a repository []
e3fbdfb44187: Preparing
5f70df18a086: Preparing
a3d5c80a4eba: Preparing
7f18d442972b: Preparing
3cb512daaf78: Preparing
7bbe4540b42d: Waiting
unauthorized: authentication required

Log in with docker login and repeat the push attempt. Then verify that it exists on your Docker Hub repository page.

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

Wrapping up
Following this lab environment tutorial, you successfully installed Docker, worked with images and containers, and pushed a modified image to Docker Hub.

No comments:

Powered by Blogger.