Computer Science Atlas
Tutorial

How to Install Docker on Ubuntu

January 16, 2021|Last Updated March 8, 2021
 

Overview

These instructions work on Ubuntu versions 20.10 (Groovy), 20.04 (Focal), 18.04 (Bionic) and 16.04 (Xenial).

Step 1. Open a Terminal Session

If you're using an Ubuntu laptop or desktop, you can press Ctrl + Alt + T on your keyboard to open a new terminal window. If you're using a remote Ubuntu server, you can connect using SSH to open a new terminal session.

Step 2. Set Apt to Trust Docker's Repo

Download Docker's Apt repo key:

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

Verify the fingerprint of the key you just downloaded by running the command:

Bash
Copy
sudo apt-key fingerprint 0EBFCD88
$
sudo apt-key fingerprint 0EBFCD88

and checking that the output matches this:

Out
Copy
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) 
sub   rsa4096 2017-02-22 [S]
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) 
sub   rsa4096 2017-02-22 [S]

Step 3. Add the Docker Apt Repo

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

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

Step 4. Install Docker using Apt

Bash
Copy
sudo apt update -y
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose
$
$
sudo apt update -y
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose

Step 5. Test the Installation

Run a Docker command to test that the installation worked:

Bash
Copy
sudo docker ps
$
sudo docker ps

If installation was successful, you should see the following output:

Out
Copy
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Step 6. Allow the Current User to Use Docker Commands

In the previous step, you had to use sudo in front of the docker command. To get rid of this requirement, first add the current user to the docker user group:

Bash
Copy
sudo groupadd docker
sudo gpasswd -a $USER docker
$
$
sudo groupadd docker
sudo gpasswd -a $USER docker

Then exit the current terminal session:

Bash
Copy
exit
$
exit

The next time you login, you will be able to run Docker commands without sudo.

Congratulations!

You are now ready to use Docker on your machine.