Docker Installation on Ubuntu Server: A Complete Guide
Step-by-step guide to manually installing Docker Engine on Ubuntu servers with Docker Compose, GPG key management, and user group configuration
Why Install Docker Manually?
While Docker provides convenience scripts, understanding the manual installation process gives you better control and insight into how Docker integrates with your Ubuntu system. This guide walks through the official installation method that's production-ready and idempotent—meaning you can safely repeat these steps without breaking anything.
Prerequisites
Before we begin, make sure you have:
- Ubuntu server (18.04, 20.04, 22.04, or later)
- Root or sudo access
- Internet connection for package downloads
Step 1: Check Existing Installation
First, let's see if Docker is already installed on your system:
docker --version
docker compose version
systemctl status dockerIf Docker is already installed, running, and you're in the docker group, you can skip ahead. Otherwise, let's proceed with a fresh installation.
Step 2: Clean Up Old Installations
Remove any conflicting Docker packages that might be present:
sudo apt remove -y docker docker-engine docker.io containerd runcThis ensures a clean slate and prevents package conflicts during installation.
Step 3: Install Prerequisites
Update your package index and install the necessary tools:
sudo apt update -y
sudo apt install -y ca-certificates curl gnupg lsb-releaseThese packages are required for:
- ca-certificates: Verify SSL certificates when downloading packages
- curl: Download files from the internet
- gnupg: Manage GPG keys for package verification
- lsb-release: Detect your Ubuntu version automatically
Step 4: Add Docker's GPG Key
Docker signs its packages to ensure authenticity. Let's add their 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.gpgThis downloads Docker's public key and stores it in a format APT can use for package verification.
Step 5: Configure Docker Repository
Add Docker's official APT repository to your system:
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
sudo apt updateThis command:
- Detects your system architecture automatically
- Links to the GPG key for verification
- Uses
lsb_releaseto detect your Ubuntu version - Updates the package list with Docker packages
Step 6: Install Docker Engine
Now install Docker and all its components:
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginThis installs:
- docker-ce: Docker Engine (Community Edition)
- docker-ce-cli: Command-line interface
- containerd.io: Container runtime
- docker-buildx-plugin: Advanced build features
- docker-compose-plugin: Docker Compose v2
Step 7: Configure User Permissions
By default, Docker requires sudo for every command. Add your user to the docker group:
sudo usermod -aG docker $USERImportant: You must log out and log back in for this change to take effect. Alternatively, run:
newgrp dockerThis creates a new shell session with the updated group membership.
Step 8: Enable Docker Service
Ensure Docker starts automatically on system boot:
sudo systemctl enable docker
sudo systemctl start dockerCheck the service status:
sudo systemctl status dockerYou should see active (running) in the output.
Step 9: Verify Installation
Test that everything is working correctly:
# Check Docker version
docker --version
# Check Docker Compose version
docker compose version
# Run a test container
docker run hello-worldThe hello-world container will download, run, print a success message, and exit. If you see the success message, Docker is working perfectly!
Troubleshooting
Permission Denied Error
If you get permission denied when running Docker commands:
- Verify you're in the docker group:
groups - If not, ensure you logged out and back in after Step 7
- Or use
newgrp dockerto refresh your session
Service Won't Start
Check for errors:
sudo systemctl status docker
sudo journalctl -u docker.serviceCommon issues:
- Port conflicts with existing services
- Missing dependencies
- Corrupted installation (try removing and reinstalling)
Docker Compose Command Not Found
If docker compose doesn't work but docker-compose does, you might have the old standalone version. The plugin uses docker compose (space, not hyphen).
What's Next?
Now that Docker is installed, you can:
- Learn about Docker images and containers
- Create your first Dockerfile
- Explore Docker Compose for multi-container applications
- Set up Docker volumes for persistent data
Conclusion
You now have a production-ready Docker installation on Ubuntu! This manual process gives you full control over Docker's configuration and helps you understand each component. The installation is idempotent, so you can safely re-run these steps if needed without breaking your system.
Remember to keep Docker updated with regular apt update && apt upgrade commands, and always test new Docker versions in a development environment before deploying to production.
Happy containerizing!