How to Install Docker in Linux

Virtualization and containers are continually rising in popularity. The reasons why they’re useful mostly revolve around the isolation, security and portability benefits they provide.

For example, if you’re offering some form of cloud services, they make it easier to keep customer data and services isolated from each other. But it’s not limited to these scenarios. If you are a developer of a Linux application, you will quickly realize how hard it is to make your app available to all Linux distributions.

Once you make your program work in Ubuntu, making it also function in Arch Linux, Debian and other distros won’t be as straightforward as you might think. Every distribution is different, and you have to make adaptations so that your utility functions in every one of them.

docker-shipping-containers

Containerization makes it easier to “develop once, deploy everywhere.” That’s because instead of relying on the operating system to provide what your application needs, all dependencies are bundled in the container itself. Once the container works, you can move it around from computer to computer and between different operating systems easily.

Docker is a containerization utility that became very popular for simplifying such tasks. Furthermore, when something goes wrong with the operating system, instead of having to reinstall and reconfigure the application, you just reinstall the operating system, copy the container back and continue where you left off.

Cleanup Old Docker Installations

This is an optional step, required only if you have older Docker versions already installed. These might conflict with newer versions, especially if they come from different repositories than what you’ll use here.

On Debian or Ubuntu, clean up with:

sudo apt --purge autoremove docker docker-engine docker.io containerd runc

On Fedora:

sudo dnf remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine

How to Install Docker on Ubuntu

Open a terminal emulator and install prerequisites.

sudo apt update && sudo apt install software-properties-common gnupg2 curl ca-certificates apt-transport-https

Check Docker’s GPG key fingerprint.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg

docker-gpg-fingerprint

At the time of writing this tutorial, the fingerprint was 9DC858229FC7DD38854AE2D88D81803C0EBFCD88. This may change in the future. Check Docker’s official website to see if they match. The key is used to verify digital signatures, so you can make sure the software you are installing is legitimate and not malware uploaded to the server by an attacker.

Once you make sure you have the right key, add it to APT’s trusted keys.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add Docker’s repository for Ubuntu to your software sources.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Install Docker.

sudo apt update && sudo apt install docker-ce

If you also need Docker Compose, you can install it with:

sudo apt install docker-compose

If you don’t know what this is, you probably don’t need it.

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

sudo adduser $USER 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.

How to Install Docker on Debian

Since Ubuntu is created from Debian, the steps are very similar.

sudo apt update && sudo apt install software-properties-common apt-transport-https ca-certificates curl gnupg2

Check fingerprint which should be the same as in the previous section.

curl -fsSL https://download.docker.com/linux/debian/gpg | gpg

Add to trusted keys.

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

Add software sources.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

Install Docker.

sudo apt update && sudo apt install docker-ce

If you also need Docker Compose, install it with:

sudo apt install docker-compose

To use Docker commands without prefixing with sudo:

sudo adduser $USER docker

Then log out and log back in.

How to Install Docker on Fedora

Install prerequisites.

sudo dnf install dnf-plugins-core

Add Docker software repository.

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

Install Docker.

sudo dnf install docker-ce

docker-install-on-fedora

On some systems this will take some time to complete and might seem like it’s stuck. Don’t abort the operation. Of course, it might also really get stuck, but give it at least ten minutes to do its job on slower computers.

Enable auto-start of Docker at boot.

sudo systemctl enable docker.service

Start Docker.

sudo systemctl start docker.service

Add yourself to the Docker group.

sudo usermod -aG docker $USER

Log out and log back in to your graphical user interface.

People that also need Docker Compose can install it with:

sudo dnf install docker-compose

How to Install Docker on Arch Linux

Arch already includes Docker in its community repositories, so the install procedure is easier here.

sudo pacman -S docker

docker-install-on-arch

If you also need Docker Compose:

sudo pacman -S docker-compose

Add yourself to the Docker group:

sudo usermod -aG docker $USER

Log out of the graphical user interface and log back in for the setting to take effect.

Enable Docker to automatically start at boot.

sudo systemctl enable docker

Start the Docker service.

sudo systemctl start docker

Conclusion

Obviously, this doesn’t cover all distributions that are out there. But if you’re using something like openSUSE, you may be lucky enough to find an instruction page on how to install Docker on your distribution.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox