Installing Docker on Ubuntu

Installing Docker on Ubuntu

This article will give you 2 options for installing Docker on Ubuntu. The first will be a line by line guide that includes the commands you need to run alongside why you need to run them. At the bottom of this article you’ll find the the delightful second option – a bash script. If this is your first time setting up Docker I’d recommend going through the step by step instructions, but if this isn’t your first rodeo, copy/paste that bash script and save yourself a couple minutes.

Note: This guide will work on all Ubuntu 16.04, Ubuntu 18.04, and Ubuntu 20.04 distributions.

Section 1: Line by Line docker install

1. Ensure you have the prerequisite packages installed. These packages are needed for later steps.

sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

2. Add the official docker repository GPG key. This tells apt that it can trust packages coming from docker.

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

3. Add the docker repository itself.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(source /etc/os-release;echo $UBUNTU_CODENAME) stable"

4. Update your repository list so that it scans download.docker.com when looking for new packages.

sudo apt update

5. Install Docker. After this docker should be installed. It’s recommended to keep proceeding through the next steps to ensure that docker is setup to give you least hassle as possible.

sudo apt install -y docker-ce

6. Ensure the docker service is enabled (set to start on reboots).

sudo systemctl enable docker

7. Add yourself to the ‘docker’ group. This will prevent you from having to run ‘sudo’ in front of every docker command you issue. The second part of this command refreshes your terminal so that the group settings are applied – alternatively you could have logged out and back in again.

sudo usermod -aG docker ${USER} && su - ${USER}

That’s it! Docker should be all installed on your Ubuntu system. If you want to take a gander at the bash script that performs these actions for us carry on to the next section.

Section 2: Installing Docker on Ubuntu with a bash script

Compatibility note: This script should work on all Ubuntu 16.04, Ubuntu 18.04, and Ubuntu 20.04 distributions.

To run this bash script simply copy the content below into a new file – we’ll call it docker_install.sh and run it with: bash docker_install.sh

#!/bin/bash
clear
echo "Starting installation of Docker on Ubuntu"
sudo apt update &&
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common &&
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &&
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(source /etc/os-release;echo $UBUNTU_CODENAME) stable" &&
sudo apt update &&
sudo apt install -y docker-ce &&
sudo systemctl enable docker &&
sudo usermod -aG docker ${USER} &&
su - ${USER}

Conclusion

Docker should be up and running on your Ubuntu system. To help you get started I also created a Docker CLI Cheat Sheet. The Cheat Sheet is a PDF that lets you copy, paste, and search with ease. You might also find some of our other Docker content interesting as well.

2 thoughts on “Installing Docker on Ubuntu”

  1. Pingback: Docker Cheat Sheet (PDF) - Swiss Army DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *