Complete Guide: Installing Docker on Ubuntu 24.04 LTS

Complete Guide: Installing Docker on Ubuntu 24.04 LTS

Docker has revolutionized application deployment by enabling consistent environments across different platforms. This comprehensive guide will walk you through installing Docker on Ubuntu 24.04 LTS, with expert tips and best practices.

Prerequisites

Before beginning the installation process, ensure you have:

  • A system running Ubuntu 24.04 LTS
  • A user with sudo privileges
  • Terminal access to your system
  • At least 4GB of RAM (recommended)
  • At least 20GB of disk space (recommended)

Installation Guide

1. System Preparation

First, ensure your system is up-to-date:

sudo apt update
sudo apt upgrade -y

Install essential dependencies:

sudo apt install -y \
    curl \
    apt-transport-https \
    ca-certificates \
    software-properties-common \
    gnupg

2. Docker Installation

There are two methods to install Docker: via Ubuntu’s default repository or Docker’s official repository. We recommend using Docker’s official repository for the latest stable version.

Setting up Docker’s Official Repository

  1. 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
  1. Add the Docker repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Update package index with the new repository:
sudo apt update
  1. Install Docker CE (Community Edition):
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

3. Post-Installation Steps

Configure User Permissions

Add your user to the docker group to run Docker commands without sudo:

sudo usermod -aG docker $USER
newgrp docker

Verify Installation

  1. Check Docker service status:
sudo systemctl status docker

  1. Run a test container:
docker run hello-world

Configure Docker to Start on Boot

Enable Docker to start automatically on system boot:

sudo systemctl enable docker

4. Essential Docker Commands

Here are some fundamental Docker commands to get you started:

  • List images: docker images
  • Pull an image: docker pull <image-name>
  • Run a container: docker run -d -p <host-port>:<container-port> <image-name>
  • List running containers: docker ps
  • List all containers: docker ps -a
  • Stop a container: docker stop <container-id>
  • Remove a container: docker rm <container-id>
  • Remove an image: docker rmi <image-name>

5. Best Practices and Expert Tips

  1. Security Considerations:

    • Regularly update Docker with sudo apt update && sudo apt upgrade
    • Use official images from Docker Hub
    • Scan images for vulnerabilities using docker scan <image-name>
    • Avoid running containers with --privileged flag
  2. Performance Optimization:

    • Use multi-stage builds to reduce image size
    • Implement resource limits using --memory and --cpu flags
    • Clean unused resources regularly with docker system prune
  3. Troubleshooting Tips:

    • Check Docker logs: sudo journalctl -fu docker
    • Inspect container logs: docker logs <container-id>
    • Monitor resource usage: docker stats

6. Uninstalling Docker

If you need to remove Docker completely:

# Stop all running containers
docker stop $(docker ps -aq)

# Remove all containers
docker rm $(docker ps -aq)

# Remove all images
docker rmi $(docker images -q)

# Uninstall Docker packages
sudo apt purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Remove Docker directories
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Common Issues and Solutions

  1. Permission Denied

    • Solution: Run newgrp docker or log out and back in after adding user to docker group
  2. Docker Daemon Not Starting

    • Solution: Check system logs with journalctl -xe
    • Verify daemon configuration in /etc/docker/daemon.json
  3. Network Issues

    • Solution: Check Docker network settings with docker network ls
    • Restart Docker network with sudo systemctl restart docker

Advanced Configuration

Docker Daemon Configuration

Create or modify /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "default-address-pools": [
    {
      "base": "172.17.0.0/16",
      "size": 24
    }
  ]
}

This configuration:

  • Limits log file size to 10MB
  • Keeps maximum 3 log files
  • Sets default IP address range for container networks

Remember to restart Docker after making changes:

sudo systemctl restart docker

Conclusion

Docker is a powerful tool for containerization, and proper installation is crucial for optimal performance. Keep your Docker installation updated and regularly check for security updates. For production environments, consider implementing monitoring solutions and establishing backup procedures for your containers and images.

For more advanced usage, explore Docker Compose and Docker Swarm for container orchestration, and consider implementing CI/CD pipelines with Docker.