BLOG POSTS
How to Install and Use Docker on Ubuntu 24

How to Install and Use Docker on Ubuntu 24

Docker has become an essential tool for modern application development and deployment, offering lightweight containerization that consistently runs applications across different environments. This comprehensive guide will walk you through installing Docker on Ubuntu 24, configuring it properly, and getting hands-on experience with real-world containerization scenarios. You’ll learn not just the installation process, but also practical usage patterns, troubleshooting common issues, and best practices that’ll save you headaches down the road.

How Docker Works on Ubuntu

Docker operates as a client-server architecture where the Docker daemon (dockerd) manages containers, images, networks, and volumes. On Ubuntu 24, Docker integrates seamlessly with the systemd init system and leverages Linux kernel features like cgroups and namespaces for process isolation.

The Docker engine consists of three main components:

  • Docker daemon – Background service handling container operations
  • Docker CLI – Command-line interface for interacting with Docker
  • Docker API – RESTful API enabling programmatic container management

Ubuntu 24 ships with containerd as the default container runtime, which Docker uses under the hood. This setup provides better performance and stability compared to older Docker versions that used their own runtime.

Step-by-Step Docker Installation

Let’s get Docker installed properly on your Ubuntu 24 system. I’ll show you the official method that ensures you get the latest stable version with proper security updates.

Remove Old Docker Versions

First, clean up any existing Docker installations to avoid conflicts:

sudo apt-get remove docker docker-engine docker.io containerd runc

Set Up Docker’s Official Repository

Update your package index and install prerequisites:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release

Add Docker’s official GPG key:

sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Set up the 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

Update the package index and install Docker:

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

Verify the installation by running the hello-world container:

sudo docker run hello-world

Post-Installation Configuration

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

sudo usermod -aG docker $USER
newgrp docker

Configure Docker to start on boot:

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Essential Docker Commands and Usage

Now that Docker is installed, let’s explore the commands you’ll use daily. Here are the essential operations with practical examples:

Image Management

# Pull an image from Docker Hub
docker pull nginx:latest

# List local images
docker images

# Remove an image
docker rmi nginx:latest

# Build an image from Dockerfile
docker build -t myapp:v1.0 .

Container Operations

# Run a container interactively
docker run -it ubuntu:22.04 /bin/bash

# Run a container in detached mode
docker run -d -p 8080:80 --name webserver nginx

# List running containers
docker ps

# List all containers
docker ps -a

# Stop a container
docker stop webserver

# Remove a container
docker rm webserver

Real-World Example: Setting Up a Development Environment

Here’s a complete example of setting up a LAMP stack for development:

# Create a custom network
docker network create lamp-network

# Run MySQL container
docker run -d \
  --name mysql-db \
  --network lamp-network \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=myapp \
  -e MYSQL_USER=appuser \
  -e MYSQL_PASSWORD=apppass \
  -v mysql-data:/var/lib/mysql \
  mysql:8.0

# Run PHP-Apache container
docker run -d \
  --name php-apache \
  --network lamp-network \
  -p 8080:80 \
  -v $(pwd)/src:/var/www/html \
  php:8.1-apache

# Install MySQL extension in PHP container
docker exec php-apache docker-php-ext-install mysqli pdo pdo_mysql

Docker Compose for Multi-Container Applications

Docker Compose simplifies managing multi-container applications. Here’s the same LAMP stack using a docker-compose.yml file:

version: '3.8'

services:
  web:
    image: php:8.1-apache
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db
    networks:
      - lamp-network

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: myapp
      MYSQL_USER: appuser
      MYSQL_PASSWORD: apppass
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - lamp-network

  phpmyadmin:
    image: phpmyadmin:latest
    ports:
      - "8081:80"
    environment:
      PMA_HOST: db
      PMA_USER: appuser
      PMA_PASSWORD: apppass
    depends_on:
      - db
    networks:
      - lamp-network

volumes:
  mysql-data:

networks:
  lamp-network:

Run this stack with:

docker compose up -d

Performance Optimization and Best Practices

Docker performance on Ubuntu can be significantly improved with proper configuration. Here are the optimizations I’ve found most effective:

Storage Driver Configuration

Ubuntu 24 uses overlay2 by default, which is optimal for most use cases. Check your current driver:

docker info | grep "Storage Driver"

Memory and CPU Limits

Always set resource limits for production containers:

# Limit container to 512MB RAM and 0.5 CPU cores
docker run -d --memory=512m --cpus=0.5 nginx

Docker Performance Comparison

Metric Native Ubuntu Docker Container Performance Impact
CPU Performance 100% 95-98% 2-5% overhead
Memory Usage Baseline +50-100MB Minimal overhead
Network Throughput 100% 85-95% 5-15% overhead
Disk I/O 100% 90-95% 5-10% overhead

Common Issues and Troubleshooting

Here are the most frequent Docker problems on Ubuntu 24 and their solutions:

Permission Denied Errors

If you get permission denied when running Docker commands:

# Check if your user is in docker group
groups $USER

# If not, add user and restart session
sudo usermod -aG docker $USER
newgrp docker

Container Won’t Start

Debug container startup issues:

# Check container logs
docker logs container-name

# Run container interactively to debug
docker run -it --entrypoint /bin/bash image-name

Port Already in Use

Find and stop processes using required ports:

# Find process using port 8080
sudo netstat -tulpn | grep :8080

# Kill process by PID
sudo kill -9 PID

Disk Space Issues

Clean up Docker system resources:

# Remove unused containers, networks, images
docker system prune -a

# Remove specific unused volumes
docker volume prune

Real-World Use Cases

Docker excels in several practical scenarios that developers and sysadmins encounter regularly:

Microservices Development

When building microservices, Docker enables you to run multiple services locally with different technology stacks. For example, you might run a Node.js API, Python ML service, and Redis cache simultaneously without conflicts.

CI/CD Pipeline Integration

Docker containers provide consistent build environments across development, staging, and production. Your VPS can run the same container images that work on your local machine.

Database Testing

Spin up database instances quickly for testing:

# PostgreSQL for testing
docker run -d --name test-postgres \
  -e POSTGRES_PASSWORD=testpass \
  -e POSTGRES_DB=testdb \
  -p 5432:5432 \
  postgres:15

# Run tests
npm test

# Clean up
docker rm -f test-postgres

Docker vs Alternatives Comparison

Feature Docker Podman LXC/LXD Virtual Machines
Startup Time 1-3 seconds 1-3 seconds 2-5 seconds 30-60 seconds
Resource Usage Low Low Medium High
Security Model Daemon-based Rootless System containers Full isolation
Ecosystem Largest Docker compatible Limited Extensive
Learning Curve Moderate Easy if you know Docker Steep Moderate

Security Considerations

Running Docker securely on Ubuntu requires attention to several areas:

User Namespace Remapping

Configure Docker to use user namespaces for better security:

# Edit /etc/docker/daemon.json
{
  "userns-remap": "default"
}

# Restart Docker
sudo systemctl restart docker

Runtime Security

  • Never run containers as root in production
  • Use official images or build from minimal base images
  • Regularly update images to patch security vulnerabilities
  • Scan images for vulnerabilities using tools like docker scan

Network Security

Create custom networks instead of using the default bridge:

# Create isolated network
docker network create --driver bridge secure-network

# Run containers on custom network
docker run -d --network secure-network --name app1 nginx
docker run -d --network secure-network --name app2 mysql

Advanced Docker Features

Multi-Stage Builds

Reduce image size with multi-stage builds:

# Dockerfile
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

FROM node:18-alpine AS runtime
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Health Checks

Add health checks to monitor container status:

docker run -d \
  --name web-app \
  --health-cmd="curl -f http://localhost:3000/health || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  my-web-app

Resource Monitoring

Monitor container resource usage:

# Real-time stats
docker stats

# Detailed container inspection
docker inspect container-name

For production deployments on dedicated servers, consider implementing comprehensive monitoring with tools like Prometheus and Grafana to track Docker metrics alongside system performance.

Docker on Ubuntu 24 provides a robust foundation for modern application deployment and development workflows. The combination of Ubuntu’s stability and Docker’s flexibility creates an ideal environment for everything from simple development setups to complex microservices architectures. Remember to regularly update both your system and Docker installation to maintain security and access the latest features.

For more detailed information about Docker installation and configuration options, check the official Docker documentation for Ubuntu.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked