In this tutorial, we are going to be looking at Docker setup, by far one of the most popular container solutions out there. While Docker is available on Windows, Mac, and various Linux distributions, we’ll be working off an Ubuntu 20.04 server.
Ubuntu ships a docker.io package in its own repositories, and it is usually well behind. Adding Docker’s repository instead means you get current releases and security fixes when Docker publishes them rather than when the distribution gets round to it. That is the reason for the extra steps below, which otherwise look like a lot of ceremony for one install.
lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
Spin up and SSH into the server. When working with Linux, Docker follows the same basic steps when installing Docker regardless of distributions. First, we configure the Docker repository, and then finally we install the needed Docker packages themselves.
Prerequisites
Let’s start with updating our machine.
sudo apt update
And then install any prerequisite packages.
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
We can now add Docker’s GPG key.
This is what makes the repository trustworthy rather than just reachable. Packages from Docker are signed with their key, apt checks each one against it, and anything that fails the check is refused. Without the key you are downloading software over the internet and hoping.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
And verify its configuration.
sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
To add the Docker repository, we now just need to run:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 https://download.docker.com/linux/ubuntu focal InRelease [36.2 kB]
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [3056 B]
Fetched 39.2 kB in 0s (83.8 kB/s)
Reading package lists... Done
And update our server again.
sudo apt update
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
Worth actually reading the output rather than glancing at it. If the candidate version points at Ubuntu’s repository instead of Docker’s, something in the previous steps did not take, and it is far easier to catch here than to work out afterwards why the version is older than expected.
apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 5:19.03.12~3-0~ubuntu-focal
Version table:
5:19.03.12~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.11~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.10~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.9~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
Installing Docker
We can now install the necessary packages.
sudo apt install docker-ce docker-ce-cli containerd.io
sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-09-05 22:53:29 UTC; 1min 22s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 3945 (dockerd)
Tasks: 8
Memory: 45.5M
CGroup: /system.slice/docker.service
└─3945 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Executing the Docker Command Without Sudo (Optional)
To finish up, we want to add our ubuntu user to the docker group.
Read this bit before running it. Membership of the docker group is equivalent to root on the machine. Not similar to, equivalent to. Anyone in that group can start a container that mounts the host filesystem and read or change anything on it, entirely without sudo and without leaving the usual audit trail.
On your own development box that is a reasonable trade for convenience. On a shared or production server it is a decision to make deliberately, because you are handing out administrator access under a name that does not sound like it.
sudo usermod -aG docker ${USER}
id -nG
ubuntu adm dialout cdrom floppy sudo audio dip video plugdev netdev lxd docker
You can also confirm the change using the grep command as follows.
grep docker /etc/group
docker:x:998:ubuntu
Working with Docker Images
Log out then log back in to refresh the Bash session before running any docker commands.
Not optional, and the reason is that group membership is attached to your session when you log in. Your current shell has no idea anything changed, so docker commands keep failing with a permission error on the socket and it looks as though the group change did not work. It did. The session is just stale.
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
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/
Now check the images status on server.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 8 months ago 13.3kB
To install Ubuntu image we will first search with the following command.
docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 11285 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 459 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 246 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 225 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 110 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 98 [OK]
neurodebian NeuroDebian provides neuroscience research s… 69 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50
You need to execute the below mentioned command to download the official ubuntu image which is indicated with OK in OFFICIAL column:
docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
54ee1f796a1e: Pull complete
f7bfea53ad12: Pull complete
46d371e02073: Pull complete
b66c17bbf772: Pull complete
Digest: sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
You can now see the ubuntu latest image as follows.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 4e2eef94cd6b 2 weeks ago 73.9MB
hello-world latest bf756fb1ae65 8 months ago 13.3kB
Conclusion
That is Docker installed from its own repository, running without sudo, with an image pulled to prove it works.
The two things worth carrying forward from this: the repository setup is what keeps you on current versions rather than whatever the distribution froze, and the docker group is a genuine privilege decision rather than a convenience toggle. Everything else about Docker is easier to learn once there is a working install to try it on.
With the daemon running, the next thing worth understanding is how images are put together and why layer caching matters: working effectively with Docker images.