Docker

Install Docker on Debian 9

How to Install and Use Docker on Debian 9 Stretch

Docker is a containerization system like LXC for virtualizing Linux operating systems using the same kernel as the host operating system. Containers are fast and lightweight. They don’t need much disk space or RAM to run.

The Docker repository has pre-built images for almost everything you may want to do. For example, if you need to develop a PHP website, you can download a Docker PHP image and start developing. You don’t have to download and set up all the packages one by one as you do on a typical Linux system. This is why it is widely used by software developers all over the world.

In this article, I will show you how to install and use Docker CE on Debian 9 Stretch. Let’s get started.

Installing Docker

Docker Community Edition or Docker CE is not available in the official package repository of Debian 9 Stretch. But it can be easily installed from the official package repository of Docker.  First update the apt package repository cache with the following command:

$ sudo apt-get update

The apt package repository cache should be updated.

Now run the following command to install the packages required to install Docker CE:

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

Now press y and then press <Enter> to continue.

Now add the GPG key of official Docker repository with the following command:

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

The GPG key of the official Docker repository should be added.

Now add the official Docker CE repository with the following command:

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

Now update the apt package repository cache again with the following command:

$ sudo apt-get update

The apt package repository cache should be updated.

Now install Docker CE with the following command:

$ sudo apt-get install docker-ce

Press y and then press <Enter> to continue.

Docker CE should be installed.

Now check whether docker service is running with the following command:

$ sudo systemctl status docker

As you can see from the screenshot below, docker service is running.

If it’s not running, you can start docker service with the following command:

$ sudo systemctl start docker

Also add docker service to the system startup with the following command, so it will start when your computer boots.

$ sudo systemctl enable docker

As you can see, docker service is added to the system startup.

Now check whether Docker CE is working with the following command:

$ sudo docker version

As you can see from the screenshot below, Docker CE is working correctly.

Now that Docker CE is installed and working correctly, from the next section, I will show you how to use Docker CE.

Searching for Docker Images on Docker Image Repository

Just like Debian package repository, Docker also has an image repository where all the Docker images are hosted. You can search for images in the Docker image repository and download the ones you need.

To search for a Docker image, let’s say alpine Linux Docker image, run the following command:

$ sudo docker search "alpine"

The search result should be displayed as you can see from the screenshot below. The marked column is the name of the Docker image. This is what you use to download that particular Docker image. I am going to install the first Docker image alpine.

Downloading and Listing Docker Images

Now to download the alpine Docker image from the earlier section, run the following command:

$ sudo docker pull alpine

The alpine Docker image is downloaded.

You can list the Docker images available on your computer with the following command:

$ sudo docker images

As you can see from the marked section of the screenshot below, the alpine image I just downloaded is listed. You can find information such as Image ID, SIZE from here as well.

Creating a Docker Container

In this section, I am going to show you how to create a Docker container.

You can create a Docker container of the Docker alpine image with the following command:

$ sudo docker run alpine

If you want to create a container of the Docker image alpine and log into the shell of the container at the same time, run the following command:

$ sudo docker run -it alpine

As you can see from the screenshot below, you’re logged into the shell of the container.

You can run any command you like here.

Once you’re done with the container, run the following command to exit out of it.

$ exit

You can also run a command and create a container, for example:

$ sudo docker run alpine echo "Hello World"

Listing the Docker Containers

You can get a list of all the running Docker containers with the following command:

$ sudo docker ps

As you can see from the screenshot below, only the container 6f2488135966, which is an alpine container, is running a command sleep 1000.

You can get a list of all the running and not running Docker containers with the following command:

$ sudo docker ps -a

These are all the containers I have created as you can see in the screenshot below.

Starting a Stopped Container

You can start a stopped container and re run it with the same command as you ran when you created it as follows:

$ sudo docker start -i CONTAINER_NAME
Or
$ sudo docker start -i CONTAINER_ID

NOTE: CONTAINER_ID and CONTAINER_NAME can be found from sudo docker ps -a command as stated above.

For example,

$ sudo docker start -i 97663a84f96c

Attaching a Running Container

You can log into the shell of a Docker container that is running if you’ve exited out of it as follows:

$ sudo docker attach CONTAINER_ID
Or
$ sudo docker attach CONTAINER_NAME

For example:

$ sudo docker attach 6452e4e4890c

That’s how you install and use Docker on Debian 9 Stretch. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.