π³ 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.
| |
π‘ Notes:
- The
apt updatecommand updates the list of available packages from repositories - The
apt upgrade -ycommand 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 protocolca-certificates: Provides SSL/TLS certificates for secure connection authenticationcurl: Tool to download files from the internetsoftware-properties-common: Provides utilities for managing repositories
| |
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:
| |
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:
| |
π‘ Explanation:
arch=amd64: Specifies 64-bit architecturesigned-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 usetestornightlyfor experimental versions)
2.3. Install Docker Engine
After adding the repository, update the package list and install Docker Engine:
| |
π¦ Packages Being Installed:
docker-ce: Docker Community Edition - the open-source version of Dockerdocker-ce-cli: Docker Command Line Interface - command-line tool to interact with Dockercontainerd.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:
| |
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:
| |
π‘ Command Explanations:
systemctl start docker: Starts the Docker service immediatelysystemctl 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:
| |
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-worldimage 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:
| |
π‘ Explanation:
usermod -aG docker $USER: Adds the current user to thedockergroup (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
dockergroup grants equivalent root privileges on the Docker daemon, so only add trusted users
Verify New Permissions:
After configuration, try running Docker without sudo:
| |
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
| |
Step 2: Delete Docker Data
Docker data (images, containers, volumes, networks) is stored in /var/lib/docker. To remove completely:
| |
β οΈ 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:
| |
π Basic Docker Commands
After installation, here are some basic commands you should know:
| Command | Description |
|---|---|
docker --version | Check Docker version |
docker ps | List running containers |
docker ps -a | List all containers (including stopped) |
docker images | List 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:
| |
Issue: Permission denied when running Docker
Cause: User hasn’t been added to the docker group.
Solution:
| |
Issue: Cannot connect to Docker daemon
Cause: Docker daemon hasn’t been started.
Solution:
| |
π― 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:
- Official Docker Documentation
- Docker Hub - The largest image repository
- Docker Best Practices
Good luck with Docker! ππ
