How to Install and Use Docker on Debian 12

If you're new to Debian and want to get started with the basics of creating new user, firewall etc. You can check our Getting started guide.

Installing Docker

First, update your existing list of packages:

apt update

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

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@server:~# curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

Add the Docker repository to APT sources:

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:

apt update

Make sure you are about to install from the Docker repo instead of the default Debian repo:

apt-cache policy docker-ce

Notice that docker-ce is not installed.

Finally, install Docker:

apt install docker-ce

Output:

root@vps:~# apt install docker-ce
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  containerd.io docker-buildx-plugin docker-ce-cli docker-ce-rootless-extras docker-compose-plugin git git-man
  iptables liberror-perl libip6tc2 libltdl7 libnetfilter-conntrack3 libnfnetlink0 libslirp0 patch pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk
  gitweb git-cvs git-mediawiki git-svn firewalld ed diffutils-doc
The following NEW packages will be installed:

Docker is now installed, the daemon started, and the process enabled to start on boot.

And to check if thats active:

systemctl status docker

Output:

root@vps:~# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Fri 2023-06-16 11:21:19 UTC; 55s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 3607 (dockerd)
      Tasks: 9
     Memory: 30.3M
        CPU: 795ms
     CGroup: /system.slice/docker.service
             └─3607 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Executing the Docker Command Without Sudo

Add a new user in Debian

adduser user1 or useradd user1

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

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:

$ id -nG
user1 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:
app*        Docker App (Docker Inc., v0.9.1-beta3)
builder     Manage builds
buildx*     Build with BuildKit (Docker Inc., v0.6.1-docker)
config      Manage Docker configs
container   Manage containers
context     Manage contexts
image       Manage images
manifest    Manage Docker image manifests and manifest lists
network     Manage networks
node        Manage Swarm nodes
plugin      Manage plugins
scan*       Docker Scan (Docker Inc., v0.8.0)

To view system-wide information about Docker, use:

docker info

Output:

root@vps:~# docker info
Client: Docker Engine - Community
 Version:    24.0.2
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.5
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.18.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Working with Docker Images

To check whether you can access and downloaded images from Docker Hub, run the below command:

docker run hello-world

Output:

root@vps:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:c2e23624975516c7e27b1b25be3682a8c6c4c0cea011b791ce98aa423b5040a0
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

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 Debian image, type:

docker search debian

Output:

root@vps:~# docker search debian
NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                Ubuntu is a Debian-based Linux operating sys…   16067     [OK]
debian                                Debian is a Linux distribution that's compos…   4697      [OK]
neurodebian                           NeuroDebian provides neuroscience research s…   100       [OK]
bitnami/debian-base-buildpack         Debian base compilation image                   2                    [OK]
kasmweb/debian-bullseye-desktop       Debian Bullseye desktop for Kasm Workspaces     0
mirantis/debian-build-ubuntu-xenial                                                   0
rancher/debianconsole                                                                 1
datadog/debian-i386                                                                   0
mirantis/debian-build-ubuntu-trusty                                                   0

To download the official debian image to your computer:

docker pull debian

Output:

root@vps:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
bba7bb10d5ba: Pull complete
Digest: sha256:d568e251e460295a8743e9d5ef7de673c5a8f9027db11f4e666e96fb5bed708e
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest

To see the images that have been downloaded to your computer, run the below command:

docker images

Output:

root@vps:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
debian        latest    49081a1edb0b   3 days ago    116MB
hello-world   latest    9c7a54a9a43c   6 weeks ago   13.3kB

Running a Docker Container

Let's run a container using the latest image of Debian:

docker run -it debian

Output:

root@vps:~# docker run -it debian
root@e9a2653fdd35:/#

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

apt update

Output:

root@5a4a39b56450:/# apt update
Get:1 http://deb.debian.org/debian buster InRelease [122 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 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 Debian repository. When the installation finishes, verify that Node.js is installed:

node -v

Output:

root@e9a2653fdd35:/# node -v
v18.13.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@server:~$ 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:

root@vps:~# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                        PORTS     NAMES
e9a2653fdd35   debian        "bash"     2 minutes ago   Exited (127) 31 seconds ago             musing_meitner
b5ca09ab5eb8   hello-world   "/hello"   3 minutes ago   Exited (0) 3 minutes ago                pedantic_morse

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

docker ps -l

Output:

root@vps:~# docker ps -l
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                        PORTS     NAMES
e9a2653fdd35   debian    "bash"    2 minutes ago   Exited (127) 51 seconds ago             musing_meitner

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

docker start e9a2653fdd35

Output:

root@vps:~# docker start e9a2653fdd35

To view the latest container:

user1@server:~$ docker ps

Output:

root@vps:~# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS          PORTS     NAMES
e9a2653fdd35   debian    "bash"    3 minutes ago   Up 16 seconds             musing_meitner

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

docker stop hardcore_austin

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 hardcore_austin

CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS