How to Install Docker on Ubuntu 20.04
If you're using Ubuntu 20.04 and want to set up Docker, you're in the right place. Let's go through the steps to install Docker on your system, making sure you're ready to dive into containerization.
Before You Start
First, let's make sure your system is ready for Docker. You'll need the 64-bit version of Ubuntu 20.04 (LTS). Docker works with different hardware types, so whether you're using an x86_64, armhf, arm64, s390x, or ppc64le system, you should be good to go.
Security and Firewall Considerations
Before installing Docker, consider your firewall setup. If you use ufw
or firewalld
, remember that when you open ports for Docker containers, those ports won't be affected by your usual firewall rules. Also, Docker only works well with iptables-nft
and iptables-legacy
. If you're using nft
for firewall rules, you might run into issues. Make sure any firewall rules you set up are made with iptables
or ip6tables
and add them to the DOCKER-USER
chain.
Removing Old Docker Versions
If you've used Docker before, you might have older versions installed that could cause problems. You'll want to remove these before installing the latest version. Here are the packages you might need to remove:
docker.io
docker-compose
docker-compose-v2
docker-doc
podman-docker
Also, Docker depends on containerd
and runc
. If you've installed these separately, you'll need to remove them too to avoid conflicts. Use this command to get rid of any old packages:
sudo apt-get remove docker.io docker-compose docker-compose-v2 docker-doc podman-docker
If you want a completely fresh start, you can also remove any existing Docker data:
sudo rm -rf /var/lib/docker
Installing Docker Using the Repository
The recommended way to install Docker on Ubuntu is by using Docker's official apt
repository. Here's how you do it:
- Update Your Package Index:
First, make sure your package index is up to date:sudo apt-get update
- Install Prerequisites:
You'll need some packages to allowapt
to use a repository over HTTPS:sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
- Add Docker's Official GPG Key:
This key helps verify the integrity of the Docker packages:curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Add the Docker Repository:
Now, add the Docker repository to yourapt
sources:echo "deb [arch=$(dpkg --print-architecture) 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
- Update the Package Index Again:
With the new repository added, update your package index again:sudo apt-get update
- Install Docker Engine:
Now you can install the latest version of Docker Engine:sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Verify the Installation:
Run a simple test to make sure Docker is installed correctly:sudo docker run hello-world
This command will download a test image and run it in a container. If everything is set up right, you'll see a message saying the Docker installation is working.
Running Docker Without Root
By default, you need to use sudo
to run Docker commands. If you want to run Docker without sudo
, you can add your user to the docker
group:
sudo usermod -aG docker $USER
After doing this, you'll need to log out and log back in for the changes to take effect.
Upgrading Docker
To upgrade Docker, you just need to update your package index and install the latest version:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Wrapping Up
That's it! You've now got Docker installed on your Ubuntu 20.04 system. You're all set to start using Docker to create, deploy, and run applications in containers. If you run into any issues or have questions, the Docker community and documentation are great resources to check out.