In this tutorial you will learn how to install docker on your Ubuntu machine.
Table of Contents
Prerequisites
- Ubuntu installation as host, in VM or as WSL
- Internet connection
Motivation
I assume that you already know advantages of containerizing your applications via docker. However if not, here are some benefits:
- Portability: Since docker containers can run on almost any host with docker installed on it (also windows hosts) it is really easy to migrate your containers to another system. e.g. if you want to create a production system out of your development containers, it won’t take a lot of effort.
- Ease-of-use: With docker-compose scripts, the required administration efforts for installation and maintenance are reduced because you only need to maintain one YAML file instead of a whole filesystem.
- Low coupling: Docker also provides the benefit that you can run different applications simultaneously without one application messing up the dependencies of another application.
- Higher performance than VM’s: Since Docker containers run on the host’s kernel they only need new user spaces with the desired applications. VM’s on the other hand have their own kernel and therefore need more space and system resources to run each operation through both VM’s and host’s kernel. If you need multiple applications creating separate VMs for each app would lead to many redundant kernel installations.
Installation
- Remove old docker installation
sudo apt-get remove docker docker-engine docker-compose docker.io containerd runc
2. Update apt package index
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
3. Add docker’s GPG encryption key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. Setup stable repository
echo \ "deb [arch=amd64 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
5. Updating & upgrading
sudo apt update && sudo apt upgrade -y
6. Installing docker engine and docker-compose
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose
Usage
Check installation
After that you can check whether the installation was successful by checking the installed versions.
sudo docker version
You can expect an output like the following:
Client: Docker Engine - Community Version: 20.10.6 API version: 1.41 Go version: go1.13.15 Git commit: 370c289 Built: Fri Apr 9 22:46:38 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.6 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 8728dd2 Built: Fri Apr 9 22:44:49 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.4 GitCommit: x runc: Version: 1.0.0-rc93 GitCommit: x docker-init: Version: 0.19.0 GitCommit: x
And for docker compose you can use:
sudo docker-compose version
docker-compose version 1.25.0, build unknown docker-py version: 4.1.0 CPython version: 3.8.6 OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
Example Application
Now you can also test docker (compose) with a stardard webserver container like nginx.
1. Navigate to the directory where you want the container configuration YAML-file to be located at.
mkdir -p ~/docker/example/ cd ~/docker/example/
2. Create docker-compose.yml
sudo nano docker-compose.yml
I will use nano for that. If you don’t have it installed, yet, use “sudo apt install nano -y“
If you are not fimiliar with nano here a few basic controls:
Save file: CTRL+O then ENTER Navigate in code: Arrow Keys
In the editor you can paste the follwing configuration-lines:
version: '3.5' services: webserver: image: nginx restart: always ports: - 80:80
This configuration will create a container from the latest Nginx image. Moreover, it will link the hosts port 80 (web port) to container’s port 80, so the host (and other network devices) can access the webserver in the container.
Now, we only have to compose a docker container from the docker-compose.yml. This will enter the container’s shell and display all status messages. If you only want to run the container and hide detach from the container’s shell, append the “-d” option.
sudo docker-compose up # or for detached shell: sudo docker-compose up -d
Now you can access the webserver in the host by opening http://127.0.0.1/ or http://localhost/ in the browser.
If you cannot do that on the host itself because you only have a command-line, there’s also the possibility to access the webserver from another computer by opening http://ip-adress-in-network/. You can find the host’s IP address with the “ifconfig” command. If you’d like to make the container accessible from all over the internet, you have to allow connections through the host’s and router’s firewall. Find more about port-forwarding.

With the command
sudo docker ps
you are now also able to check out all running docker containers.
Shutting down test container
Now, we only need to stop and remove the container again. We can do that by executing the following command in the same directory as the docker-compose.yml file.
sudo docker-compose down
Credits
This tutorial is based on docker’s official installation guide for Ubuntu.
Disclaimer
If you are facing problems or spot mistakes, please inform me about it.