Table of Contents
In this article, I will take you through the steps to Install and use Docker on Debian 11. In today's digital world, Docker has completely transformed the way modern applications are deployed and created. It has now become a key component in the growth of an IT Industry and also became an important ingredient in the development of container based technologies. In this fast emerging World, there are lots and lots of application are getting created everyday but it is almost impossible for the Global Industries to have that much resources to deploy at that scale. This is simply because of the resource crunch.
So to deploy applications in a fast and much more efficient way, it is absolutely necessary to use container technology provided by Docker. Now a days, developers and programmers always prefers to use docker containers to deploy their applications and services rather than deploying it into some legacy systems. We will see some more cool features about docker in below section. So Let's get Started !!
Docker was created by a company called Docker. It comes in two segments - one is the Community Edition(CE) which is closed source yet completely free and then there is Enterprise Edition(EE) which is also closed-source but needs to be licensed on yearly basis. We will see the steps to install and use Docker Community Edition in below section.
How to Install and Use Docker on Debian 11
Also Read: How to Install and Use MongoDB on RHEL/CentOS 7/8 [Easy Steps]
Step 1: Uninstall Older Version
First of all if you have any older version of docker or its tools then you need to remove them completely by using apt-get remove docker docker-engine docker.io containerd runc
command as shown below.
NOTE:
root
user to run all the below commands. You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.root@localhost:~# apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package docker-engine
Step 2: Install Prerequisites
In the next step, you need to install all the prerequisites which are required to install and use docker engine.
root@localhost:~# apt-get install ca-certificates curl gnupg lsb-release -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20210119).
gnupg is already the newest version (2.2.27-2).
gnupg set to manually installed.
lsb-release is already the newest version (11.1.0).
lsb-release set to manually installed.
The following packages were automatically installed and are no longer required:
libaribb24-0 libcddb2 libdouble-conversion3 libdvbpsi10 libebml5 libixml10 liblzo2-2 libmad0 libmatroska7 libmd4c0 libopenmpt-modplug1 libpcre2-16-0
libplacebo72 libprotobuf-lite23 libproxy-tools libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libqt5x11extras5
libresid-builder0c2a libsdl-image1.2 libsdl1.2debian libsidplay2 libspatialaudio0 libupnp13 libva-wayland2 libvlc-bin libvlc5 libvlccore9 libvncclient1
libxcb-composite0 libxcb-xinerama0 libxcb-xinput0 qt5-gtk-platformtheme qttranslations5-l10n vlc-bin vlc-data vlc-l10n vlc-plugin-access-extra
vlc-plugin-base vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter
vlc-plugin-visualization
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
curl
0 upgraded, 1 newly installed, 0 to remove and 94 not upgraded.
Need to get 267 kB of archives.
Step 3: Add GPG Key
You need to add the Docker Official's GPG Key in the Keyrings before setting up the repository for downloading and installing docker packages. This will ensure the validity of downloaded docker packages.
root@localhost:~# curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Add Stable Repository
Now you need to add the stable repository using below command to download the latest version of docker packages. Since we do not have any direct utility like add-apt-repository
on RHEL/CentOS to add repository information what we have in Ubuntu/Debian based Systems, so we need to rely on utilities like tee
to setup the repository.
root@localhost:~# echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 5: Update Package Cache
After adding repository information, you need to update the packages cache with all the package information from the newly added repository using apt-get update
command as shown below.
root@localhost:~# apt-get update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Hit:2 http://security.debian.org/debian-security bullseye-security InRelease
Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
Get:4 https://download.docker.com/linux/debian bullseye InRelease [43.3 kB]
Get:5 https://download.docker.com/linux/debian bullseye/stable amd64 Packages [6,092 B]
Fetched 49.4 kB in 1s (44.1 kB/s)
Reading package lists... Done
Step 6: Install Docker Engine
In the next step, you need to download and install the latest version of docker engine and containerd using apt-get install docker-ce docker-ce-cli containerd.io -y
command as shown below.
root@localhost:~# apt-get install docker-ce docker-ce-cli containerd.io -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
libaribb24-0 libcddb2 libdouble-conversion3 libdvbpsi10 libebml5 libixml10 liblzo2-2 libmad0 libmatroska7 libmd4c0 libopenmpt-modplug1 libpcre2-16-0
libplacebo72 libprotobuf-lite23 libproxy-tools libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libqt5x11extras5
libresid-builder0c2a libsdl-image1.2 libsdl1.2debian libsidplay2 libspatialaudio0 libupnp13 libva-wayland2 libvlc-bin libvlc5 libvlccore9 libvncclient1
libxcb-composite0 libxcb-xinerama0 libxcb-xinput0 qt5-gtk-platformtheme qttranslations5-l10n vlc-bin vlc-data vlc-l10n vlc-plugin-access-extra
vlc-plugin-base vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter
vlc-plugin-visualization
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
docker-ce-rootless-extras docker-scan-plugin git git-man liberror-perl libslirp0 pigz slirp4netns
Step 7: Check Docker Version
After successful installation, you can check the installed version by using docker --version
command. As you can see from output, it is 20.10.10
.
root@localhost:~# docker --version
Docker version 20.10.10, build b485636
Step 8: Check Docker Service
By default, Docker service will start automatically after installation but you can always check the status using systemctl status docker
command. If needed, you can also start the service by using systemctl start docker
command or restart the service by using systemctl restart docker
command.
root@localhost:~# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-11-09 19:32:18 PST; 36min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 2195 (dockerd)
Tasks: 8
Memory: 38.8M
CPU: 566ms
CGroup: /system.slice/docker.service
└─2195 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Step 9: Run Hello World Image
You can now use the docker engine to create your first container from hello-world image. This can be done by running hello-world
image using docker run hello-world
command. You can also check the steps docker has taken to create the container on the below shown output.
root@localhost:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Step 10: List Docker Images
You can list all the available images using docker images
command. Please do not confuse this command with docker image
command as both serve different purposes. If you are looking to manage your docker images then you need to use docker image <command>
. You can check all the options available with this command using docker image --help
root@localhost:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
Step 11: Run a Ubuntu Container
If you want to run a Ubuntu Container then you need to use docker run -it ubuntu bash
command as shown below. If you don't specify any tag with the image then it will by default look for ubuntu:latest
image. If it is not available in your local system, then it will get downloaded from Ubuntu Library and then container will be created using that image.
root@localhost:~# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
root@b1507b4cac2c:/#
Step 12: List Docker Containers
You can also list all the containers using docker ps -a
command. This command will list all the containers irrespective of their state.
root@localhost:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1507b4cac2c ubuntu "bash" 38 minutes ago Exited (127) 4 seconds ago zen_goldstine
2f03f8f77164 hello-world "/hello" 40 minutes ago Exited (0) 40 minutes ago nervous_volhard
f048d264d90a hello-world "/hello" 43 minutes ago Exited (0) 43 minutes ago eager_kalam
Step 13: Manage Docker Containers
If you want to start a container then you need to use docker start <container_id>
syntax. Here we are starting container with ID b1507b4cac2c
using docker start b1507b4cac2c
command.
root@localhost:~# docker start b1507b4cac2c
b1507b4cac2c
Then, check the status of Container using docker ps
command.
root@localhost:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1507b4cac2c ubuntu "bash" 43 minutes ago Up 1 second zen_goldstine
Similarly, if you want to stop a container then you need to use docker stop <container_id>
syntax. To stop Container ID b1507b4cac2c
, you need to use docker stop b1507b4cac2c
command as shown below.
root@localhost:~# docker stop b1507b4cac2c
b1507b4cac2c
Again, check the status using docker ps
command as shown below. You may notice that container with ID b1507b4cac2c
is not UP anymore.
root@localhost:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You can also delete the image using docker rm <container_id>
syntax. Here we are removing container with ID b1507b4cac2c
using docker rm b1507b4cac2c
command.
root@localhost:~# docker rm b1507b4cac2c
b1507b4cac2c
Now check the list of images using docker images command as shown below. You may notice that container with ID b1507b4cac2c
is no more available.
root@localhost:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 3 weeks ago 72.8MB
hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
Step 14: Push Images to Docker Hub Repository
If you want to store docker images to a repository like Docker Hub Repository then you need to first Login to that Repository using your user account credentials. In case you don't have an account, then you need to Sign Up here first.
root@localhost:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: cyberithub
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
After login successfully, you can now push your Images using docker push <Image_name>
syntax. Here we are pushing ubuntu:latest
image using docker push cyberithub/ubuntu:latest
command as shown below. It is important to note here that you might need to tag the image before you can push it to Docker Hub Repository. If you don't know how to tag an image then you might want to check 40 Ultimate docker commands with examples article once.
root@localhost:~# docker push cyberithub/ubuntu:latest
The push refers to repository [docker.io/cyberithub/ubuntu]
9f54eef41275: Mounted from library/ubuntu
latest: digest: sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 size: 529
Step 15: Remove Docker
Once you are done using docker, then you can remove it by using apt-get remove docker-ce docker-ce-cli containerd.io -y
command as shown below.
root@localhost:~# apt-get remove docker-ce docker-ce-cli containerd.io -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
docker-ce-rootless-extras docker-scan-plugin libaribb24-0 libcddb2 libdouble-conversion3 libdvbpsi10 libebml5 libixml10 liblzo2-2 libmad0 libmatroska7
libmd4c0 libopenmpt-modplug1 libpcre2-16-0 libplacebo72 libprotobuf-lite23 libproxy-tools libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5
libqt5widgets5 libqt5x11extras5 libresid-builder0c2a libsdl-image1.2 libsdl1.2debian libsidplay2 libslirp0 libspatialaudio0 libupnp13 libva-wayland2
libvlc-bin libvlc5 libvlccore9 libvncclient1 libxcb-composite0 libxcb-xinerama0 libxcb-xinput0 qt5-gtk-platformtheme qttranslations5-l10n slirp4netns
vlc-bin vlc-data vlc-l10n vlc-plugin-access-extra vlc-plugin-base vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2
vlc-plugin-video-output vlc-plugin-video-splitter vlc-plugin-visualization
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
containerd.io docker-ce docker-ce-cli
0 upgraded, 0 newly installed, 3 to remove and 94 not upgraded.
After this operation, 368 MB disk space will be freed.
(Reading database ... 144194 files and directories currently installed.)
Removing docker-ce (5:20.10.10~3-0~debian-bullseye) ...
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
Removing containerd.io (1.4.11-1) ...
Removing docker-ce-cli (5:20.10.10~3-0~debian-bullseye) ...
Processing triggers for man-db (2.9.4-2) ...