How to Install and Configure Docker on Ubuntu: Complete Beginner Guide

Introduction

Docker revolutionizes application deployment by using containerization technology. This comprehensive guide walks you through installing Docker on Ubuntu, from basic setup to advanced configuration and first container deployment.

What is Docker?

Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers. Unlike virtual machines, containers share the host OS kernel, making them more efficient and faster to start.

Key Benefits:

  • Consistent environments across development and production
  • Faster deployment and scaling
  • Improved resource utilization
  • Easy application isolation
  • Simplified dependency management

Prerequisites

  • Ubuntu 20.04 LTS or later (64-bit)
  • Sudo privileges
  • Internet connection
  • At least 2GB RAM (4GB recommended)
  • 10GB free disk space

Method 1: Install Using Official Repository (Recommended)

Step 1: Update Package Index

sudo apt update

Step 2: Install Prerequisites

sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release

Step 3: Add Docker’s Official GPG Key

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

Step 4: Set Up Stable Repository

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu  stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Install Docker Engine

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 6: Verify Installation

sudo docker run hello-world

Method 2: Install Using Convenience Script

For testing environments only (not recommended for production):

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Post-Installation Configuration

Add User to Docker Group

To run Docker commands without sudo:

sudo usermod -aG docker $USER
newgrp docker

Log out and back in, then test:

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 file:

sudo nano /etc/docker/daemon.json

Add configuration (example):

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}

Restart Docker:

sudo systemctl restart docker

Essential Docker Commands

Image Management

# List images
docker images

# Pull an image
docker pull ubuntu:latest

# Remove image
docker rmi ubuntu:latest

# Build image from Dockerfile
docker build -t myapp:latest .

Container Management

# List running containers
docker ps

# List all containers
docker ps -a

# Run container
docker run -d --name webserver nginx

# Stop container
docker stop webserver

# Start container
docker start webserver

# Remove container
docker rm webserver

# Execute command in running container
docker exec -it webserver bash

System Commands

# Show Docker version
docker version

# Show system information
docker info

# Show disk usage
docker system df

# Clean up unused resources
docker system prune

Running Your First Container

Example 1: Web Server

# Run Nginx web server
docker run -d --name my-nginx -p 8080:80 nginx

# Access at http://localhost:8080

Example 2: Interactive Ubuntu Container

# Run interactive Ubuntu container
docker run -it --name my-ubuntu ubuntu:latest bash

# Install packages, make changes, then exit
exit

# Commit changes to new image
docker commit my-ubuntu my-custom-ubuntu

Example 3: Database Container

# Run MySQL database
docker run -d --name mysql-db 
  -e MYSQL_ROOT_PASSWORD=mypassword 
  -e MYSQL_DATABASE=testdb 
  -p 3306:3306 
  mysql:8.0

Docker Compose Setup

Verify Docker Compose

docker compose version

Create docker-compose.yml

version: '3.8'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  
  database:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: webapp
    volumes:
      - mysql_data:/var/lib/mysql

volumes:
  mysql_data:

Run with Docker Compose

# Start services
docker compose up -d

# Stop services
docker compose down

# View logs
docker compose logs

Best Practices

Security

  • Don’t run containers as root when possible
  • Use official images from trusted sources
  • Regularly update base images
  • Scan images for vulnerabilities
  • Use secrets management for sensitive data

Performance

  • Use multi-stage builds to reduce image size
  • Leverage build cache effectively
  • Choose appropriate base images
  • Set resource limits for containers
  • Use health checks for containers

Maintenance

  • Regularly clean up unused images and containers
  • Monitor disk usage
  • Set up log rotation
  • Backup important data volumes
  • Keep Docker updated

Troubleshooting Common Issues

Permission Denied Error

# Add user to docker group
sudo usermod -aG docker $USER
newgrp docker

Cannot Connect to Docker Daemon

# Start Docker service
sudo systemctl start docker

# Check service status
sudo systemctl status docker

Disk Space Issues

# Clean up system
docker system prune -a

# Remove unused volumes
docker volume prune

Container Won’t Start

# Check container logs
docker logs container_name

# Inspect container configuration
docker inspect container_name

Useful Docker Tools

Portainer (Web-based GUI)

docker run -d -p 9000:9000 --name portainer 
  --restart=always 
  -v /var/run/docker.sock:/var/run/docker.sock 
  -v portainer_data:/data 
  portainer/portainer-ce

Docker Desktop Alternative

For GUI management, consider:

  • Portainer (web-based)
  • Lazydocker (terminal-based)
  • Dockstation (desktop app)

Next Steps

  1. Learn Dockerfile creation
  2. Explore Docker networking
  3. Study Docker volumes and data persistence
  4. Practice with Docker Swarm for orchestration
  5. Consider Kubernetes for advanced orchestration

Conclusion

Docker installation on Ubuntu is straightforward and opens up powerful containerization capabilities. Start with simple containers and gradually explore advanced features like multi-container applications, custom images, and orchestration.

Remember to follow security best practices and regularly maintain your Docker environment for optimal performance and security.

Add Comment