Aller au contenu

Docker compose

De Banane Atomic

Links

Description

CLI

Use -f /path/docker-compose.yml if you are not in the folder of docker-compose.yml file.
# give cpu and ram metrics by container
docker stats

# (re)create the containers and run the services in background
docker compose up -d
# stop the containers
docker compose down -v

# status of all services
docker compose ps

# logs de tous les services
docker compose logs --tail 50 -f
# --tail 50 seulement les 50 dernières lignes, sinon tous les logs depuis le démarrage du conteneur
# -f en temps réel
docker compose logs [service] --tail 50

# restart a service
docker compose restart [service]

# backup the mariadb database
docker compose exec [mariadb-service] mariadb-dump [dbname] > backup.sql

# connect to a pgsql database
docker compose exec [pgsql-service] psql -U [username] -d [dbname]

# reload the nginx configuration
docker compose exec [nginx-service] nginx -s reload

# execute a sql script
env $(cat .env | xargs) docker exec -i [container-name] psql -U $POSTGRES_USER -d $POSTGRES_DB < script.sql

# open a postgres shell
export $(grep -v '^#' .env | xargs) && docker compose exec [pgsql-service] psql -U $POSTGRES_USER -d $POSTGRES_DB

# run a sql script
export $(grep -v '^#' .env | xargs) && docker compose exec -T [pgsql-service] psql -U $POSTGRES_USER -d $POSTGRES_DB < script.sql

Update images

# update images and recreate the updated container for app service only
docker compose pull app
docker compose up -d app

# all in one command
docker compose up -d --pull always

# check before update
docker compose pull --dry-run

# check the logs
docker compose logs app --tail 30

# check the version
docker compose exec app app-exe --version

# delete unused images
docker image prune -f

Volume

# recreate a volume
docker compose down              # stop
docker volume rm my-volume       # remove the volume
docker compose up -d my-service  # start the service which will recreate the volume

Ports

# list the already used ports
sudo ss -tulpn

# the mapping docker - port
docker ps --format "table {{.Names}}\t{{.Ports}}"

Installation

Mediawiki FPM Alpine

  1. go to the mediawiki service url
    1. fill the installation form (database hostname: mariadb)
    2. it will create the database tables
    3. download the LocalSettings.php
  2. Install LocalSettings.php
    1. copy it into the service folder
    2. map the LocalSettings.php file by uncommenting the LocalSettings line in docker-compose.yml
  3. recreate the mediawiki container docker compose up -d
/srv/mediawiki/docker-compose.yml
services:

  # ─────────────────────────────────────────
  # MariaDB – persistent database
  # ─────────────────────────────────────────
  mariadb:
    image: mariadb:11
    container_name: mediawiki_db
    restart: unless-stopped
    environment:
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MARIADB_DATABASE: ${DB_NAME}
      MARIADB_USER: ${DB_USER}
      MARIADB_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - backend
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      start_period: 30s
      interval: 10s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────
  # MediaWiki (PHP-FPM)
  # ─────────────────────────────────────────
  mediawiki:
    image: mediawiki:1.45.1-fpm-alpine
    container_name: mediawiki_app
    restart: unless-stopped
    depends_on:
      mariadb:
        condition: service_healthy
    environment:
      # Used only as hints; real config is in LocalSettings.php
      MW_DB_TYPE: mysql
      MW_DB_SERVER: mariadb
      MW_DB_NAME: ${DB_NAME}
      MW_DB_USER: ${DB_USER}
      MW_DB_PASSWORD: ${DB_PASSWORD}
    volumes:
      - mw_webroot:/var/www/html
      # Mount LocalSettings.php after first-run wizard completes:
      # - ./LocalSettings.php:/var/www/html/LocalSettings.php:ro
      - ./opcache.ini:/usr/local/etc/php/conf.d/opcache.ini:ro
    networks:
      - backend
      - frontend

  # ─────────────────────────────────────────
  # Nginx – reverse proxy
  # ─────────────────────────────────────────
  nginx:
    image: nginx:stable-alpine
    container_name: mediawiki_nginx
    restart: unless-stopped
    depends_on:
      - mediawiki
    ports:
      - "80:80"
      # Uncomment when you add TLS:
      # - "443:443"
    volumes:
      - ./nginx/mediawiki.conf:/etc/nginx/conf.d/default.conf:ro
      # Needed for Nginx to serve /images directly (optional optimisation) and by PHP-FPM to try_files
      - mw_webroot:/var/www/html:ro
      # TLS certs (uncomment when ready):
      # - ./nginx/certs:/etc/nginx/certs:ro
    networks:
      - frontend

  # ─────────────────────────────────────────
  # Portainer CE – optional web UI
  # ─────────────────────────────────────────
  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    restart: unless-stopped
    ports:
      - "9000:9000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
    networks:
      - frontend

volumes:
  db_data:
  mw_webroot:
  portainer_data:

networks:
  backend:   # mariadb ↔ mediawiki only
  frontend:  # mediawiki ↔ nginx (+ portainer)
/srv/mediawiki/nginx/mediawiki.conf
server {
    listen 80;
    server_name _; # ! REPLACE by your server domain name !
    root /var/www/html;
    index index.php;

    client_max_body_size 50M;   # Allow larger file uploads

    # ! UNCOMMENT after installation !
    # Block access to sensitive paths
    #location ~ ^/(maintenance|mw-config)/ {
    #    deny all;
    #}
    location ~ /\. {
        deny all;
    }

    # Serve MediaWiki images directly from Nginx (faster)
    location /images {
        try_files $uri =404;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Cache-Control "public, max-age=86400";
    }

    # Long cache for static assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2)$ {
        try_files $uri =404;
        add_header Cache-Control "public, max-age=604800";
        expires 7d;
    }

    # All .php files → PHP-FPM
    location ~ \.php$ {
        try_files      $uri =404;
        fastcgi_pass   mediawiki:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_param  SERVER_NAME $host;
        fastcgi_read_timeout 120s;
    }

    # Everything else: try static file, then rewrite to index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}
/srv/mediawiki/.env
DB_ROOT_PASSWORD=xxx
DB_NAME=mediawiki
DB_USER=mediawiki
DB_PASSWORD=xxx