Install Docker on Ubuntu 20.04 (with Compose) + 3 Easy Tips

There are many ways to install docker on Ubuntu, which can be overwhelming. This post shows how to install Docker on Ubuntu 20.04 LTS Focal Fossa, with Docker Compose support.

If you did not already know, Docker is the heart of my home server. Previously, I used to install all my home server apps directly on my server.

Needless to say, this was cumbersome and docker has revolutionized the way applications are deployed worldwide.

While most of our guides published previously include brief instructions, due to the importance of this topic, I decided to write a dedicated guide to install Docker on Ubuntu and Debian based Linux distributions.

In this post, you will learn how to install Docker on Ubuntu 20.04 LTS codenamed "Focal Fossa". I am currently running Focal but will soon upgrade to Jammy. [Read: Install Docker on Ubuntu 22.04 (with Compose) + 3 Simple Tips]

But stick around, my original and very popular docker media server guide and Traefik reverse proxy guide are getting old and updated versions of those are in the works.

What is Docker?

Docker, also known as Docker Engine, is an open-source containerization technology for applications. It allows users to install applications inside software containers.

What this means is that applications can be separated/isolated from the operating system on which they are running.

But isn't this what Virtualization does? Yes, but there are differences.

Docker Vs Virtual Machines Made By Docker
Docker Vs Virtual Machines Made By Docker

We have previously explained what is Docker and compared it to virtual machines. Therefore, for more details I strongly urge you to review the linked article.

Why use Docker for Home Server?

In Docker containers, each application is self-contained, therefore setup and removal is easy and has little to no effect on the operating system.

With several HTPC and home server apps already available for easy installation as containers, on Docker Hub, this is the new way to setup your home server quickly.

Add to this, the features of Docker Compose (more on this later), starting up and down your stack and migrating it becomes so much easier.

Install Docker on Ubuntu 20.04 LTS

Note that in this tutorial, all commands are being run as the user (anand). If you are logged in as root, you can drop the sudo in front of the commands.

Now that you know what Docker is, let us learn how to install Docker on Ubuntu 20.04 and Debian Linux.

Install Docker on Ubuntu (with Compose) - Don't Do It WRONG

While there are several operating systems for home server, Ubuntu Server has been and still is my choice.

There are many ways to setup Docker on Debian systems. First, I am going to show you the best and the recommended way.

Note that the "Best" way for Ubuntu docker installation does not mean the "fastest" or the "easiest". It means, the best way to install it for long-term benefit (i.e. faster updates, security patches, etc.).

Even though the Ubuntu Docker installation method that I recommed should be enough, I will list some of the other methods for informational purposes.

Pre-Requisites for Docker Ubuntu Setup

Before you begin, you must enable hardware virtualization. This refers to VT-x on Intel and AMD-V on AMD motherboards. This is required for Docker to run.

On AMD motherboards AMD-V is enabled by default. However, on Intel motherboards, you will to manually enable VT-x from BIOS / UEFI. [Read: enable Intel VT-x hardware virtualization in BIOS or UEFI firmware]

Step 1: Update and Install Docker Dependencies

First, let us update our packages list and install the required docker dependencies.

sudo apt update

Then, use the following command to install the dependencies or pre-requisite packages.

sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

Step 2: Add Docker Repository to APT Sources

While installing Docker Engine from Ubuntu repositories is easier, adding official docker repository gives you faster updates. Hence why this is the recommended method.

First, let us get the GPG key which is needed to connect to the Docker repository. To that, use the following command.

 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Note: If you use curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - as in some older guides, you see the following message - Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead.

Next, add the repository to the sources list. While you can also add it manually, the command below will do it automatically for you.

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

The above command will automatically fill in your release code name (jammy for 22.04, focal for 20.04, and bionic for 18.04).

Finally, refresh your packages again.

sudo apt update

If you forget to add the GPG key, then the above step would fail with an error message. Otherwise, let us get on with installing Docker on Ubuntu.

Step 3: Install Docker on Ubuntu 20.04 (or Debian) Linux

What is the difference between docker.io and docker-ce?

docker.io is the docker package that is offered by some popular Linux distributions (e.g. Ubuntu/Debian). docker-ce on the other hand, is the docker package from official Docker repository. Typically docker-ce more up-to-date and preferred.

In this Ubuntu Docker setup guide, we will install the docker-ce (and not docker.io package).

To install Docker on Ubuntu 20.04 LTS or Debian, use the following command:

sudo apt install docker-ce

This will download and install several hundrend MBs of packages, as shown below.

Install Docker On Ubuntu Server 20.04 Focal Fossa
Install Docker On Ubuntu Server 20.04 Focal Fossa

Continue and the docker engine installation process should normally go through without any issues.

Step 4: Verify that Docker is Running on Ubuntu

There are many ways to check if Docker is running on Ubuntu. One way is to use the following command:

sudo systemctl status docker

You should see an output that says active for status.

Check If Docker Is Running On Ubuntu
Check If Docker Is Running On Ubuntu

The next way is to use sudo docker ps -a command, which will show you the running containers. There should be nothing at this point but the command should still return a blank list.

List Of Active Docker Containers
List Of Active Docker Containers

The example above shows the error that is output when docker is not running. Then we start docker and rerun the command to see a blank list of containers.

The last yet the official way to check if Docker is running is to actually start a container and check. The most famous one is "hello-world".

Check If Docker Is Running Using Hello World
Check If Docker Is Running Using Hello World

The above command downloads and runs the hello-world test container and if successful, prints a message, and exits as shown above.

If the tests pass then you are good to go. You now have Docker installed on Ubuntu.

Be the 1 in 200,000. Help us sustain what we do.
35 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Install Docker-Compose on Ubuntu 20.04

Docker Compose is an additional piece of software that takes Docker to the next level.

Compose allows you to create a list of services/containers and their configuration in YAML. Once done, you can create, start, and stop all of your containers with one command.

Unfortunately, Docker Compose is not automatically installed with Docker. Although it is included in repositories of most Linux distributions, as with Docker, the version is probably not current.

So let us see how to install Docker Compose on Ubuntu and Debian Linux systems.

Step 1: Check the Current Version of Docker Compose

As said before, the version of docker-compose packaged with the Linux distribution is probably old.

Verion Of Docker Compose In Ubuntu 20.04 Focal Fossa Repo
Verion Of Docker Compose In Ubuntu 20.04 Focal Fossa Repo

Checking the releases on Docker Compose GitHub, the last release is v2.4.1. So the Ubuntu repo version (1.29.2) in the screenshot above is at least several versions old.

Step 2: Install Docker Compose on Ubuntu

Unlike Docker, there are no official repositories that you can add to easily install Docker Compose on Ubuntu.

You will have to manually download and install it. But fear not, it is quite easy.

First, download the latest version of Docker Compose using the following command:

sudo curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Change v2.4.1 to the current realease number. Next, make it executable using the following command:

sudo chmod +x /usr/local/bin/docker-compose

That is it. Docker Compose should now be installed on your Ubuntu system.

Step 3: Check if Docker Compose is Installed

Let us check to make sure Docker Compose is installed and is available for us to use using docker-compose -v.

Check If Docker Compose Is Installed
Check If Docker Compose Is Installed

The screenshot above shows Docker compose installation, permission change, and version check.

If the installation was successful, you should see the docker compose version number as the output.

Ubuntu Docker Installation: Alternate Methods

As said in the beginning of this guide, the best and the recommended method to install Docker Engine on Ubuntu is described above.

But here are few other ways to get Docker on Ubuntu:

From Linux Distro Repo

Right after installing Ubuntu or other distros, you can run sudo apt install docker.io and you should have Docker running.

The version of Docker you will get is probably older and hence this method is not recommended.

Using Docker DEB Package

Pre-packaged DEB files are available here for Ubuntu 20.04.

Download the stable file from pool->stable for your architecture (normally amd64) and run sudo dpkg -i package-name.deb from the same directory as the downloaded .deb file.

This should install Docker as well. However, you will have to manually download and install each update the same way. Therefore, I wouldn't recommend this method either.

Using Installer Script

There is a script that can basically do all the steps listed in the recommended method. Download the script using the following command:

curl -fsSL https://get.docker.com -o get-docker.sh

Then, run it using the following command:

sudo sh get-docker.sh

Although in this case it is probably OK (trusted source), running a one line script is never recommended. In addition, this script assumes all default options (e.g. cannot pick a custom version).

