How to Install Docker and Docker Compose on Ubuntu: Complete Guide
Docker has revolutionized how we deploy and manage applications, making it easier than ever to containerize your projects. Whether you’re a developer looking to streamline your workflow or a system administrator managing multiple services, Docker and Docker Compose are essential tools in your toolkit.
In this comprehensive guide, we’ll walk you through two methods to install Docker and Docker Compose on Ubuntu: a step-by-step manual installation and an automated script method for quick setup.
Prerequisites
Before we begin, make sure you have:
- Ubuntu 18.04 LTS or later
- sudo privileges on your system
- A stable internet connection
Method 1: Manual Step-by-Step Installation
Step 1: Update Your System
First, let’s ensure your system is up to date:
sudo apt-get update
sudo apt-get upgrade -y
Step 2: Install Required Dependencies
Install the necessary packages that Docker requires:
sudo apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release
Step 3: Add Docker’s Official GPG Key
Add Docker’s official GPG key to verify the authenticity of the packages:
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Step 4: Set Up the Docker Repository
Add the Docker repository to your system’s package sources:
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
Step 5: Install Docker Engine
Update your package index and install Docker:
sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Step 6: Add Your User to the Docker Group
To run Docker commands without sudo, add your user to the docker group:
sudo usermod -aG docker $USER
Important: You’ll need to log out and log back in for this change to take effect, or run:
newgrp docker
Step 7: Verify the Installation
Test that Docker is installed correctly:
docker --version docker run hello-world
Step 8: Verify Docker Compose
Docker Compose is now included as a plugin. Verify it’s working:
docker compose version
Method 2: Automated Installation Script
For those who prefer a quick, automated installation, here’s a bash script that handles everything for you:
#!/bin/bash # Set strict error handling set -euo pipefail # Function to check and install Docker install_docker() { if command -v docker &> /dev/null; then echo "Docker is already installed" docker --version return 0 fi echo "Docker not found. Installing Docker..." # Update package index and install prerequisites sudo apt-get update sudo apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release # Add Docker's official GPG key sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg # Set up Docker repository 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 # Install Docker Engine sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin # Add current user to docker group sudo usermod -aG docker $USER echo "Docker installed successfully!" docker --version echo "Please log out and log back in for docker group changes to take effect." # Apply group changes for current session echo "Applying group changes for current session..." newgrp docker } # Function to verify Docker Compose verify_docker_compose() { echo "Verifying Docker Compose installation..." if docker compose version &> /dev/null; then echo "Docker Compose is working correctly!" docker compose version else echo "Docker Compose verification failed. Please check your installation." exit 1 fi } # Main script execution echo "Starting Docker and Docker Compose installation..." install_docker verify_docker_compose echo "Installation completed successfully!"
How to Use the Script
- Save the script to a file:
nano install_docker.sh
- Make it executable:
chmod +x install_docker.sh
- Run the script:
./install_docker.sh
Post-Installation Steps
Enable Docker to Start on Boot
To ensure Docker starts automatically when your system boots:
sudo systemctl enable docker sudo systemctl enable containerd
Test Your Installation
Create a simple test to ensure everything is working:
# Test Docker docker run --rm hello-world # Test Docker Compose with a simple configuration mkdir docker-test && cd docker-test cat > docker-compose.yml << 'EOF' version: '3.8' services: test: image: nginx:alpine ports: - "8080:80" EOF docker compose up -d docker compose down cd .. && rm -rf docker-test
Troubleshooting Common Issues
Permission Denied Error
If you get a “permission denied” error when running Docker commands:
sudo usermod -aG docker $USER newgrp docker
Docker Daemon Not Running
If Docker daemon isn’t running:
sudo systemctl start docker sudo systemctl enable docker
Repository Issues
If you encounter repository-related errors, ensure your system’s date and time are correct:
sudo apt-get update
Best Practices and Security Considerations
- Regular Updates: Keep Docker updated to the latest version for security patches
- User Management: Only add trusted users to the docker group
- Resource Limits: Configure appropriate resource limits for containers
- Network Security: Be cautious with port mappings and network configurations
Uninstalling Docker (If Needed)
If you need to remove Docker completely:
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.gpg