I will not be installing Docker Desktop (UI) because it is not relevant to my needs and/or system.

Why write this

I understand there are probably thousands of these tutorials, but this is mainly for two people: myself and beginners.

I have installed Docker several hundred times over the last 7+ years and I never remember the commands, nor do I care to. However, I always know there are certain steps I need to take before/after installing.

🚨
Check the Docker website for the most up to date!
I appreciate you following my tutorial, and I will try to keep this up to date, but always double check for any recent updates on the Official Docker Installation Guide

Installing Docker for Ubuntu

OS requirements

To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:

  • Ubuntu Kinetic 22.10
  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Focal 20.04 (LTS) <– My Current OS
  • Ubuntu Bionic 18.04 (LTS)

Docker Engine is compatible with x86_64 (or amd64), armhf, arm64, and s390x architectures. If there is interest, I can install Docker on a NVIDIA Jetson for ARM processor users and post instructions.


Add the Docker repository

Before you install Docker Engine for the first time on a new machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository using the usual apt commands. So if you have never installed Docker before on your machine or you literally took the machine out of the box five minutes ago, follow these steps.

Step 1.

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update

sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Step 2.

Add Docker's GPG key:

sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 3.

Setup the Docker repository:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install Docker Engine

Step 1.

Do not forget this step, update the apt package index:

sudo apt-get update

Step 2.

Time to install Docker Engine, containerd, and Docker Compose!

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

Step 3.

Verify that the Docker Engine installation is successful by running the hello-world image:

sudo docker run hello-world

Running this command in your terminal should look something like this:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
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/
Output of running the command sudo docker run hello-world.

Post Installation

You may have noticed that we had to run the docker run command with sudo. This is because Docker runs as root.

Production Users: If you are setting a server up for production use, then you most likely do not need to follow these post-install instructions since it will open you up to vulnerabilities. For instance, if you add your user to the docker group, you technically have sudo access. You can launch an ubuntu container and mount whatever you want inside the container and go to town. If you want more info, head over the the Docker post-install page.

Local Users: If this is a personal machine, I highly recommend going through these instructions since it will save you from running sudo every time for docker commands.

Step 1.

Create the docker group

sudo groupadd docker

Step 2.

Add your user to the docker group

sudo usermod -aG docker $USER
đź’ˇ
Bash Tips
The $ here refers to the environment variable USER and you can print what that variable is by running echo $USER.

Step 3.

Log out and log back in and you should be able to run docker commands without sudo. Try these one:

# Check what images you downloaded on your computer
docker images

# See what's running
docker ps

🚀 Bonus Tips

Housekeeping

Many times we end up downloading a bunch of docker images and forgetting to cleanup unused images and data. Here are some helpful commands for cleaning up images and data:

# Cleanup dangling images
docker image prune

# Remove all unused images, not just dangling ones (-a, --all)
docker image prune -a

# Remove all unused local volumes
docker volume prune

# Remove unused data safely
docker system prune

Fresh Ubuntu Container

Sometimes I want to install and try random packages in an isolated environment that doesn't mess with my current OS. And sometimes, I need to install and test linux packages on my Mac. Enter the Ubuntu container! You can spin up a base Ubuntu (any flavor 18.04, 20.04, 22.04, etc.) container and do whatever you want in there.

# Pull the image you want, for me Ubuntu 22.04
docker pull ubuntu:22.04


# Get the image name
docker images

REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
ubuntu          22.04     6b7dfa7e8fdb   5 weeks ago    77.8MB


# Run the image, you will be placed into the container in root /
docker run -it --name ubuntu:22.04

# Run linux commands as usual
apt update 

# Install vim, the undisputed greatest and most powerful text editor
apt install vim

# Install FFMPEG to process media files like videos
apt install ffmpeg

I can even create a user.

root@fbcbaebf02f1:/# adduser amil
Adding user `amil' ...
Adding new group `amil' (1000) ...
Adding new user `amil' (1000) with group `amil' ...
Creating home directory `/home/amil' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for amil
Enter the new value, or press ENTER for the default
	Full Name []: Amil
	Room Number []: 69
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] 

root@fbcbaebf02f1:/# ls home/
amil

There are tons of prebuilt containers that you can use to tryout packages instead of installing them on your system. I highly recommend you try them out.

I'm done using the container. How do I delete it? Remember that once you stop this container, everything you did inside the container—apt, pip, yum installs—is deleted as well.

# Since we used the --name flag in the run command, we can use it here
docker stop ubuntu

# If you didn't use the --name
docker ps
docker stop [CONTAINER ID]

# Example
docker stop 45b4d7047db9

Check status of Docker

Sometimes we might have to check the status of the Docker service for any errors. The easiest way that I usually check is by running the following:

sudo systemctl status docker.service

If I need to restart the Docker Service, then I can run:

sudo systemctl restart docker.service
Share this post