Complete Docker Installation on Ubuntu Server
Complete guide to installing Docker on Ubuntu Server including repository setup, post-installation configuration, and verification.
# Complete Docker Installation on Ubuntu Server
Introduction
Docker is a containerization platform that enables developers to package applications with their dependencies into standardized units. This tutorial covers Docker installation on Ubuntu Server, post-installation configuration, and verification.
Learning Objectives
By the end of this tutorial, you will be able to:
- Install Docker Engine on Ubuntu Server
- Configure Docker post-installation
- Manage Docker as a non-root user
- Verify Docker installation
- Install Docker Compose
- Uninstall Docker if needed
Technical Background
Docker Architecture
Client --> Docker Daemon --> Containers
| |
| Images Registry
|
CLI/REST APIDocker Components
| Component | Description | |-----------|-------------| | Docker Daemon | Background service managing containers | | Docker CLI | Command-line interface | | Docker Image | Read-only template for containers | | Docker Container | Runnable instance of an image | | Docker Registry | Storage for images (Docker Hub, ECR, etc.) | | Docker Compose | Multi-container application tool |
Prerequisites
- Ubuntu Server 22.04 LTS
- Root or sudo access
- 64-bit architecture
- Internet access
Hardware and Software Requirements
| Component | Requirement | |-----------|-------------| | OS | Ubuntu 22.04 LTS | | RAM | 2 GB minimum | | Disk | 20 GB minimum | | CPU | 1 vCPU minimum |
Step-by-Step Implementation
Phase 1: Install Docker Using Repository
# Update package index
sudo apt-get update
# Install prerequisites
sudo apt-get install -y \
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 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 docker-ce-rootless-extras
# Verify installation
sudo docker --versionPhase 2: Post-Installation Configuration
#### Manage Docker as Non-Root User
# Create docker group
sudo groupadd docker
# Add current user to docker group
sudo usermod -aG docker $USER
# Apply group changes
newgrp docker
# Verify (no sudo needed)
docker run hello-world#### Configure Docker to Start on Boot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service#### Configure Docker Daemon
# Create daemon configuration
sudo tee /etc/docker/daemon.json << 'EOF'
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"storage-driver": "overlay2"
}
EOF
# Restart Docker
sudo systemctl restart dockerPhase 3: Verify Installation
# Run hello-world container
docker run hello-world
# Check Docker info
docker info
# Check Docker version
docker version
# List running containers
docker ps
# List all containers
docker ps -a
# List images
docker imagesPhase 4: Install Docker Compose (Standalone)
# Download Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Make executable
sudo chmod +x /usr/local/bin/docker-compose
# Create symlink
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# Verify
docker compose versionPhase 5: Basic Docker Operations
#### Run a Container
# Run Nginx container
docker run -d -p 80:80 --name my-nginx nginx:latest
# View running container
docker ps
# Stop container
docker stop my-nginx
# Remove container
docker rm my-nginx#### Build a Custom Image
# Dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]# Build image
docker build -t my-nginx:latest .
# Run image
docker run -d -p 8080:80 --name my-nginx-custom my-nginx:latest#### Manage Images
# List images
docker images
# Remove image
docker rmi my-nginx:latest
# Pull image
docker pull nginx:latest
# Tag image
docker tag my-nginx:latest myregistry.com/my-nginx:latest
# Push image
docker push myregistry.com/my-nginx:latestPhase 6: Docker Networking
# List networks
docker network ls
# Create custom network
docker network create my-network
# Run container on network
docker run -d --name my-container --network my-network nginx:latest
# Connect running container to network
docker network connect my-network my-containerPhase 7: Docker Volumes
# Create volume
docker volume create my-data
# Run container with volume
docker run -d -v my-data:/app/data --name my-app nginx:latest
# List volumes
docker volume ls
# Inspect volume
docker volume inspect my-data
# Remove volume
docker volume rm my-dataPhase 7: Docker Compose Example
# docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
networks:
- app-network
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: example
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-network
networks:
app-network:
volumes:
db-data:# Start services
docker compose up -d
# Stop services
docker compose down
# View logs
docker compose logs -f
# Scale service
docker compose up -d --scale web=3Phase 8: Uninstall Docker
# Remove packages
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Remove dependencies
sudo apt-get autoremove -y
# Remove images and data
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerdExpected Output
- Docker Engine installed and running
- Docker Compose installed
- Non-root user can run Docker commands
- Containers and images manageable
- Docker networks and volumes functional
Verification Procedures
| Check | Command | Expected Result | |-------|---------|-----------------| | Docker version | `docker --version` | Version displayed | | Docker info | `docker info` | Server details shown | | Hello world | `docker run hello-world` | Success message | | Docker Compose | `docker compose version` | Version displayed |
Troubleshooting Scenarios
| Issue | Diagnosis | Resolution | |-------|-----------|------------| | Permission denied | User not in docker group | Add user to docker group; newgrp | | Docker daemon not running | Service not started | `sudo systemctl start docker` | | Cannot pull image | Network or auth issue | Check network; login to registry | | Container exits immediately | Application error | Check container logs |
Best Practices
- **Use official images:** Start from verified base images
- **Pin image versions:** Use specific tags, not `latest`
- **Multi-stage builds:** Reduce image size
- **Non-root user:** Run containers as non-root
- **Resource limits:** Set memory and CPU limits
- **Health checks:** Add HEALTHCHECK to Dockerfile
- **Use volumes:** Don't store data in container layers
Security Considerations
- **Run as non-root:** Avoid root user in containers
- **Image scanning:** Scan images for vulnerabilities
- **Content trust:** Enable Docker Content Trust
- **Secrets management:** Use Docker Secrets or external secret manager
- **Network isolation:** Use custom networks to isolate containers
- **Read-only root filesystem:** Where possible
Conclusion
Docker installation on Ubuntu Server is straightforward using the official repository. With proper post-installation configuration, Docker Compose, and security best practices, you can run containerized applications reliably and securely.
References
- Docker Documentation: https://docs.docker.com/
- Docker Engine Install: https://docs.docker.com/engine/install/ubuntu/
- Docker Compose: https://docs.docker.com/compose/
- Docker Security: https://docs.docker.com/engine/security/