How to Install and Use Docker on Debian 11
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
Output:
root@server:~# apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 5:20.10.8~3-0~debian-bullseye
rsion table: 5:20.10.8~3-0~debian-bullseye 500
500 https://download.docker.com/linux/debian bullseye/stable amd64 Packages
5:20.10.7~3-0~debian-bullseye 500
500 https://download.docker.com/linux/debian bullseye/stable amd64 Packages
5:20.10.6~3-0~debian-bullseye 500
500 https://download.docker.com/linux/debian bullseye/stable amd64 Packages
Notice that docker-ce
is not installed.
Finally, install Docker:
apt install docker-ce
Output:
root@server:~# 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-ce-cli docker-ce-rootless-extras
docker-scan-plugin git git-man liberror-perl libltdl7 libslirp0
patch pigz slirp4netns
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@server:~#
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-08-04 15:02:12 GMT; 1min 2s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 76508 (dockerd)
Tasks: 12
Memory: 36.3M
CPU: 566ms
CGroup: /system.slice/docker.service
└─76508 /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@server:~# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
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@server:~#docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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 Debian image, type:
docker search debian
Output:
root@server:~# docker search debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 12586 [OK]
debian Debian is a Linux distribution that's compos… 3934 [OK]
arm32v7/debian Debian is a Linux distribution that's compos… 72
itscaro/debian-ssh debian:jessie 28 [OK]
To download the official debian image to your computer:
docker pull debian
Output:
root@server:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
Digest: sha256:cc58a29c333ee594f7624d968123429b26916face46169304f07580644dde6b2
Status: Image is up to date 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@server:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian latest 0980b84bde89 13 days ago 114MB
hello-world latest d1165f221234 5 months ago 13.3kB
Running a Docker Container
Let's run a container using the latest image of Debian:
docker run -it debian
Output:
root@server:~# docker run -it debian
root@5a4a39b56450:/#
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@5a4a39b56450:/# node -v
v10.24.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:
user1@server:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a4a39b56450 debian "bash" 4 minutes ago Exited (0) 2 minutes ago hardcore_austin
00f27dc84bf6 hello-world "/hello" 18 minutes ago Exited (0) 18 minutes ago relaxed_euclid
To view the latest container you created, pass it the -l switch:
docker ps -l
Output:
user1@server:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a4a39b56450 debian "bash" 5 minutes ago Exited (0) 2 minutes ago hardcore_austin
To start a stopped container, use docker start, followed by the container ID or the container's name
docker start 5a4a39b56450
Output:
user1@server:~$ docker start 5a4a39b56450
5a4a39b56450
To view the latest container:
user1@server:~$ docker ps
Output:
user1@server:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a4a39b56450 debian "bash" 8 minutes ago Up 2 minutes hardcore_austin
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