How to Install and Use Docker on Debian 9

Updated on

6 min read

Install and Use Docker on Debian 9

Docker is a containerization platform that allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can run virtually anywhere.

Docker is de facto standard for container technology and it is an essential tool for DevOps engineers and their continuous integration and delivery pipeline.

In this tutorial, we will guide you through the process of installing Docker on a Debian 9 machine and explore the basic Docker concepts and commands.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges . All the commands in this tutorial should be run as a non-root user.

Install Docker on Debian

The following steps describe how to install the latest stable Docker version from the Docker’s repositories.

  1. Update the installed packages to the latest version:

    sudo apt updatesudo apt upgrade
  2. Install the dependencies necessary to add a new repository over HTTPS:

    sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg2
  3. Import the repository’s GPG key using the following curl command :

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

    Add the Docker APT repository to your system’s software repository list by typing:

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

    $(lsb_release -cs) will return the name of the Debian distribution, in this case, it will return stretch.

  4. Now that the Docker repository is enabled, update the apt package list and install the latest version of Docker CE (Community Edition) with:

    sudo apt updatesudo apt install docker-ce
  5. Once the installation is completed the Docker service will start automatically. You can verify it by typing:

    sudo systemctl status docker
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-07-27 17:02:07 UTC; 1min 14s ago
        Docs: https://docs.docker.com
    Main PID: 16929 (dockerd)
    CGroup: /system.slice/docker.service
  6. At the time of writing, the current version of Docker available for Debian 9 is 18.06.0-ce. Check the Docker version with:

    docker -v
    Docker version 18.06.0-ce, build 0ffa825

Executing the Docker Command Without Sudo

By default, only a user with administrator privileges can execute Docker commands.

If you want to run Docker commands as a non-root user without prepending sudo you’ll need to add your user to the docker group which is created during the installation of the Docker CE package. You can do that by typing:

sudo usermod -aG docker $USER

$USER is an environment variable that holds your username.

Log out and log back in so that the group membership is refreshed.

To verify that you can run docker commands without prepending sudo run the following command which will download a test image, run it in a container, print a “Hello from Docker” message and exit:

docker container run hello-world

The output should look like the following:

Docker Hello World

Docker command line interface

Now that we have Docker installed, let’s go over the basic syntax of the docker CLI:

docker [option] [subcommand] [arguments]

To list all available commands run docker with no parameters:

docker

If you need more help on any [subcommand], you can use the --help switch as shown below:

docker [subcommand] --help

Docker Images

A Docker image is made up of a series of filesystem layers representing instructions in the image’s Dockerfile that make up an executable software application. An image is an immutable binary file including the application and all other dependencies such as libraries, binaries, and instructions necessary for running the application.

You can think of a Docker image as a snapshot of a Docker container.

Most Docker images are available on Docker Hub.

The Docker Hub is cloud-based registry service which among other functionalities is used for keeping the Docker images either in a public or private repository.

Search Docker Image

To search for an image from Docker Hub registry, use the search subcommand.

For example, to search for a Debian image, you would type:

docker search debian

The output should look like this:

Docker Search Image

As you can see the search results prints a table with five columns, NAME, DESCRIPTION, STARS, OFFICIAL and AUTOMATED.

The official image is an image that Docker develops in conjunction with upstream partners.

Most Docker images on Docker Hub are tagged with version numbers. When no tag is specified Docker will pull the latest image.

Download Docker Image

If we want to download the official build of the Debian image we can do that by using the image pull subcommand:

docker image pull debian
Docker Pull Image

Depending on your Internet speed, the download may take a few seconds or a few minutes.

Since we haven’t specified a tag, docker will pull the latest Debian image which is 9.5. If you want to pull some of the previous Debian versions , let’s say Debian 8 then you need to use docker image pull debian:8

Once the image is downloaded we can list the images by typing:

docker image ls

The output will look something like this:

Docker List Image

Remove Docker Image

If for some reason you want to delete an image you can do that with the image rm [image_name] subcommand:

docker image rm debian
Docker Remove Image

Docker Containers

An instance of an image is called a container. A container represents a runtime for a single application, process, or service.

It may not be the most appropriate comparison but if you are a programmer you can think of a Docker image as class and Docker container as an instance of a class.

We can start, stop, remove and manage a container with the docker container subcommand.

Start Docker Container

The following command will start a Docker container based on the Debian image. If you don’t have the image locally, it will be downloaded first:

docker container run debian

At first sight, it may seem to you that nothing happened at all. Well, that is not true. The Debian container stops immediately after booting up because it does not have a long-running process and we didn’t provide any command, so the container booted up, ran an empty command and then exited.

The switch -it allows us to interact with the container via the command line. To start an interactive container type:

docker container run -it debian /bin/bash
root@ee86c8c81b3b:/#

As you can see from the output above once the container is started the command prompt is changed which means that you’re now working from inside the container .

List Docker Containers

To list running containers , type:

docker container ls
Docker List Containers
If you don’t have any running containers the output will be empty.

To view all containers, pass the -a switch:

docker container ls -a
Docker List All Containers

Remove Docker Containers

To delete one or more containers just copy the container ID (or IDs) and paste them after the container rm subcommand:

docker container rm c55680af670c

Conclusion

You have learned how to install Docker on your Debian 9 machine and how to download Docker images and manage Docker containers. This tutorial barely scratches the surface of the Docker ecosystem. In some of our next articles, we will continue to dive into other aspects of Docker.

You should also check out the official Docker documentation .

If you have any questions, please leave a comment below.