How to Install Docker in Linux

A photograph of a computer monitor screen showing a Docker Compose file.

Docker is a powerful containerization platform that allows anyone to deploy and release complex programs just like regular apps. This makes it attractive to users who want to run online services but don’t want to deal with the headache of managing dependencies and config files. This article will show you how to install Docker and Docker Compose on some of the most popular Linux distros today.

Why Use Docker to Deploy Services in Linux

Container platforms such as Docker are a popular way to install services on your Linux machine. They allow you to easily isolate complex software into portable units which, in turn, improves the security of your server.

A terminal showing the different Docker containers running to maintain Shlink.

One powerful feature of Docker is that once a container works in one distro, making it work in others is a relatively straightforward process. This is because Docker abstracts the differences between these Linux distros making it compatible with little modifications on the container.

Docker also simplifies service management compared to non-containerized deployments. Originally, when something breaks in the system, you’ll have to reinstall and reconfigure every service on that machine. With Docker, you just need to copy the config files to another machine, start the service and you can immediately continue where you left off.

Install Docker on Ubuntu

Start by checking the key fingerprint of the official Docker project’s public key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --show-keys --with-fingerprint

At the moment, the fingerprint for the Docker project’s signing key is: 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88.

A terminal highlighting the Docker CE signing key fingerprint.

That said, this key may change in the future. Check Docker’s official website to see if they match. It’s important to cross-reference this to ensure that the software you’re installing is legitimate.

Once you confirm the validity of the signing key, download and save it to your “/etc/apt/keyrings” folder:

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

Ensure that the permission bits of the keyfile is correct:

sudo chmod a+r /etc/apt/keyrings/docker.gpg

Create a new repository file for the Docker project:

sudo nano /etc/apt/sources.list.d/docker.list

Paste the following line of code inside your new repository file:

deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu noble stable

Save your new repository file, then update your system’s package repositories:

sudo apt update && sudo apt upgrade

Install the core Docker packages along with the Docker Compose plugin. These will allow you to deploy Docker applications and orchestrate them properly:

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

Most docker commands need to be prefixed with sudo. If you want to avoid having to type your password every time, add your user account to the “docker” group:

sudo adduser YOUR-CURRENT-USERNAME docker

Log out of your graphical user interface and log back in. Now you can use commands such as docker ps instead of sudo docker ps.

Tip: you can also disable password verification in sudo by tweaking the sudoers file.

Install Docker on Debian

Since Debian is the upstream Linux distro for Ubuntu, it also uses apt for managing its packages and repositories. This means that the steps are almost similar to Ubuntu barring a handful of Debian-specific GPG keys and repository links.

To start, fetch the Docker repository’s signing key from the Docker project:

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

Note: Just like with Ubuntu, you also should confirm the GPG fingerprint of the Debian Docker repository. At the moment, it’s: 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88.

Create the apt repository file for the Docker project:

sudo nano /etc/apt/sources.list.d/docker.list

Paste the following line of code inside the repository file:

deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable

Refresh the machine’s repository listings and update your Debian system:

sudo apt update && sudo apt upgrade

Fetch and install the Docker core packages and the Docker Compose plugin:

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

Add your user account to the “docker” group:

sudo usermod -aG docker YOUR-USERNAME

Restart your Debian machine to apply your new settings, then run the following command to test if Docker install is working properly:

docker -v
A terminal showing the latest version of Docker available in the repository.

Install Docker on Fedora

To install Docker and Docker Compose on Fedora, first obtain the management program for dnf:

sudo dnf install dnf-plugins-core

Fetch the repository details for Docker and commit it to your dnf installation:

sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Install the Docker, Docker Compose, and their dependencies:

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

Confirm that the fingerprint for the Docker repository is: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35. Type “y”, then press Enter to continue the installation process:

A terminal highlighting the key verfication prompt in Fedora.

Enable and start the Docker systemd service:

sudo systemctl enable --now docker.service

Make sure that your current user account is in the “docker” user group:

sudo usermod -aG docker YOUR-USERNAME

Log out and log back in to your graphical user interface, then test your Docker installation by running the “Hello, world!” container:

docker run hello-world
A terminal showing the hello-world container in Fedora Linux.

