Want to use Docker on Debian 12? Let me help you with that.
Docker is available to install from the Debian repositories. All you have to do is to run this command:
sudo apt install docker.io
However, you will not get the latest Docker version from Debian.
This is why I recommend installing it from the Docker repositories itself. This way, you get the latest Docker version on Debian along with any future updates directly from the source.
And to do so, you can use the following command:
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Once done, you can proceed to the shown installation methods.
Install Docker on Debian 12 using Docker repositories
The main reason why I recommend using this method is you get hassle-free upgrades as repositories can easily be updated!
So first, use the following command to install prerequisites for this method:
sudo apt update && sudo apt install ca-certificates curl gnupg
Now, let's create a directory to store the keyrings using the following:
sudo install -m 0755 -d /etc/apt/keyrings
Next, download the GPG key and store it in the /etc/apt/keyrings
directory using the given command:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Once done, use the chmod command to change the permissions of the docker.gpg
file:
sudo chmod a+r /etc/apt/keyrings/docker.gpg
And finally, use the following command to set up the repository for Docker:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
The extra \
at the end of each line in the above command is just a way to add new line so that you can easily see the entire command. That's it!
Now, you can update the repository index and install Docker using the following command:
sudo apt update && sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
To verify the Docker installation, you can install the hello-world image:
sudo docker run hello-world
The hello-world docker image is tiny and solely aimed to check if Docker is running fine.
Here's a replay of all the above commands.
Use Docker without sudo
If you noticed, while running a hello-world image, I used sudo
.
And this may not be convenient. So how about you configure it in such a way that you don't have to use sudo?
To do so, first, create a docker group using the groupadd command:
sudo groupadd docker
Now, add the user to the group (docker):
sudo usermod -aG docker $USER
Now log out from the terminal and log back in to take effect from the changes.
Let's put it to the test by running the hello-world image:
docker run hello-world
And as you can see, I was able to get the same results without using sudo.
Uninstall Docker
First, stop the docker service using the following:
sudo systemctl stop docker
Then use the apt purge command in the following manner to remove the Docker from your system:
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
If you're planning to do the fresh install or you don't want to have any previous data, then you can use the rm command to remove Docker files:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
That's it! Docker is removed successfully.
Next: Learn the essential Docker commands
If you're just starting with Docker, I highly recommend you learn the essential commands first.
If nothing else, at least learn the basics Docker commands to manage the containers.
I hope you will find this guide helpful.