Posted in

Installing Docker on Debian

I assume, if you are reading this than you are already familiar with its power. It allows services to be “containerized”. Just like a real shipping container can carry anything — clothes, electronics, food — and still be loaded onto any ship, Docker lets you package up an app (like WordPress or Traefik, spoiler we’re working towards that) with everything it needs to run: the code, the server, the database, even the settings. No surprises. No “it worked on my machine” problems. One of the powerful parts of Docker is that if there is an issues with one container and it fails there is no effect on the other applications (containers) running on your server (cargo ship).

Before we start: SUDO!

Sudo (some say, ‘Superuser Do’) lets you run commands as the superuser (root), giving you permission to do things that regular users can’t, like installing software or changing system settings. It’s like being the assistant manager at a retail store, you have all the power but only if there is a need.

Sudo, is not generally available in a standard installation of Linux. Debian, anyway. Other distributions may, but Debian does not come with it installed in the base distribution. So let’s go over getting that functionality. First, make sure that you are logged in as the root user. That is to say, log into the system with the user name ‘root’ and the root password set during installation. If you are working with a VPS, that information may have been sent to you and the password should be changed so only you have the password.

Now we can move on to the good stuff. Everything in a ‘code block’ should be copied and pasted into you terminal on the Debian machine. First we will update the system and actually install the sudo application.

apt update && apt install sudo

Easy enough, right?

Well, now we need to create a user and add that user to the “sudoers” (assistant managers) group. At this point you should still be logged in as the root user. Remember, we don’t have sudo authority yet for any other user and root is the actual store manager. While logged in as root we will add a user. In this case you are going to call the user ‘docker’ but these commands will work to add other users and add them to the sudoer group, just change ‘docker’ to something else.

adduser docker

I won’t hold your hand through the prompts but just know that you you can “enter” your way through everything after giving the new user a password. You don’t actually have to give it any personal information.

Next let’s add the docker user, we just created, to the “sudo” group so they can get that sweet role as the assistant store manager. The magic happens by being a member of the “sudo” group.

usermod -aG sudo docker

You should now log out of root using the ‘logout’ command and log back in as the docker user.

Now you’re playing with power, SUDO POWER! You can use sudo privileges by appending ‘sudo’ to the front of any command. For example, try to see the contents of a file that is only for the root account, like say /etc/shadow. Note, ‘cat’ is a program that is commonly used to so the contents of a file inside of the shell.

cat /etc/shadow

You will see something like “cat: /etc/shadow: Permission denied”. That file is owned by root. Now let’s try that again but this time invoking the sudo application first.

sudo cat /etc/shadow

Now we can see the contents! You are acting on behalf on the root account for this command. Also, don’t ever share the contents of the file anywhere, for any reason. There is a reason that root owns it!

As a side note, just something to keep in mind, and I use a lot, ‘!!’ is shortcut in the shell for the last command entered. So for example, do the ‘cat /etc/shadow’ command again. You will again see that the permission is denied. This time type ‘sudo !!”, immediately after, and you will again see the contents displayed. In this example ‘sudo !!’ is seen by the shell as ‘sudo cat /etc/shadow’. If you want to sound extra geeky, it’s referred to as ‘bang, bang’ as in ‘sudo bang, bang’. Try not to say it twice when you say it out loud, I dare you. When you forget that you need ‘sudo’ privileges to run something ‘sudo !!’ is your friend!! !!

Docker Installation

For this example I will continue with Debian 12.

Add Docker’s official GPG key:

sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Add the repository to Apt sources:

***Note: The entire echo command below, straight down to /dev/null is a single command so don’t split it up.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Finally, Install all of the Docker packages

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Let’s give it a test!

sudo docker run hello-world

You should see something like this:

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/

Success!! Docker is installed and you can launch a container, in this case it’s the “hello-world” image. It does nothing but say hello and tell you that you’re good to go.

You might not know it yet but this has just opened up a whole world of services you can easily tap into.

Leave a Reply

Your email address will not be published. Required fields are marked *