Files
docker-workshop/06 docker-compose-uebungen-mit-loesung.md
Frank Woeckener e429c37f62 Initial Commit
2025-03-18 10:06:06 +01:00

6.8 KiB

10 Praktische Docker Compose Übungen

Übung 1: Einfaches Compose-File erstellen

Aufgabe: Erstelle ein docker-compose.yml für einen Webserver (Nginx) und stelle sicher, dass er auf Port 8080 erreichbar ist.

Lösung:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"

Übung 2: Multi-Service Anwendung

Aufgabe: Erstelle ein Compose-File für eine PHP-Anwendung mit Nginx und MySQL-Datenbank.

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html
  
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app
# ./nginx/default.conf
server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/html;

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Übung 3: Volume-Konfiguration

Aufgabe: Erweitere das Setup aus Übung 2 um ein benanntes Volume für die Datenbank.

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html
  
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:

Übung 4: Netzwerk-Konfiguration

Aufgabe: Erstelle eine Compose-Konfiguration mit zwei separaten Netzwerken: frontend (für Nginx und PHP-FPM) und backend (für PHP-FPM und Datenbank).

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - frontend
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html
    networks:
      - frontend
      - backend
  
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
    networks:
      - backend

networks:
  frontend:
  backend:
    internal: true

Übung 5: Umgebungsvariablen

Aufgabe: Erstelle ein Compose-File, das Umgebungsvariablen aus einer .env-Datei verwendet.

Lösung:

# docker-compose.yml
version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "${WEB_PORT:-8000}:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html
    environment:
      DB_HOST: db
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASS: ${DB_PASS}
  
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
# .env Datei
WEB_PORT=8000
DB_NAME=appdb
DB_USER=appuser
DB_PASS=secret
DB_ROOT_PASS=rootsecret

Übung 6: Service-Abhängigkeiten

Aufgabe: Konfiguriere ein Compose-Setup mit drei Services (Backend mit PHP-FPM und Nginx, Frontend, DB), wobei Backend von DB abhängt und Frontend von Backend.

Lösung:

version: '3'
services:
  frontend:
    image: node:16
    command: npm start
    ports:
      - "3000:3000"
    depends_on:
      - nginx
  
  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db
  
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app

Übung 7: Build-Konfiguration

Aufgabe: Erstelle ein Compose-File mit einem Service, der aus einem lokalen Dockerfile gebaut wird.

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    build:
      context: ./php
      dockerfile: Dockerfile
      args:
        - PHP_VERSION=8.1
    volumes:
      - ./src:/var/www/html
# ./php/Dockerfile
ARG PHP_VERSION=8.1
FROM php:${PHP_VERSION}-fpm

RUN apt-get update && apt-get install -y \
    libzip-dev \
    zip \
    && docker-php-ext-install zip pdo_mysql

WORKDIR /var/www/html

Übung 8: Service-Skalierung

Aufgabe: Konfiguriere ein Compose-Setup, bei dem der Nginx-Service auf 3 Instanzen skaliert werden kann.

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8080-8082:80"
    deploy:
      replicas: 3
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  php:
    image: php:8.1-fpm
    volumes:
      - ./src:/var/www/html

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root

Übung 9: Override-Konfiguration

Aufgabe: Erstelle eine Basis-Compose-Datei und eine Entwicklungs-Override-Datei.

Lösung:

# docker-compose.yml
version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "80:80"
    depends_on:
      - php
  
  php:
    image: php:8.1-fpm
# docker-compose.override.yml
version: '3'
services:
  nginx:
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    environment:
      - DEBUG=true
  
  php:
    volumes:
      - ./src:/var/www/html
    environment:
      - DEBUG=true

Übung 10: Debugging-Setup

Aufgabe: Erstelle ein Compose-Setup für PHP-FPM mit Nginx und Xdebug-Unterstützung.

Lösung:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  
  php:
    build:
      context: ./php
      dockerfile: Dockerfile.dev
    volumes:
      - ./src:/var/www/html
    environment:
      XDEBUG_CONFIG: client_host=host.docker.internal
      PHP_IDE_CONFIG: serverName=docker
    extra_hosts:
      - "host.docker.internal:host-gateway"
# ./php/Dockerfile.dev
FROM php:8.1-fpm

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
# ./php/xdebug.ini
zend_extension=xdebug
xdebug.mode=develop,debug
xdebug.start_with_request=yes
xdebug.client_port=9003