Skip to content

Docker Compose Options

ultimate docker-compose example

Check

YAML
services:
  service_name:
    extra_hosts:
      - "sub.somedomain.com:10.0.10.30"
    image: great/success:tag
    container_name: example
    hostname: example
    user: "${UID}:${GID}"
    environment:
      - JAVA_OPTS="-xms 1Gi"  # example - it can be anything
      - CUSTOM="data_here"
    # logging (rotate)
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
        compress: "true"
    dns:
      - "10.0.0.2"
      - "8.8.8.8"
    dns_search: example.com
    command: >
      /bin/bash -c "
      while ! nc -z cloud-config 8888; do
        echo \"Cloud-Config not available yet. Sleeping 5 sec.\";
        sleep 5;
      done;
      echo \"Connected to Cloud-Config!\";
      /usr/local/tomcat/bin/catalina.sh run
      "
    ---
    command: [ "bundle", "exec", "thin", "-p", "3000" ]
    # start after
    depends_on:
      - database
    ---
      database: # pick one
        condition: service_started
        condition: service_healthy
        condition: service_completed_successfully
    cap_add:
      - ALL
    cap_drop:
      - NET_ADMIN
      - SYS_ADMIN
    ---
    network_mode: "host"
    ---
    network_mode: "none"
    ---
    network_mode: "service:[service name]"
    networks: # cannot be used if network_mode is enabled
      - some-network
      - other-network
    ports:
      - "3000"
      - "3000-3005"
      - "8000:8000"
      - "9090-9091:8080-8081"
      - "49100:22"
      - "8000-9000:80"
      - "127.0.0.1:8001:8001"
      - "127.0.0.1:5000-5010:5000-5010"
      - "6060:6060/udp"
    restart: "no"
    restart: always
    restart: on-failure
    restart: on-failure:3
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 1m30s
      timeout: 10s
      retries: 3
      start_period: 40s
      start_interval: 5s
    ---
      test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]

loging

Reduce container size of logs (support compose & swarm)

YAML
   logging:
     driver: "json-file"
     options:
       max-size: "500m"
       max-file: "10"
       compress: "true"

dns

YAML
   dns:
     - "10.0.0.2"
     - "8.8.8.8"

commnds

Fire commands inside container

YAML
   command: >
     /bin/bash -c "
     while ! nc -z cloud-config 8888; do
       echo \"Cloud-Config not available yet. Sleeping 5 sec.\";
       sleep 5;
     done;
     echo \"Connected to Cloud-Config!\";
     /usr/local/tomcat/bin/catalina.sh run
     "

ports

YAML
   ports:
     - "8080:8080

extra_hosts

YAML
   extra_hosts:
     - "sub.somedomain.com:10.0.10.30"

Labels

YAML
    labels: [app=reporting]

Container Name

Give container a name (support compose & !swarm)

YAML
    container_name: somename

healthchecks

Zero Downtime Deploy

YAML
    healthcheck:
      test: ["CMD", "curl", "127.0.0.1:8080/api/something"]
    deploy:
      replicas: 1
      resources:
        reservations:
          memory: 768M
        limits:
          memory: 2048M
      update_config:
        order: start-first
        failure_action: rollback
        delay: 10s
      rollback_config:
        parallelism: 0
        order: stop-first
      restart_policy:
        condition: any
        delay: 5s
        max_attempts: 3
        window: 120s

depends on (healthcheck based on other service)

YAML
services:
  server:
    image: your_server_image
    depends_on:
      db:
        condition: service_healthy

Limit Resources

YAML
      resources:
        reservations:
          memory: 2048M
          cpus: '0.0001'
        limits:
          memory: 4096M
          cpus: '0.5'

User option

YAML
services:
  container_name: some-server
  image: some:img
  user: "${UID}:${GID}"  # or 1001:1001