Therefore, I do not recommend using the Docker installer script as well. Plus, the steps described above are not complex to follow.

Basic Docker and Docker-Compose Commands

Now that you have both Docker and Docker Compose installed on Ubuntu 20.04 LTS Focal Fossa, let us look at some basic usage.

There are numerous docker and docker compose commands and covering them all is not the purpose of this post. You can see all possible commands using the following command:

docker

But, here are few docker and docker compose commands to get started:

  • sudo docker info - Information on your docker installation.
  • sudo docker search IMAGE_NAME - Search for specific images/containers.
  • sudo docker start CONTAINER_NAME - Start one or more containers.
  • sudo docker stop CONTAINER_NAME - Stop one or more containers.
  • sudo docker restart CONTAINER_NAME - Restart one or more containers.
  • sudo docker top CONTAINER_NAME - See running processes of a container.
  • sudo docker rm CONTAINER_NAME - Remove one or more containers.
  • sudo docker pull CONTAINER_NAME - Pull updated images from Docker Hub.
  • sudo docker network ls - See all defined docker networks.
  • sudo docker ps -a - See all running containers.
  • sudo docker logs CONTAINER_NAME - See logs of a one, more, or all containers.
  • sudo docker-compose up -d CONTAINER_NAME - Start specific or all services defined in docker-compose.yml.
  • sudo docker-compose down CONTAINER_NAME - Stop specific or all services defined in docker-compose.yml.

Note that in most cases CONTAINER_NAME is optional and the command applies to all possible containers if no container name is specified.

There are several more cool commands I use, but it is best that I refer you to my bash aliases that lists all the commands I use frequently in my Ubuntu docker environment.

Tips to Enchance Docker Experience

1. Setup Bash Aliases to Simplify Commands

If you have no clue what I am talking about, then I suggest you to read our Bash Aliases guide for a general idea.

With bash aliases set, you can run several docker and docker compose commands using shortcuts (instead of the full commands listed above). For example, just dps (or whatever you set) instead of sudo docker ps -a.

For a list of all bash aliases I currently use to help manage my Docker home server, check out my GitHub repo.

2. Add User to Docker Group

I personally do not do or recommend this. All my bash aliases have sudo added where required and I elevate my privileges when required. But if you prefer the convenience of not having to use sudo for Docker commands then you may choose to implement it.

Running and managing docker containers requires sudo privileges. So this means you will have to type sudo for every command or switch to the root user account. But you can get around this by adding the current user to the docker group using the following command:

sudo usermod -aG docker ${USER}

You can replace ${USER} with your user name or just run the command as-is while you are logged in.

While this can be a minor security risk, it should be OK as long as other Docker security measures are in place.

3. Securing Docker

If not implemented properly, there are several places where you can leave a security hole for the intruder to get in.

There are several simple security tweaks such as:

I suggest that you review the security practices linked above to ensure that your Docker setup on Ubuntu is well protected.

FAQs

How do autostart Docker on Boot?

If you followed the method recommended above, Docker should autostart on Ubuntu during boot. Nothing more is needed. However, in case of any issues or if you want to force it anyway, use sudo systemctl enable docker.

Does Docker run on Ubuntu?

Yes, Docker does run very well on Ubuntu, Debian, and other Linux distributions. On Mac and Windows, there is Docker Destop.

Is Docker the same as Docker Desktop?

No, Docker and Docker Desktop are different products. Docker Desktop includes Docker Engine and is designed to enable Mac and Windows systems to build, share, and run Docker containers. Having said that, Docker Desktop Ubuntu and for other Linux distributions will be available soon (currently in Beta).

Concluding Remarks

So there you have it, detailed information on how to install Docker and Docker Compose on Ubuntu 20.04 LTS Focal Fossa and additional information to take your installation to the next well.

As mentioned before, Docker is the engine that runs my home server. I also run Docker on my Synology to host many apps. In addition, this website runs on a server powered by Docker as well.

It has made deployments and migrations a breeze for me.

As said before, I am currently on 20.04 LTS version but will soon upgrade to the new Ubuntu LTS release.

In the next few days/weeks, I will also be publishing long overdue updated versions of my Docker media server and Traefik reverse proxy guides. So stay tuned!

Be the 1 in 200,000. Help us sustain what we do.
35 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.