Docker Config Reference (docker stack / docker start / docker stats)

We go over numerous examples of the docker stack / docker stats and docker start commands. We push out examples of the docker-config.yml when using docker stack!

Docker Config Reference (docker stack / docker start / docker stats)
docker stack / docker start / docker docker stats examples!

Examples of the Docker Stack Command

The docker stack command manages stacks in Docker Swarm mode, which are collections of services defined in a Compose file for orchestrated deployments. It supports operations such as deployment, listing, removal, and inspection, facilitating multi-service application management across clusters.

docker stack deploy -c docker-compose.yml mystack

This command deploys a stack named "mystack" using the configuration from "docker-compose.yml". It interprets the YAML file to create and schedule services, networks, and volumes in the Swarm cluster, ensuring the defined application topology is realized with automatic load balancing and scaling.

Docker Stack docker-compose.yml examples


Example 1: Basic Web Application Stack with Database and Caching

This docker-compose.yml defines a stack comprising an Nginx web server for HTTP handling, a MySQL database for data persistence, and a Redis cache for performance optimization. It includes basic networking and volume configurations for data durability. This setup is suitable for a simple web application requiring fast data access and storage. To deploy, use docker stack deploy -c docker-compose.yml webstack.

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - appnet

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: appdb
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - appnet

  cache:
    image: redis:7.0
    ports:
      - "6379:6379"
    networks:
      - appnet

volumes:
  dbdata:

networks:
  appnet:
    driver: overlay

Example 2: Secure API Stack with Authentication and Logging

This configuration creates a stack with an Apache HTTP server for API endpoints, an OpenSSH server for secure remote access, and an Elasticsearch instance for logging. It emphasizes security through environment variables and isolated networking. Volumes ensure log persistence. Deploy via docker stack deploy -c docker-compose.yml apistack.

version: '3.8'

services:
  api:
    image: httpd:2.4
    ports:
      - "8080:80"
    networks:
      - securenet

  ssh:
    image: linuxserver/openssh-server:latest
    environment:
      PASSWORD_ACCESS: true
      USER_PASSWORD: securepass
    ports:
      - "2222:22"
    networks:
      - securenet

  logging:
    image: elasticsearch:8.0
    environment:
      discovery.type: single-node
      xpack.security.enabled: false
    volumes:
      - logdata:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
    networks:
      - securenet

volumes:
  logdata:

networks:
  securenet:
    driver: overlay

Example 3: Data Processing Stack with Queue and Storage

This docker-compose.yml outlines a stack featuring a RabbitMQ message queue for task distribution, a MongoDB database for NoSQL storage, and a MinIO object storage server for file handling. It includes health checks for reliability. This is ideal for data pipelines. Use docker stack deploy -c docker-compose.yml datastack for deployment.

version: '3.8'

services:
  queue:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - datanet
    healthcheck:
      test: ["CMD", "rabbitmqctl", "status"]
      interval: 30s
      timeout: 10s
      retries: 5

  db:
    image: mongo:5.0
    ports:
      - "27017:27017"
    volumes:
      - mongodata:/data/db
    networks:
      - datanet

  storage:
    image: minio/minio:latest
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9000:9000"
      - "9001:9001"
    command: server /data --console-address ":9001"
    volumes:
      - miniodata:/data
    networks:
      - datanet

volumes:
  mongodata:
  miniodata:

networks:
  datanet:
    driver: overlay

Example 4: Monitoring Stack with Metrics and Alerting

This example defines a stack with Prometheus for metrics collection, Grafana for visualization (HTTP-based dashboard), and Alertmanager for notifications. It incorporates persistent storage for metrics data and custom networking. Suitable for observability in microservices. Deploy with docker stack deploy -c docker-compose.yml monitorstack.

version: '3.8'

services:
  metrics:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - promdata:/prometheus
    networks:
      - monitornet

  dashboard:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    environment:
      GF_SECURITY_ADMIN_PASSWORD: adminpass
    networks:
      - monitornet

  alerting:
    image: prom/alertmanager:latest
    ports:
      - "9093:9093"
    networks:
      - monitornet

volumes:
  promdata:

networks:
  monitornet:
    driver: overlay

Example 5: Development Stack with API, Cache, and Remote Access

This docker-compose.yml sets up a stack including a Node.js API server (for HTTP endpoints), a Memcached cache, and an SFTP server for file transfers (SSH-based). It uses environment variables for configuration and volumes for data sharing. Ideal for development workflows. Execute docker stack deploy -c docker-compose.yml devstack to deploy.

version: '3.8'

services:
  api:
    image: node:18
    command: node app.js
    ports:
      - "3000:3000"
    volumes:
      - ./app:/app
    networks:
      - devnet

  cache:
    image: memcached:latest
    ports:
      - "11211:11211"
    networks:
      - devnet

  sftp:
    image: atmoz/sftp:latest
    ports:
      - "2223:22"
    command: user:pass:1001
    volumes:
      - sftpdata:/home/user/upload
    networks:
      - devnet

volumes:
  sftpdata:

networks:
  devnet:
    driver: overlay
docker stack ls

