Ubuntu Docker Install: The Ultimate Guide
This guide provides a comprehensive walkthrough on how to install Docker Engine on Ubuntu, covering both quick setup and production-ready methods. Docker empowers developers to package applications and their dependencies into portable containers, ensuring consistency across various environments.
What is Docker?
Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and are thus more lightweight than virtual machines.
Benefits of Docker:
* Portability: Applications run the same way regardless of the underlying infrastructure.
* Isolation: Applications and their dependencies are isolated, preventing conflicts.
* Efficiency: Containers are lightweight and start quickly, making better use of system resources.
* Scalability: Easily scale applications by deploying more containers.
* CI/CD Integration: Seamlessly integrates with Continuous Integration and Continuous Deployment pipelines.
Prerequisites
Before you begin, ensure you have the following:
* A machine running Ubuntu (LTS versions like 20.04, 22.04 are recommended).
* A non-root user with sudo privileges.
* An active internet connection.
Method 1: Install using the convenience script (Quick & Easy)
This method is suitable for development environments or quick testing. It’s generally not recommended for production environments as it can install older or less tested versions.
-
Download and run the convenience script:
bash
sudo apt update
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.shcurl -fsSL https://get.docker.com -o get-docker.sh: Downloads the script from Docker’s official website.sudo sh get-docker.sh: Executes the script, which automatically installs Docker and its dependencies.
-
Verify the installation:
bash
sudo docker run hello-worldIf Docker is installed correctly, this command downloads a test image and runs a container that prints “Hello from Docker!”
Method 2: Install from Docker’s official repository (Recommended for Production)
This is the most robust and recommended way to install Docker Engine on Ubuntu, ensuring you get the latest stable version directly from Docker.
-
Update your existing list of packages:
bash
sudo apt update -
Install necessary packages for
aptto use a repository over HTTPS:bash
sudo apt install ca-certificates curl gnupg lsb-release -y -
Add Docker’s official GPG key:
bash
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpgThis command downloads the GPG key and adds it to your system, which verifies the authenticity of the Docker packages.
-
Set up the stable Docker repository:
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/nullThis command adds the Docker repository to your APT sources, so your system knows where to download Docker packages from.
-
Update the
aptpackage index again:bash
sudo apt update -
Install Docker Engine, containerd, and Docker Compose:
bash
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -ydocker-ce: The Docker Engine (Community Edition).docker-ce-cli: The command-line interface for Docker.containerd.io: A high-level container runtime.docker-buildx-plugin: Docker BuildX plugin for extended build capabilities.docker-compose-plugin: Docker Compose V2.
-
Verify the Docker Engine installation:
bash
sudo docker run hello-worldThis command should download a test image and run a container that outputs “Hello from Docker!”, confirming a successful installation.
Post-installation Steps
After installing Docker, there are a few important post-installation steps to enhance usability and security.
Manage Docker as a non-root user
By default, the docker command requires sudo privileges. To run Docker commands without sudo, you need to add your user to the docker group.
-
Add your user to the
dockergroup:bash
sudo usermod -aG docker $USER -
Apply the new group membership:
You need to log out and log back in, or restart your system, for the changes to take effect. Alternatively, you can activate the changes for your current session using:bash
newgrp docker -
Verify that you can run Docker commands without
sudo:bash
docker run hello-worldIf it runs without error, your user can now manage Docker.
Configure Docker to start on boot
Docker is typically configured to start automatically on boot. You can check its status and enable it if necessary:
-
Check Docker service status:
bash
sudo systemctl status dockerLook for “Active: active (running)”.
-
Enable Docker to start on boot (if not already):
bash
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
Uninstalling Docker (Optional)
If you need to remove Docker from your system, follow these steps:
-
Stop and remove Docker packages:
bash
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y -
Remove Docker images, containers, volumes, and networks (optional, but recommended for a clean uninstall):
bash
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd -
Remove the Docker repository and GPG key:
bash
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.gpg -
Update your package index:
bash
sudo apt update
Conclusion
You have now successfully installed Docker Engine on your Ubuntu system using the recommended method and configured it for ease of use. With Docker, you can now effortlessly package, deploy, and manage your applications in isolated and portable containers. Explore Docker Hub for pre-built images or start creating your own Dockerfiles to containerize your projects.