Docker Hub and Docker Compose

Docker Hub

A public (or private) cloud registry of Docker images.

Used for searching and sharing ready-made images.

Address: https://hub.docker.com

How to Choose the Right Image?

When selecting an image, look for:

  • Official Image
  • Number of downloads
  • Date of last update

How to Read Documentation on Docker Hub?

Each image has:

  1. Overview: documentation about the image's content and usage
  2. Tags
  3. Dockerfile
  4. Other resources

Python Container Image

On Docker Hub we can find an image with Python:

docker.io/library/python:3.12-slim

Breakdown:

Part Meaning
docker.io registry (Docker Hub)
library namespace (official images)
python image name
3.12-slim tag (version + variant)

How to Navigate Tags?

On the Python image page we can find e.g.:

  • 3.12
  • 3.12-slim
  • 3.12-alpine
  • 3.12-bookworm
  • latest

🔎 Meaning of Tags:

Tag Meaning
3.12 full version
slim smaller version (fewer packages)
alpine very small image (Alpine Linux)
bookworm Debian version
latest latest stable version

How to Read a Dockerfile?

On the image's GitHub page we can find the Dockerfile.

Typical Dockerfile:

FROM debian:bookworm-slim

ENV PYTHON_VERSION 3.12.2

RUN apt-get update && apt-get install -y \
    build-essential \
    libssl-dev

CMD ["python3"]

What We Look For:

  • FROM → base image
  • RUN → what is installed
  • ENV → environment variables
  • CMD → default command

This tells us:

  • what OS it is based on
  • what packages it contains
  • whether it is secure

Docker Compose

A tool for configuring multi-container applications.

Microservices

Web applications typically consist of multiple services:

  • The application is divided into components that communicate with each other.
  • Better scaling
  • Simpler development

Components of a Web Application

    HTML
     |
+----------+
| Frontend |
+----------+
      | REST
+----------+  NFS  +----------+
|  Backend |-------| Storage  |
+----------+       +----------+
      | postgres
+----------+
| Database |
+----------+

A typical web application consists of multiple components. Each component has different requirements.

E.g.

  1. SQL Database, accessible only from the internal network for application needs
  2. Backend: Implements the REST interface and application logic.
  3. Frontend: JS Single Page Application, communicates with the BACKEND via the REST interface.

What is Docker Compose?

Docker Compose allows you to:

  • Define the entire application in a single YAML file
  • Start everything with one command
  • Easily manage networks, volumes, and configuration
  • Reproducible environment (dev/test/prod)

Virtual Environment Configuration

Uses a YAML file:

docker-compose.yml

docker-compose.yaml

services:
  service_name:
    image: image_name
    ports:
      - "host_port:container_port"
    volumes:
      - volume_name:/path/in/container
    environment:
      VARIABLE: value
    depends_on:
      - other_service

volumes:
  volume_name:

Simple Virtual Environment Startup

Start the application:

docker compose up -d

Stop it:

docker compose down

Structure of compose.yaml

  • services: containers, image, ports, environment
  • networks: connectivity between services
  • volumes: persistent data
  • secrets/configs: configuration without baking into the image

Docker Service

  • One DNS name
  • One or more containers

Service Configuration

  • image – Docker image
  • ports – port mapping
  • environment – environment variables
  • volumes – persistent storage
  • networks – networks
  • cmd: how to start

Docker Named Volume

  • Named volumes for mapping, managed by Docker

Where is it physically located?

On Linux, typically:

/var/lib/docker/volumes/

Docker Network

Custom networks between containers.

projectname_default

Within it:

  • Each container has an internal IP
  • Each service has a DNS record

Example: Deploying WordPress with Docker Compose

WordPress requires:

  • PHP + Apache (wordpress image)
  • MySQL database

Deployment using docker run

docker network create wordpress_net
docker volume create db_data
docker volume create wordpress_data
docker run -d \
  --name wordpress_db \
  --restart always \
  --network wordpress_net \
  -v db_data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=example \
  -e MYSQL_DATABASE=wordpress \
  -e MYSQL_USER=wpuser \
  -e MYSQL_PASSWORD=wppassword \
  mysql:8.0
docker run -d \
  --name wordpress_app \
  --restart always \
  --network wordpress_net \
  -p 8080:80 \
  -v wordpress_data:/var/www/html \
  -e WORDPRESS_DB_HOST=wordpress_db:3306 \
  -e WORDPRESS_DB_USER=wpuser \
  -e WORDPRESS_DB_PASSWORD=wppassword \
  -e WORDPRESS_DB_NAME=wordpress \
  wordpress:latest

Deploying WordPress with Compose

docker-compose.yml for WordPress

services:
  db:
    image: mysql:8.0
    container_name: wordpress_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wpuser
      MYSQL_PASSWORD: wppassword
    volumes:
      - db_data:/var/lib/mysql
  wordpress:
    image: wordpress:latest
    container_name: wordpress_app
    restart: always
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wpuser
      WORDPRESS_DB_PASSWORD: wppassword
      WORDPRESS_DB_NAME: wordpress
    depends_on:
      - db
    volumes:
      - wordpress_data:/var/www/html
volumes:
  db_data:
  wordpress_data:

Healthcheck

depends_on only waits for the container to start, not for when the database is ready.

Solution – healthcheck condition:

services:
  db:
    image: mysql:8.0
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

  wordpress:
    depends_on:
      db:
        condition: service_healthy

WordPress will only start once MySQL successfully responds to the ping.

Docker Swarm Mode

Docker's native tool for orchestrating containers in a cluster of multiple servers.

Allows:

  • managing multiple nodes as a single unit
  • high availability (High Availability)
  • horizontal scaling of services
  • rolling updates without downtime
  • automatic placement of containers

Docker Compose

  • Defining and running multi-container applications
  • Typically a local development environment
  • Single node (single Docker Engine)

Docker Swarm

  • Container orchestration in a multi-node cluster
  • Production environment
  • High availability and scaling

Docker Swarm Mode

  • One or more compute nodes
  • Connected by a network
  • Manager Node
  • Worker Node

Docker Swarm Mode

        Internet

   +----------------+  +---------+  +---------+
   | Leader Manager |  | Manager |  | Manager |
   +----------------+  +---------+  +---------+

   +--------+  +--------+  +--------+
   | Worker |  | Worker |  | Worker |
   +--------+  +--------+  +--------+

   +-----+     +-----+
   | NAS |     | SAN |
   +-----+     +-----+

Docker Swarm Mode

  • Service resilience against compute node failure.
  • Scaling – adding nodes as needed.
  • Upgrade or update compute nodes without service interruption.

Manager Node

  • Maintains cluster state
  • Allocates resources
  • Maintains information about services and assigns DNS names.
  • RAFT algorithm for electing the "leader" node.

Worker Node

  • Starts and stops containers
Reload?