Back to Blog

Docker on Ubuntu: installation, logging and cgroup drivers

Complete tutorial on docker in Ubuntu. Learn official repos, daemon.json, systemd cgroups, log rotation.

Docker on Ubuntu: installation, logging and cgroup drivers

Docker on Ubuntu: Installation, Logging, and Cgroup Drivers

Introduction

Docker is a powerful platform for developing, shipping, and running applications in containers. Containers provide a lightweight, portable, and consistent way to deploy applications across different environments. Unlike traditional virtual machines, containers share the host OS kernel, making them more efficient in terms of resource utilization. This matters because it allows developers and system administrators to encapsulate applications with all dependencies, ensuring that they run seamlessly across various environments, from development to production.

In this tutorial, we will cover the installation of Docker on Ubuntu, configuring logging mechanisms, and understanding cgroup drivers. This knowledge is crucial for managing containerized applications effectively, ensuring performance, and optimizing resource usage. By the end of this tutorial, you will have a solid understanding of how to set up and manage Docker, along with best practices for logging and cgroup configurations.

Prerequisites

  • Ubuntu Version: This tutorial is compatible with Ubuntu 20.04 LTS and later.
  • Required Packages: You will need curl, apt-transport-https, ca-certificates, and software-properties-common.
  • Permissions: You must have sudo privileges to install packages and modify system files.
  • Risks: Incorrect configurations can lead to security vulnerabilities or performance issues. Always back up existing configurations before making changes.

Core Concepts

Terminology

  • Docker: An open-source platform for automating the deployment of applications in containers.
  • Container: An isolated environment where an application runs along with its dependencies.
  • Cgroups (Control Groups): A Linux kernel feature that limits, accounts for, and isolates resource usage of a collection of processes.
  • Daemon: The background service (Docker daemon) that manages containers and images.

Architecture

Docker employs a client-server architecture. The Docker client communicates with the Docker daemon, which handles the creation, execution, and management of containers.

When to Use

Use Docker when you need to:

  • Package applications and dependencies in a standardized unit.
  • Ensure consistency across development, testing, and production environments.
  • Optimize resource usage and scalability.

Limits

While Docker is powerful, it may not be suitable for every use case, such as running applications that require a full OS or when hardware isolation is crucial.

Syntax/Commands

Command Description
docker run [OPTIONS] IMAGE Run a container from a specified image.
docker ps List running containers.
docker logs [OPTIONS] CONTAINER Fetch logs from a container.
docker info Display system-wide information about Docker.
docker pull [OPTIONS] IMAGE Download an image from a registry.
docker rm [OPTIONS] CONTAINER Remove one or more containers.
docker rmi [OPTIONS] IMAGE Remove one or more images.

Practical Examples

1. Installing Docker

# Update package index
sudo apt update

# Install required packages
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add Docker's stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Update package index again
sudo apt update

# Install Docker
sudo apt install -y docker-ce

2. Starting and Enabling Docker

# Start Docker service
sudo systemctl start docker

# Enable Docker to start on boot
sudo systemctl enable docker

3. Running Your First Container

# Run a test container
sudo docker run hello-world

4. Checking Running Containers

# List running containers
sudo docker ps

5. Fetching Logs from a Container

# Run a container and fetch its logs
sudo docker run --name test-container -d nginx
sudo docker logs test-container

6. Configuring Docker Logging Driver

First, create or edit the daemon.json file:

# Open the daemon.json file for editing
sudo nano /etc/docker/daemon.json

Add the following content to set the default logging driver to json-file:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Then restart Docker:

# Restart Docker to apply changes
sudo systemctl restart docker

7. Viewing Docker Info

# Display Docker system-wide information
sudo docker info

8. Removing Unused Docker Objects

# Remove all stopped containers and unused images
sudo docker system prune -a

Real-World Scenarios

Scenario 1: CI/CD Pipeline

A common use case for Docker is in Continuous Integration/Continuous Deployment (CI/CD) pipelines. By containerizing your application, you can run tests in isolated environments, ensuring that your application behaves the same way regardless of where it is deployed.

Scenario 2: Microservices Architecture

In a microservices architecture, each service can run in its own container. This allows for easier scaling, as you can manage each service independently and deploy updates without affecting the entire application.

Scenario 3: Resource Management

Using cgroups, you can limit the resources allocated to Docker containers. For instance, if you're running multiple containers on a single host, you can set limits to ensure no single container consumes too much CPU or memory, leading to improved stability and performance.

Best Practices

  1. Use Official Docker Images: Always pull images from trusted sources to avoid security vulnerabilities. ✅
  2. Configure Log Rotation: Set up log rotation to prevent logs from consuming excessive disk space. 🔧
  3. Limit Resource Usage: Use cgroup configurations to limit CPU and memory allocation for containers. ⚠️
  4. Regularly Update Docker: Keep Docker up to date to benefit from security patches and new features. 🚀
  5. Use Docker Compose for Multi-Container Applications: Simplify the management of multi-container setups using Docker Compose. 💡

Common Errors

1. Error: "Cannot connect to the Docker daemon"

Cause: The Docker service may not be running.

Fix: Start the Docker service with sudo systemctl start docker.

2. Error: "No space left on device"

Cause: Disk space is exhausted due to uncleaned containers or images.

Fix: Use sudo docker system prune to remove unused containers and images.

3. Error: "Permission denied"

Cause: Insufficient permissions to run Docker commands.

Fix: Either run commands with sudo or add your user to the docker group.

4. Error: "Cgroup driver 'systemd' does not match the Docker daemon"

Cause: A mismatch between the cgroup driver used by the Docker daemon and the one used by the underlying system.

Fix: Set the cgroup driver in /etc/docker/daemon.json to match systemd:

{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

Then restart Docker.

Related Commands

Command Description
docker-compose Tool for defining and running multi-container applications.
docker network Manage Docker networks for container communication.
docker volume Manage Docker volumes for persistent data storage.
docker exec Run a command in a running container.

Automation Script

Here is a complete bash script to automate the Docker installation and logging configuration:

#!/bin/bash

# Check for root privileges
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root"
  exit 1
fi

# Update package index
apt update

# Install required packages
apt install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

# Add Docker's stable repository
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Install Docker
apt update
apt install -y docker-ce

# Start and enable Docker
systemctl start docker
systemctl enable docker

# Configure logging options
cat <<EOT >> /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
EOT

# Restart Docker to apply changes
systemctl restart docker

echo "Docker installation and configuration complete!"

Conclusion

In this tutorial, we have covered the installation of Docker on Ubuntu, how to manage logging configurations, and the use of cgroup drivers for resource management. Understanding these concepts is essential for effectively deploying and managing containerized applications.

Next Steps

  • Explore Docker Compose for managing multi-container applications.
  • Look into advanced logging solutions such as ELK Stack for better log analysis.
  • Consider integrating Docker with orchestration tools like Kubernetes for scaling your applications.

References