This command lists all deployed stacks in the Swarm cluster, displaying details such as stack name, number of services, orchestrator, and creation timestamp. It provides an overview of active multi-service applications, aiding in cluster-wide management and monitoring.

docker stack rm mystack

This command removes the stack "mystack" from the Swarm cluster, terminating all associated services, networks, and volumes. It ensures orderly cleanup, preventing resource leaks while allowing for redeployment or reconfiguration.

docker stack services mystack

This command lists the services within the "mystack" stack, including service ID, name, mode, replicas, image, and ports. It enables detailed inspection of stack composition, supporting troubleshooting and scaling decisions.

docker stack ps mystack

This command lists the tasks (container instances) running for the "mystack" stack, detailing node assignment, task ID, name, image, and status. It facilitates analysis of task distribution and health across Swarm nodes.

With the --prune flag, this command updates "mystack" using "updated.yml" and removes services no longer defined in the file. It streamlines stack evolution, ensuring alignment with the latest configuration while minimizing obsolete components.

docker stack deploy -c compose.yml --with-registry-auth mystack

This command deploys "mystack" from "compose.yml" and includes the --with-registry-auth flag to pass authentication credentials to nodes for private image pulls. It enhances security in environments with protected registries.

docker stack ps --filter desired-state=running mystack

This command lists tasks in "mystack" filtered by the desired state of "running". It isolates active tasks for focused monitoring, useful in identifying operational components amid updates or failures.

docker stack services --format "{{.Name}} {{.Replicas}}" mystack

This command lists services in "mystack" with a custom format showing name and replicas. It provides concise scalability insights, supporting quick assessments in automated reporting.

docker stack rm --help

This command displays help documentation for the docker stack rm subcommand, including usage, options, and descriptions. It serves as a reference for understanding removal behaviors and flags.

Examples of the Docker Start Command

The docker start command initiates a stopped container, resuming its processes from the previous state. It preserves data and configurations, making it suitable for restarting services without recreation.

docker start mycontainer

This command starts the stopped container "mycontainer", reactivating its entrypoint or command. It restores the container to a running state, maintaining mounted volumes and network connections for seamless continuation.

docker start -a mycontainer

With the -a or --attach flag, this command starts "mycontainer" and attaches the console to its output. It enables real-time log viewing, useful for interactive applications or immediate debugging.

docker start -i interactivecontainer

The -i or --interactive flag starts "interactivecontainer" and attaches an interactive session. It supports user input, ideal for shell-based containers requiring manual intervention post-restart.

docker start container1 container2

This command starts multiple stopped containers, "container1" and "container2", simultaneously. It facilitates batch resumption, efficient for grouped services in multi-container setups.

docker start --attach dbcontainer

This command starts "dbcontainer" and attaches to its output. It allows monitoring database startup logs, ensuring successful initialization in persistent storage scenarios.

docker start -ai shellcontainer

Combining -a and -i, this command starts "shellcontainer" with attached interactive mode. It provides a terminal session, supporting exploratory or administrative tasks.

docker start worker

This command starts the stopped "worker" container, resuming background processing. It is applicable in queue systems where tasks need reactivation without reconfiguration.

docker start --attach webapp

This command starts "webapp" and attaches to view startup output. It aids in verifying web server initialization, including port bindings and configuration loading.

docker start cachecontainer

This command starts "cachecontainer", such as for Redis, restoring caching services. It maintains in-memory data if configured with persistence, ensuring quick recovery.

docker start --help

This command displays help for docker start, detailing options and usage. It assists in understanding flags for tailored restarts.

Examples of the Docker Stats Command

The docker stats command monitors resource usage of running containers in real-time, displaying metrics like CPU, memory, network I/O, and block I/O. It supports live streaming for performance analysis.

docker stats

This command displays live statistics for all running containers, updating every second with usage data. It provides a dashboard-like view for system-wide resource monitoring.

docker stats mycontainer

This command shows stats specifically for "mycontainer". It focuses on individual performance, aiding in isolating resource-intensive processes.

docker stats --no-stream

With --no-stream, this command outputs a single snapshot of stats for all running containers. It is useful for one-time checks or scripting without continuous updates.

docker stats --format "{{.Name}} {{.CPUPerc}} {{.MemUsage}}"

This customizes output to show name, CPU percentage, and memory usage for running containers. It tailors data for specific metrics in reports.

docker stats container1 container2

This command monitors stats for "container1" and "container2". It enables comparative analysis of selected containers' resource consumption.

docker stats --all

The --all flag includes stats for all containers, including stopped ones (showing zero usage). It offers a complete host overview.

docker stats --format "table {{.ID}}\t{{.Name}}\t{{.NetIO}}"

This formats stats as a table with ID, name, and network I/O. It enhances readability for network performance tracking.

docker stats --no-trunc

This command displays stats without truncating long IDs or names. It ensures full identifier visibility in dense outputs.

docker stats -a --no-stream

Combining -a and --no-stream, this provides a snapshot of all containers' stats. It is suitable for archival or batch processing.

docker stats --help

This command shows help for docker stats, explaining options and formats. It serves as a reference for advanced usage.