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.
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.
$(lsb_release -cs) will return the name of the Debian distibution, in this case it will return jessie.
Install Docker
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
Copy
Verify the installation
Once the installation is completed the Docker service will start automatically. You can verify it by typing:
sudo systemctl status docker
Copy
● 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
Copy
At the time of the writing of this article, the current version of Docker available for Debian 9 is 18.06.0-ce. To check the Docker version run:
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
Copy
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:
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.
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:
As you can see the search results prints a table with five columns, NAME, DESCRIPTION, STARS, OFFICIAL and AUTOMATED.
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.
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
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:
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.
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:
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 question, please leave a comment below.
No comments:
Post a Comment