How To Install and Use Docker on Debian 10

Prerequisites

If you're new to Debian and want to quickly get started with basic components such as creating a new sudo user and allowing SSH connection, Check out the following guide on Prerequisites for Debian 10

Installing Docker

First, update your existing list of packages:

sudo apt update

Next, install a few prerequisite packages which let apt use packages over HTTPS:

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

Then add the GPG key for the official Docker repository to your system:

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

Output:

root@vps:~# curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
OK

Add the Docker repository to APT sources:

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

Next, update the package database with the Docker packages from the newly added repo:

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

Output:

root@vps:~# apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 5:19.03.1~3-0~debian-buster
Version table:
 5:19.03.1~3-0~debian-buster 500

Notice that docker-ce is not installed

Finally, install Docker:

sudo apt install docker-ce

Output:

root@vps:~# ^C
root@vps:~# sudo apt install docker-ce
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:

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

sudo systemctl status docker

Output:

root@vps:~#     sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
Active: active (running) since Mon 2019-07-29 13:42:34 EDT; 1min 56s ago
 Docs: https://docs.docker.com
Main PID: 30098 (dockerd)
Tasks: 11
Memory: 44.2M

Executing the Docker Command Without Sudo

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

sudo usermod -aG docker user1

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

su - user1

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

id -nG

Output:

user1@vps:~$ id -nG
user1 sudo docker

Using the Docker Command

Using docker consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:

docker [option] [command] [arguments]

To view all available subcommands, type:

docker

Output:

 Commands:
attach      Attach local standard input, output, and error streams to a running container
build       Build an image from a Dockerfile
commit      Create a new image from a container's changes
cp          Copy files/folders between a container and the local filesystem
create      Create a new container
diff        Inspect changes to files or directories on a container's filesystem
events      Get real time events from the server
exec        Run a command in a running container
export      Export a container's filesystem as a tar archive
history     Show the history of an image
images      List images

To view system-wide information about Docker, use:

docker info

Output:

user1@vps:~$ docker info
Client:
Debug Mode: false
Server:
 Containers: 0

Working with Docker Images

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

docker run hello-world

Output:

user1@vps:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest

Docker was initially unable to find the hello-world image locally, so it downloaded the image from Docker Hub, which is the default repository.

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

Output:

user1@vps:~$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   9779                [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   326                                     [OK]

To download the official ubuntu image to your computer:

docker pull ubuntu

Output:

user1@vps:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7413c47ba209: Pull complete
0fe7e7cbb2e8: Pull complete
1d425c982345: Pull complete
344da5c95cec: Pull complete
Digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

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

docker images

Output:

user1@vps:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        6 days ago          64.2MB
hello-world         latest              fce289e99eb9        6 months ago        1.84kB

Running a Docker Container

Let's run a container using the latest image of Ubuntu

docker run -it ubuntu

Output:

user1@vps:~$ docker run -it ubuntu
root@a32dded1ad32:/# 

Now you can run any command inside the container. For example, let's update the package database inside the container

apt update

Output:

root@8d268a8e1228:/# apt update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]

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

root@8d268a8e1228:/# 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:

 node -v

Output:

root@8d268a8e1228:/# node -v
v8.10.0

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

Output:

user1@vps:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

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

docker ps -a

Output:

user1@vps:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
a32dded1ad32        ubuntu              "/bin/bash"         6 minutes ago       Exited (100) About a minute ago                       awesome_mendeleev
d1e84fe6007d        hello-world         "/hello"            16 minutes ago      Exited (0) 16 minutes ago                             cool_mcclintock
d8f17f420bf4        hello-world         "/hello"            17 minutes ago      Exited (0) 17 minutes ago                             quizzical_hamilton

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

docker ps -l

Output:

user1@vps:~$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
a32dded1ad32        ubuntu              "/bin/bash"         7 minutes ago       Exited (100) 3 minutes ago                       awesome_mendeleev

To start a stopped container, use docker start, followed by the container ID or the container's name

docker start a32dded1ad32

Output:

user1@vps:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a32dded1ad32        ubuntu              "/bin/bash"         10 minutes ago      Up 21 seconds                           awesome_mendeleev

To stop a running container, use docker stop, followed by the container ID or name.

docker stop awesome_mendeleev

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.

docker rm awesome_mendeleev