Docker is a revolutionary open-source containerization platform that has redefined the way applications are built, tested, and deployed. It enables developers to package an application and its dependencies into a portable container that runs consistently across diverse environments, whether on local development machines, servers, or cloud-based platforms.
This guide provides a comprehensive step-by-step tutorial on how to install Docker on Ubuntu, ensuring you get the latest version for your projects.
Prerequisites
Before starting the installation, ensure the following:
- Supported Ubuntu Version: You are using a compatible Ubuntu version, such as Ubuntu 23.10, 22.04, or 20.04.
- User Privileges: You have a user account with
sudo
privileges to execute administrative commands. - Updated System: Your system’s package index is up-to-date to avoid compatibility issues.
How to Install Docker on Ubuntu
Step 1: Update Your System
The first step is to update your system. Keeping your system updated ensures that you have the latest security patches and compatibility improvements. Open a terminal and run:
sudo apt update sudo apt upgrade -y
This command updates the package index and upgrades all outdated packages.
Step 2: Install Required Dependencies
Docker requires certain dependencies to be installed before proceeding with the installation. Install them using the following command:
sudo apt install -y ca-certificates curl gnupg
These dependencies ensure secure communication with Docker’s repository and the ability to manage cryptographic keys.
Step 3: Add Docker’s GPG Key
The GPG key verifies the authenticity of the Docker packages. Add the key to your system using these commands:
sudo mkdir -p /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
This step ensures that the packages you install are trusted and secure.
Step 4: Add Docker’s Official Repository
To install the latest version of Docker, add its official repository to your system’s APT sources list. Execute the following command:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \ sudo tee /etc/apt/sources.list.d/docker.list
The command dynamically fetches your Ubuntu codename (e.g., jammy
for Ubuntu 22.04) and configures the repository.
Step 5: Install Docker
After adding Docker’s repository, update your package index again to include it and install Docker:
sudo apt update sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
This command installs Docker along with its essential plugins, ensuring you have all the tools needed to work with containers.
Step 6: Verify Docker Installation
To confirm that Docker was installed successfully and is running, check its status using:
sudo systemctl status docker
The output should indicate that Docker is active and running. Additionally, verify the installed version with:
docker --version
This displays the current Docker version installed on your system.
Step 7: Enable Non-Root User Access to Docker
By default, only users with sudo
privileges can run Docker commands. To allow a non-root user to execute Docker commands, add the user to the Docker group:
sudo usermod -aG docker $USER
Here, $USER
is an environment variable representing the current username. If you want to add another user, replace $USER
with the username. After executing the command, log out and log back in to apply the changes.
Step 8: Test Docker Installation
To ensure Docker is working correctly, run a test container:
docker container run hello-world
This command:
- Downloads the
hello-world
test image from Docker Hub (if not already available locally). - Runs the container, which outputs a “Hello from Docker” message.
If the message appears, your Docker installation is successful.
Installing a Specific Version of Docker
Sometimes, you may need to install a specific version of Docker. First, list all available versions in Docker’s repository:
sudo apt update apt list -a docker-ce
Identify the desired version and install it by specifying the version string. For example:
DOCKER_VERSION=5:26.0.0-1~ubuntu.22.04~jammy sudo apt-get install docker-ce=$DOCKER_VERSION docker-ce-cli=$DOCKER_VERSION containerd.io docker-buildx-plugin docker-compose-plugin
This ensures you’re installing the exact version required for your project.
Updating Docker
To update Docker to the latest version, use the following commands:
sudo apt update && sudo apt upgrade
If you wish to prevent Docker from being updated automatically, mark it as held back:
sudo apt-mark hold docker-ce
This is useful in environments where maintaining a specific version is critical.
Uninstalling Docker
If you need to remove Docker, follow these steps. First, stop all running containers and clean up resources:
docker container stop $(docker container ls -aq) docker system prune -a --volumes
Next, uninstall Docker:
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin sudo apt autoremove
To completely remove Docker’s files and directories, run:
sudo rm -rf /var/lib/{docker,containerd}
This removes all traces of Docker from your system.
Conclusion
In this guide, we have provided detailed instructions on how to install Docker on Ubuntu. From setting up Docker’s repository to running your first test container, you now have all the tools to harness Docker’s full potential. Docker’s portability, scalability, and efficiency make it a cornerstone of modern software development and DevOps practices.
For further exploration, visit Docker’s official documentation or engage with its active community forums. If you encounter any challenges during installation, leave a comment or seek assistance from Docker’s robust user community.
By following this guide, you’re ready to leverage the power of Docker to streamline your development and deployment workflows, unlocking new possibilities for innovation and efficiency.
FAQs: How to Install Docker on Ubuntu
What is Docker, and why should I install it on Ubuntu?
Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and consistent across different environments, making Docker ideal for developers and system administrators.
What are the system requirements for installing Docker on Ubuntu?
Ubuntu version: 18.04, 20.04, or later.A 64-bit architecture.
A minimum of 2GB RAM is recommended for running containers.
Sudo or root access to the system.
How do I install Docker on Ubuntu?
Steps to install Docker on Ubuntu:
Update the system:
sudo apt update && sudo apt upgrade -y
Install prerequisites:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
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
Set up the Docker repository:
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
Install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Verify installation:
docker --version
How do I check if Docker is running?
Run the following command to check Docker’s status:
sudo systemctl status docker
If it’s not running, start it with:
sudo systemctl start docker
How can I run Docker without sudo?
Add your user to the docker group:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect.
How do I test Docker after installation?
Run the following command to ensure Docker is working correctly:
sudo docker run hello-world
This pulls and runs the hello-world
container, displaying a confirmation message.
What is the difference between Docker CE and Docker EE?
Docker CE (Community Edition): Free and open-source version suitable for developers and small teams.
Docker EE (Enterprise Edition): Paid version with additional features for large organizations, such as enhanced security and support.
How do I uninstall Docker from Ubuntu?
To remove Docker and its related components, run:
sudo apt update
sudo apt install --only-upgrade docker-ce docker-ce-cli containerd.io
How do I update Docker on Ubuntu?
Update Docker to the latest version with:
sudo apt update
sudo apt install --only-upgrade docker-ce docker-ce-cli containerd.io
Where can I find more resources or troubleshooting help?
Official Docker documentation: https://docs.docker.com
Ubuntu community forums.
Stack Overflow for common troubleshooting issues.