Install Docker on Ubuntu: A Step-by-Step Guide
Docker has revolutionized the way developers build, ship, and run applications. By containerizing applications, Docker provides a consistent environment across different machines, simplifying deployment and scaling. This guide will walk you through the process of installing Docker on an Ubuntu system, ensuring you have everything set up to start containerizing your projects.
Prerequisites
Before you begin, ensure you have:
- An Ubuntu System: This guide is written for Ubuntu 20.04 LTS (Focal Fossa) or later versions.
- Sudo Privileges: You need a user account with
sudoprivileges to execute administrative commands. - Internet Connection: Required to download Docker packages.
Step 1: Update Your System
It’s always a good practice to update your package lists and upgrade any installed packages before installing new software. This ensures you have the latest security patches and dependencies.
Open your terminal (Ctrl+Alt+T) and run:
bash
sudo apt update
sudo apt upgrade -y
Step 2: Install Necessary Packages
Docker requires a few prerequisite packages to function correctly. Install them using the following command:
bash
sudo apt install ca-certificates curl gnupg lsb-release -y
ca-certificates: Allows web browsers and other applications to check the authenticity of SSL/TLS certificates.curl: A command-line tool for transferring data with URLs, used here to download Docker’s GPG key.gnupg: GNU Privacy Guard, used to manage cryptographic keys.lsb-release: Provides information about the Linux Standard Base and specific distribution.
Step 3: Add Docker’s Official GPG Key
To ensure the authenticity and integrity of the Docker packages you download, you need to add Docker’s official GPG key to your system’s keyring.
bash
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
This command first creates a directory for APT keyrings if it doesn’t exist, then downloads the GPG key from Docker’s official repository and stores it securely in /etc/apt/keyrings/docker.gpg.
Step 4: Set Up the Docker Repository
Now, you need to add the Docker repository to your APT sources. This tells your system where to find the Docker packages.
bash
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Let’s break down this command:
* deb: Indicates a Debian-style package repository.
* arch=$(dpkg --print-architecture): Automatically detects your system’s architecture (e.g., amd64).
* signed-by=/etc/apt/keyrings/docker.gpg: Specifies the GPG key used to sign the packages in this repository.
* https://download.docker.com/linux/ubuntu: The URL for Docker’s official Ubuntu repository.
* $(lsb_release -cs): Automatically detects your Ubuntu version’s codename (e.g., focal for Ubuntu 20.04).
* stable: Specifies that you want to use the stable Docker repository.
* sudo tee /etc/apt/sources.list.d/docker.list: Writes the entire string to a new file named docker.list inside /etc/apt/sources.list.d/, which is where APT looks for additional repositories.
* > /dev/null: Suppresses the output of tee to the console.
After adding the repository, update your package lists again to include the new Docker packages:
bash
sudo apt update
Step 5: Install Docker Engine
With the repository configured, you can now install the Docker Engine, Containerd, and Docker Compose.
bash
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
docker-ce: The Docker Community Edition engine.docker-ce-cli: The command-line interface for Docker.containerd.io: A high-performance container runtime.docker-buildx-plugin: A Docker CLI plugin for extended build capabilities.docker-compose-plugin: The Docker Compose plugin for defining and running multi-container Docker applications.
Step 6: Verify Docker Installation
To confirm that Docker is installed correctly and running, you can run the hello-world image:
bash
sudo docker run hello-world
If Docker is installed correctly, you will see a message similar to this:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Step 7: Manage Docker as a Non-Root User (Optional but Recommended)
By default, running Docker commands requires sudo privileges. To run Docker commands as a non-root user (without sudo), you need to add your user to the docker group.
First, add your user to the docker group:
bash
sudo usermod -aG docker $USER
usermod: Modifies user account properties.-aG docker: Adds the specified user ($USERrefers to your current username) to thedockergroup.
For the changes to take effect, you need to log out and log back in, or restart your system.
Alternatively, you can activate the changes for your current session without logging out by running:
bash
newgrp docker
Now, try running the hello-world image again without sudo:
bash
docker run hello-world
If it runs successfully, you have configured Docker to be used by your non-root user.
Step 8: Configure Docker to Start on Boot
Docker is usually configured to start automatically on boot. You can verify its status and enable it if necessary:
bash
sudo systemctl enable docker
sudo systemctl start docker
sudo systemctl status docker
The status command should show active (running).
Conclusion
You have successfully installed Docker on your Ubuntu system! You are now ready to explore the world of containerization, build and deploy your applications efficiently, and leverage the power of Docker for development and production environments.
Remember to regularly update your system and Docker to benefit from the latest features, bug fixes, and security improvements. Happy containerizing!