Docker is one of the most popular platforms for containerizing applications, allowing developers to package and deploy software in isolated environments. Installing Docker on Ubuntu is a straightforward process, but for new users, it can still present challenges. In this guide, we will walk you through the steps to install Docker on the latest Ubuntu version, covering what Docker is, how it works, and potential issues that may arise during installation.
This guide will ensure that you have a working Docker environment on your Ubuntu system, allowing you to get started with containerized application development in no time.
What is Docker?
Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications using containers. Containers are lightweight, portable, and isolated environments that ensure the application runs the same across different systems.
- Lightweight: Docker containers share the host operating system’s kernel, which reduces overhead.
- Portability: Docker allows you to package applications with their dependencies, making them portable across different environments.
In short, Docker helps developers build, ship, and run applications more efficiently.
How Docker Works
Docker works by creating containers that isolate the application’s code, dependencies, and environment variables. These containers are managed using Docker images and Dockerfiles, which serve as blueprints for creating the containers.
- Docker Engine: The core of Docker that manages containers and images.
- Docker Hub: A registry service for sharing Docker images.
Docker makes it easier for teams to work on the same application, ensuring that everyone is working in the same environment.
Steps to Install Docker on Latest Ubuntu
Follow these steps to install Docker on the latest version of Ubuntu. This guide is tested on Ubuntu 20.04 and Ubuntu 22.04, but should work on newer versions as well.
Step 1: Update the System
Before installing Docker, it is recommended to update your system to ensure all existing packages are up to date. Open a terminal and run the following command:
sudo apt update && sudo apt upgrade -y
This command will update the system’s package list and install the latest versions of all packages.
Step 2: Install Required Dependencies
Install the necessary packages that allow APT to use repositories over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
These dependencies ensure that Docker can be installed securely.
Step 3: Add Docker’s Official GPG Key
Docker provides an official GPG key that must be added to ensure that the packages are trusted. Run the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
This command downloads the Docker GPG key and adds it to your keyring.
Step 4: Set Up the Docker Repository
To add Docker’s repository to APT sources, run:
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
This command tells APT where to find Docker packages for Ubuntu.
Step 5: Install Docker Engine
Now that the Docker repository is set up, update the package list again and install Docker Engine:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
This installs Docker and all the required components to run containers on your system.
Verifying Docker Installation
To verify that Docker is installed correctly, run the following command:
docker --version
You should see the version of Docker that is installed. Additionally, you can run the Hello World container to verify that Docker is running properly:
sudo docker run hello-world
This command will download and run a test Docker container that prints “Hello from Docker!”, confirming that your installation is working.
Managing Docker as a Non-Root User
By default, Docker commands must be run with sudo, but you can configure it to run without sudo by adding your user to the Docker group:
sudo usermod -aG docker ${USER}
Log out and log back in to apply the changes. Now you can run Docker commands without needing sudo.
Common Issues and Solutions
Issue: Docker Daemon Not Running
If you encounter an error stating that the Docker daemon is not running, you can start it with:
sudo systemctl start docker
To ensure Docker starts automatically on boot, enable it with:
sudo systemctl enable docker
Issue: Permission Denied Error
If you see a permission denied error when running Docker commands without sudo, make sure your user is added to the Docker group, as shown in the Managing Docker as a Non-Root User section.
Issue: Outdated Packages
If you run into dependency errors, make sure your system packages are up to date using sudo apt update
and sudo apt upgrade
before attempting to install Docker again.
Conclusion
Installing Docker on Ubuntu is a straightforward process that allows you to leverage the power of containers for efficient application development. By following the steps outlined in this guide, you can have Docker up and running in minutes, ready to run and deploy your applications in a consistent environment.
Docker makes it easier for developers and IT professionals to create, manage, and scale applications without worrying about environment differences. Whether you are a beginner or an experienced developer, Docker is a tool worth mastering.