How do I install docker in Ubuntu 20.04? In this guide we are going to install docker in Ubuntu 20.04.
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.
Related Posts
- How to install and configure docker In Fedora 34/35
- How to install and configure docker on Debian 11
- How to install and configure docker In Centos 8
- Getting Started With Docker Compose With Examples
- Docker as a build agent – Run Jenkins builds on Docker
Prerequisites
To follow along, ensure you have:
- An Ubuntu 20.04 system
- Internet access from the system
- Root access or user with sudo access
Table of Content
- Ensure your system packages are up to date
- Uninstall previous versions if any
- Install docker engine using the repository
- Install docker engine from a package
- Install using the convenience script
- Verify installation
- Executing the Docker Command Without Sudo
- Uninstalling docker when it’s longer needed
1. Ensure your system packages are up to date
Before proceeding, let us ensure your packages are updated
sudo apt update
sudo apt upgrade -y
2. Uninstall previous versions if any
Packages providing older versions of Docker are docker
, docker.io
, or docker-engine
.
Let us use this command to ensure that these packages do not exist in our system:
sudo apt-get remove -y docker docker-engine docker.io containerd runc
It’s OK if apt-get reports that none of these packages are installed.
Installing docker
You can install Docker Engine in different ways, depending on your needs:
- You can set up docker repositories and install docker using apt
- You can download the DEB packages and install manually
- You can choose to use automated convenience scripts to install Docker
3. Install docker engine using the repository
On a new host, the docker repositories do not exist. For the first time on a new host machine, you need to set up the Docker repository. Afterwards, you can install and update Docker from the repository.
Let us install the following packages to allow apt to use a repository over HTTPS:
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Use the following command to set up the 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
Install Docker Engine
Update the apt package index since we added a new repository:
sudo apt-get update
Now let’s install the latest version of Docker Engine and containerd usi
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Optional: Installing a specific version
If you have requirements that nececitate a specific version, you can do so.
To install a specific version:
List the versions available in your repo:
$ sudo apt-cache madison docker-ce
docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.8~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.7~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.6~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.5~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
Now you can Install a specific version using the version string from the second column, for example, 5:19.03.9~3-0~ubuntu-focal
.
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
Example:
sudo apt-get install docker-ce=5:19.03.9~3-0 docker-ce-cli=5:19.03.9~3-0 containerd.io
4. Install docker engine from a package
If you cannot use Docker’s repository to install Docker Engine, you can download the .deb file for your release and install it manually. You need to download a new file each time you want to upgrade Docker.
Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version, then browse to pool/stable/
, choose amd64
, armhf
, or arm64
, and download the .deb file for the Docker Engine version you want to install.
Install Docker Engine, changing the path below to the path where you downloaded the Docker package.
sudo dpkg -i /path/to/package.deb
The Docker daemon starts automatically.
Verify that Docker Engine is installed correctly by running the hello-world image.
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
5. Install using the convenience script
Docker provides a convenience script at get.docker.com to install Docker into development environments quickly and non-interactively.
This example downloads the script from get.docker.com and runs it to install the latest stable release of Docker on Linux:
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
6. Verify installation
Upon installation, docker will run as a daemon. To check that docker is running, we use the systemctl status docker
command:
$ 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 Fri 2021-10-08 05:35:57 UTC; 3min 50s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 146750 (dockerd)
Tasks: 8
Memory: 30.0M
CGroup: /system.slice/docker.service
└─146750 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.378859538Z" level=warning msg="Your kernel does not support CPU r>
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.378869630Z" level=warning msg="Your kernel does not support cgrou>
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.378877385Z" level=warning msg="Your kernel does not support cgrou>
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.379112713Z" level=info msg="Loading containers: start."
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.500736755Z" level=info msg="Default bridge (docker0) is assigned >
Oct 08 05:35:57 ip-172-26-11-229 dockerd[146750]: time="2021-10-08T05:35:57.560469239Z" level=info msg="Loading containers: done."
The Active: active (running)
shows that docker was started successfully.
We can run a hello-world
image to test that docker works as expected:
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
I got this output, showing that it works fine:
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
7. Executing the Docker Command Without Sudo
By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:
$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
sudo usermod -aG docker ${USER}
To apply the new group membership, log out of the server and back in, or type the following:
su - ${USER}
You will be prompted to enter your user’s password to continue.
Confirm that your user is now added to the docker group by typing:
id -nG
I see this, confirming that i now belong to the docker group
$ id -nG
ubuntu docker
Now I can do docker:
I can run alpine image
$ docker run --rm -it --name alpine --entrypoint /bin/sh alpine:3
Unable to find image 'alpine:3' locally
3: Pulling from library/alpine
a0d0a0d46f8b: Pull complete
Digest: sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a
Status: Downloaded newer image for alpine:3
/ #
We have successfully installed docker in our system!
8. Uninstalling docker when it’s longer needed
To complately erase docker engine from the system, we need to uninstall the Docker Engine, CLI, and Containerd packages:
sudo apt-get purge docker-ce docker-ce-cli containerd.io -y
Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
You must delete any edited configuration files manually.
Conclusion
In this guide we managed to install docker on out Ubuntu 20.04 system.