Share your love
How To Install and Use Docker on Debian 11
In this article, we want to teach you How to install and use Docker on Debian 11.
Docker is an open-source platform that is set up based on a Linux operation system. It is a tool that can ease the creation, implementation, and performance processes with the containers.
To learn more details about Docker you can check our article about What is Docker and how does it work.
How To Install and Use Docker on Debian 11
Before you start to install and use Docker on Debian 11, you need some requirements.
You need to log in to your server as a non-root user with sudo privileges. to do this you can check this article about the Initial server setup with Debian 11.
Install Docker on Debian 11
By default, the Docker package is available in the official Debian repository. but first, you need to update and upgrade the APT packages to get the latest version of Docker.
Run the following command to update and upgrade the packages:
sudo apt update && apt upgrade
Here you need to install a few requirement packages to let APT use packages over HTTPS with the following command:
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common
Now add the GPG key for the official Docker repository to your system with the following command:
sudo curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
Then, you need to add the Docker repository to the APT sources with the command below:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
Update the packages:
sudo apt update
Here you can check the candidate of the Docker installation for Debian 11 (bullseye):
apt-cache policy docker-ce
In your output, you will see something similar to this:
Output docker-ce: Installed: (none) Candidate: 5:20.10.8~3-0~debian-bullseye Version 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
At this point, you can install Docker on Debian 11 with the following command:
sudo apt install docker-ce
Check that your service is active and running with the following command:
sudo systemctl status docker
In your output you should see:
Output docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset> Active: active (running) since Sat 2021-09-25 03:45:49 EDT; 1min 22s ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 3032 (dockerd) Tasks: 8 Memory: 35.0M CPU: 488ms CGroup: /system.slice/docker.service └─3032 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont
Now the installation of Docker is finished and the service is active and running. also, you have the docker command-line utility. in the rest of the article, we will show you how to use the docker command-line utility on Debian 11.
Execute the docker command without sudo
The docker command can only be run by the root user or the user in the docker group. docker group is created in the installation process by default.
If you don’t want to use sudo for the docker command you need to add your user to the docker group with the following command:
sudo usermod -aG docker ${USER}
To apply this change, run the following command:
su - ${USER}
Here you will be asked to enter your username’s password to continue.
you can verify that your user added to the docker group with the following command:
id -nG
In the rest of the article, we run the docker commands as a user in the docker group. if you don’t want this remember to run commands with a user with sudo privileges.
After the installation of Docker on Debian 11 is finished, Let’s see how the docker command works.
How to Use the Docker command
The syntax of the docker command is like this form:
docker [option] [command] [arguments]
You can see all available subcommands of docker with the following command:
docker
In your output you will see:
Output
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
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Also, you can run ‘docker COMMAND –help’ for more information on a command.
Here we want to teach you how to work with Docker images on Debian 11.
How to work with Docker images
The Docker Image is a portable file that contains a set of instructions that specify which software components the Container should run and how to run it.
By default, Docker pulls these images from Docker Hub. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need will have images hosted there.
You can check that you have access and download images from Docker Hub with the following command:
docker run hello-world
Your output should look like this:
Output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
This will show that Docker is unable to find the image first, so it downloaded it from the Docker Hub.
Also, you can search for the available images on the Docker hub with the search subcommand.
For example, search for the Debian image with the following command:
docker search debian
In your output, you will see a listing of images that you have searched.
Output
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 12841 [OK]
debian Debian is a Linux distribution that's compos… 4011 [OK]
arm32v7/debian Debian is a Linux distribution that's compos… 73
itscaro/debian-ssh debian:jessie 28 [OK]
...
Here you can download the official Debian image with the following command:
docker pull debian
Your output should similar to this:
Output
Using default tag: latest
latest: Pulling from library/debian
955615a668ce: Pull complete
Digest: sha256:08db48d59c0a91afb802ebafc921be3154e200c452e4d0b19634b426b03e0e25
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest
To see what images you have been downloaded to your system run the following command:
docker images
In your output, you will see something similar to this:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 33 hours ago 13.3kB
debian latest 82bd5ee7b1c5 3 weeks ago 124MB
At this point, you learn to use images to run containers that can be modified and used to generate new images.
Let’s see how to run docker containers on Debian 11.
How to Run a Docker container
Containers can be the best replacement for virtual machines. containers separate the executive environments and share the operation system’s core.
For example, run the container using the latest image of Debian. to do this run the following command:
docker run -it debian
Note: -it switches and gives you interactive shell access into the container.
You would see this form in your output:
Output
root@60cb8dc04a51:/#
Important note: remember the container ID. here the container ID is 60cb8dc04a51.
Now you can run any command in your container without sudo. because you execute commands in the container as a root user.
Update the packages inside the container with the following command:
root@60cb8dc04a51:/# apt update
Now you can install any application that you want. for example, we install Apache:
root@60cb8dc04a51:/# apt install apache2
When the installation of Apache is finished, verify that Apache is installed with the following command:
root@60cb8dc04a51:/# apache2 -v
Output
Server version: Apache/2.4.48 (Debian)
Server built: 2021-08-12T11:51:47
Note: Any changes you make inside the container only apply to that container.
To exit the container type exit:
root@60cb8dc04a51:/# exit
How to manage Docker containers
After you use Docker, you will have many active and inactive containers in your system. you can easily manage Docker containers on Debian 11.
To view active containers run the following command:
docker ps
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You have started two containers in this article. but they are not active.
To see all containers both active and inactive run the following command:
docker ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60cb8dc04a51 debian "bash" 22 minutes ago Exited(130) 6 minutes ago magical_mestorf
c66faf2131d0 hello-world "/hello" About an hour ago Exited(0) About an hour ago nice_lalande
Also, you can see the latest container that you have created with the following command:
docker ps -l
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60cb8dc04a51 debian "bash" 32 minutes ago Exited (130) 15 minutes ago magical_mestorf
You can start and stop a container with the container ID or the container’s name.
Here we start the Debian-based container with the ID:
docker start 60cb8dc04a51
Now you can check the status to see if your container is active:
docker ps
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60cb8dc04a51 debian "bash" 40 minutes ago Up About a minute magical_mestorf
Here you can stop the container. now we use the container’s name to stop it:
docker stop magical_mestorf
Also, you can remove a container with the container ID or the container’s name. for example remove the hello-world container with its name by the following command:
docker rm nice_lalande
Containers can be turned into images which you can use to build new containers.
Let’s see how it works.
How to Commit Changes in a Container to a Docker Image
After installing Apache inside the Debian container, you now have a container running off an image, but the container is different from the image you used to create it. But you might want to use this Apache container again as the basis for new images later.
You can commit the changes to a new docker image with the following command:
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
For example:
docker commit -m "install Apache" -a "olivia" 60cb8dc04a51 olivia/debian-apache
Now your new image is saved on your system.
List the docker images with the following command:
docker images
In your output you will see the new image:
Output REPOSITORY TAG IMAGE ID CREATED SIZE olivia/debian-apache latest c838b679f597 About a minute ago 252MB hello-world latest feb5d9fea6a5 35 hours ago 13.3kB debian latest 82bd5ee7b1c5 3 weeks ago 124MB
Note: The size difference reflects the changes that were made.
At this point, you can share the new image with others so they can create containers from it.
How to push Docker images to a Docker repository
After you create a new image from an existing image you can share it with others on Docker Hub or other Docker repositories that you have access to.
First, you need to create an account on Docker Hub.
Then log in to your Docker Hub to push your image with the following command:
docker login -u docker-registry-username
You will be asked to enter your Docker Hub password.
Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username.
For example:
docker tag olivia/debian-apache docker-registry-username/debian-apache
Then push your own image with the following command:
docker push docker-registry-username/docker-image-name
The process may take some time to complete as it uploads the images.
After pushing an image to a registry, it should be listed on your account’s dashboard.
Note: If a push attempt results in an error, Log in again and repeat the push attempt. Then verify that it exists on your Docker Hub repository page.
Conclusion
At this point, you learn how to install the Docker and work with the docker images and containers. and you can also push the docker images to a docker repository.
Hope you enjoy this article about How to install and use Docker on Debian 11.