Featured image of post How to Install Docker on Ubuntu 🐳

How to Install Docker on Ubuntu 🐳

Detailed guide to install Docker on Ubuntu 20.04 and 22.04, including user permission configuration and verification. Get Docker running in minutes!

🐳 Introduction to Docker

Docker is a popular platform in DevOps for deploying applications using containers. Containers allow you to package applications together with all their dependencies and necessary configurations, ensuring consistent execution across all environments from development to production.

Benefits of Docker:

  • ⚑ Consistency: Applications run identically across all environments
  • πŸš€ Speed: Containers start faster than virtual machines
  • πŸ“¦ Packaging: Easy to share and deploy applications
  • πŸ”„ Scalability: Easy to scale and manage multiple containers

This guide walks you through installing Docker on Ubuntu 20.04 and 22.04 in a straightforward and efficient manner, using Docker’s official repository to ensure you always have the latest stable version.


βœ… Prerequisites

Before you begin, make sure you have:

  • πŸ–₯️ Ubuntu 20.04 or 22.04 server installed
  • πŸ” Sudo privileges to execute administrative commands
  • 🌐 Internet connection to download packages
  • ⏱️ Time: Installation takes approximately 5-10 minutes

1. Update the System

Before installing Docker, ensure your system is fully updated. This guarantees you have the latest security patches and necessary dependencies.

1
sudo apt update && sudo apt upgrade -y

πŸ’‘ Notes:

  • The apt update command updates the list of available packages from repositories
  • The apt upgrade -y command automatically installs updates without prompting
  • This process may take several minutes depending on the number of packages to update

2. Install Docker

2.1. Install Required Dependencies

These packages are necessary for Docker to download and authenticate packages from the official repository:

  • apt-transport-https: Allows APT to use HTTPS protocol
  • ca-certificates: Provides SSL/TLS certificates for secure connection authentication
  • curl: Tool to download files from the internet
  • software-properties-common: Provides utilities for managing repositories
1
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

2.2. Add the Official Docker Repository

To install Docker from the official source, we need to add Docker’s GPG key and repository to the system. This ensures you receive official and security updates from Docker.

Step 1: Add Docker’s GPG Key

The GPG key is used to authenticate the integrity of Docker packages:

1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Step 2: Add Docker Repository

Add Docker repository to APT’s sources list. This command automatically detects your Ubuntu version ($(lsb_release -cs)) and adds the appropriate repository:

1
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

πŸ’‘ Explanation:

  • arch=amd64: Specifies 64-bit architecture
  • signed-by: Specifies the GPG key for authentication
  • $(lsb_release -cs): Automatically retrieves Ubuntu’s code name (e.g., jammy for Ubuntu 22.04)
  • stable: Uses the stable channel (you can use test or nightly for experimental versions)

2.3. Install Docker Engine

After adding the repository, update the package list and install Docker Engine:

1
2
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

πŸ“¦ Packages Being Installed:

  • docker-ce: Docker Community Edition - the open-source version of Docker
  • docker-ce-cli: Docker Command Line Interface - command-line tool to interact with Docker
  • containerd.io: Container runtime - low-level tool for managing container lifecycle

⏱️ Installation Time: This process typically takes 2-5 minutes depending on your internet speed.


3. Verify Docker

After installation, verify that Docker is running properly:

1
sudo systemctl status docker

Expected Result: You should see the status active (running) if Docker started successfully.

If Docker isn’t running, you can start it and enable auto-start on boot:

1
2
sudo systemctl start docker
sudo systemctl enable docker

πŸ’‘ Command Explanations:

  • systemctl start docker: Starts the Docker service immediately
  • systemctl enable docker: Configures Docker to auto-start on system boot

4. Test Docker

To confirm Docker is working correctly, run the sample hello-world container:

1
sudo docker run hello-world

Expected Result: If you see the “Hello from Docker!” message along with Docker information, congratulations, installation successful! πŸŽ‰

πŸ” What does the hello-world container do?

  • Downloads the hello-world image from Docker Hub (if not already present)
  • Runs the container and displays a welcome message
  • Automatically stops after completion

5. Configure User Permissions

By default, Docker requires root privileges to run commands. To run Docker without sudo, add your current user to the docker group:

1
2
sudo usermod -aG docker $USER
newgrp docker

πŸ’‘ Explanation:

  • usermod -aG docker $USER: Adds the current user to the docker group (which has Docker daemon access privileges)
  • newgrp docker: Applies group changes immediately without needing to log out

⚠️ Important Notes:

  • After executing this command, you may need to log out and log back in for changes to take effect
  • Alternatively, you can open a new terminal window to apply changes
  • Adding a user to the docker group grants equivalent root privileges on the Docker daemon, so only add trusted users

Verify New Permissions:

After configuration, try running Docker without sudo:

1
docker run hello-world

If the command succeeds without sudo, you’ve configured it correctly! βœ…


6. Uninstall Docker (if needed)

If you want to completely remove Docker from your system, follow these steps:

Step 1: Remove Docker Packages

1
sudo apt remove -y docker-ce docker-ce-cli containerd.io

Step 2: Delete Docker Data

Docker data (images, containers, volumes, networks) is stored in /var/lib/docker. To remove completely:

1
sudo rm -rf /var/lib/docker

⚠️ Warning: This command will delete ALL Docker data including:

  • All downloaded images
  • All containers (running and stopped)
  • All volumes and networks
  • All data within containers

Step 3: Remove Docker Configuration (optional)

To completely remove all traces of Docker:

1
2
sudo rm -rf /var/lib/containerd
sudo rm -rf /etc/docker

πŸ“š Basic Docker Commands

After installation, here are some basic commands you should know:

CommandDescription
docker --versionCheck Docker version
docker psList running containers
docker ps -aList all containers (including stopped)
docker imagesList all images
docker pull <image>Download image from Docker Hub
docker run <image>Run container from image
docker stop <container>Stop running container
docker rm <container>Remove container
docker rmi <image>Remove image

πŸ”§ Troubleshooting

Issue: Docker daemon won’t start

Cause: May be due to conflicts with other services or misconfiguration.

Solution:

1
2
3
4
5
# Check Docker logs
sudo journalctl -u docker

# Restart Docker
sudo systemctl restart docker

Issue: Permission denied when running Docker

Cause: User hasn’t been added to the docker group.

Solution:

1
2
3
4
5
# Add user to docker group
sudo usermod -aG docker $USER

# Log out and log back in, or run:
newgrp docker

Issue: Cannot connect to Docker daemon

Cause: Docker daemon hasn’t been started.

Solution:

1
2
3
4
5
# Check status
sudo systemctl status docker

# Start Docker
sudo systemctl start docker

🎯 Conclusion

Congratulations on completing Docker installation on Ubuntu! πŸŽ‰ Now you can:

  • 🐳 Create and run containers for your applications
  • πŸ“¦ Use Docker images available from Docker Hub
  • πŸš€ Deploy applications consistently and easily
  • πŸ”„ Manage development and production environments more efficiently

Docker is a powerful tool in the DevOps world. Continue exploring Docker features like Docker Compose, Docker Swarm, and CI/CD integration to optimize your development workflow!

πŸ“– References:

Good luck with Docker! πŸš€πŸ˜Š