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:
The apt package repository cache should be updated.
Now run the following command to install the packages required to install Docker CE:
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:
The GPG key of the official Docker repository should be added.
Now add the official Docker CE repository with the following command:
$(lsb_release -cs) stable"
Now update the apt package repository cache again with the following command:
The apt package repository cache should be updated.
Now install Docker CE with the following command:
Press y and then press <Enter> to continue.
Docker CE should be installed.
Now check whether docker service is running with the following command:
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:
Also add docker service to the system startup with the following command, so it will start when your computer boots.
As you can see, docker service is added to the system startup.
Now check whether Docker CE is working with the following command:
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:
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:
The alpine Docker image is downloaded.
You can list the Docker images available on your computer with the following command:
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:
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:
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.
You can also run a command and create a container, for example:
Listing the Docker Containers
You can get a list of all the running Docker containers with the following command:
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:
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:
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,
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:
Or
$ sudo docker attach CONTAINER_NAME
For example:
That’s how you install and use Docker on Debian 9 Stretch. Thanks for reading this article.