Good to know: learn how Docker containers simplify application deployment by self-hosting your own bit.ly service with Shlink.

Install Docker on Red Hat Enterprise Linux

Red Hat Enterprise Linux (RHEL) is a stable, long-term support downstream distro of Fedora. Unlike its upstream, RHEL provides a consistent and rock-solid environment where you can reliably run your applications. This makes it a great system for deploying programs that you want to run with little to no interruptions.

Start by fetching the repository file for the Docker project:

sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo

Obtain and install Docker, Docker Compose, and their dependencies:

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

You can also specify the version of Docker that you want to run on your system. To do this, list the available versions of Docker for your machine:

dnf list docker-ce --showduplicates

Scroll through the list of available versions, then copy the version number that you want to install.

A terminal highlighting the different versions of Docker available in RHEL.

Paste the following command to your terminal, then replace the value of the version variable with your version number:

version="YOUR-VERSION-NUMBER-HERE"

Install the specific version of Docker in your system:

sudo dnf install docker-ce-3:$version docker-ce-cli-1:$version containerd.io docker-buildx-plugin docker-compose-plugin

Enable and run the Docker service using systemctl:

sudo systemctl enable --now docker.service

Add your current user to the docker user group:

sudo useradd -aG docker YOUR-USERNAME

Test if Docker is working properly by running docker --version to list its version number.

A terminal showing the downgraded version of Docker running in RHEL.

Install Docker on Arch Linux

Arch Linux already includes Docker and Docker Compose in its community repositories. This makes installing both as simple as running pacman:

sudo pacman -S docker docker-compose containerd docker-buildx
A terminal showing the installation prompt for Docker in Arch Linux.

Add the current user account to the Docker group:

sudo usermod -aG docker YOUR-USERNAME

Reboot your machine to reload your system and start the Docker daemon.

Enable Docker service to automatically start at boot and run it in the current session:

sudo systemctl enable --now docker.service

Test if Docker is working properly by running the “Hello, world” container:

docker run hello-world
A terminal showing the hello-world Docker container running in Arch Linux.

Deploying Your First Docker Container

With Docker up and running on your machine, you can now use the platform to obtain various applications and services on your system. The quickest way to start using Docker is to look for prebuilt containers for popular applications.

Note: While Docker uses the same commands to build and run different apps, every container has its unique requirements for them to work. As such, you should always consult with the container’s documentation and an appropriate guide before pulling a Docker image.

That said, start by going to Docker Hub and search for an app that you want to install. For this, I will use the Nginx Docker container since I want to serve a basic static website.

A screenshot of the Docker image page for Nginx.

Go back to your terminal session, then create a directory for your Docker container:

mkdir ./docker-nginx && cd ./docker-nginx

Copy your static website inside the Docker directory:

cp -r ~/html-site ./docker-nginx

Create a Dockerfile inside your new directory using your favorite text editor:

nano ./Dockerfile

Paste the following lines of code inside your Dockerfile. This will load the Nginx image and copy the static website from my “html-site” folder to the “html” folder inside the container.

FROM nginx
COPY html-site /usr/share/nginx/html

Save your Dockerfile, then build the Nginx Docker container:

docker build -t static-nginx .

Run the newly built Docker container using the run subcommand:

docker run --name my-nginx-website --publish 8080:80 -d static-nginx

Using Docker Compose to Run Your Container

Aside from running directly in the command line, you can also use Docker Compose to start your container. This is a way to create reproducible Docker setups which can be helpful in more complex deployments.

Create a “docker-compose.yml” file inside your Docker directory:

nano docker-compose.yml

Paste the following block of code inside your Compose file:

services:
  nginx:
    container_name: my-nginx-website
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:80

Save your “docker-compose.yml” file, then start your Docker container:

docker compose up -d

Test if your new website is working properly by visiting “localhost:8080” using your web browser.

A screenshot showing a basic website running on a Dockerized Nginx instance.

Learning the basics of Docker, installing it to your Linux system, and running a basic Dockerized web server are just some of what you can do with this wonderful container platform. Explore the deep world of self-hosting web services with Docker by deploying your own online RSS reader with Tiny Tiny RSS.

Image credit: Mohammad Rahmani via Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.