Skip to content
artyombPublic

About

Dry Docker stack definition

Resources

Stars

1 star

Watchers

2 watching

Forks

Repository files navigation

Dry-stack

This gem allows ...

cat simple_stack.drs | dry-stack -e to_compose | docker stack deploy -c - simple_stack

$ dry-stack
Version: 0.1.63
Usage:
	dry-stack -s stackfile [options] COMMAND
	cat stackfile | dry-stack COMMAND
	dry-stack COMMAND < stack.drs

Commands:
     to_compose -  Print stack in docker compose format
                   [... to_compose <deploy name>] 
     swarm_deploy -  Call docker stack deploy & add config readme w/ description
                     [... swarm_deploy -- --prune  --resolve-image changed]

Options:
    -s, --stack STACK_NAME           Stack file
    -e, --env                        Load .env file
        --name STACK_NAME            Define stack name
        --ingress                    Generate ingress labels
        --traefik                    Generate traefik labels
        --traefik-tls                Generate traefik tls labels
        --tls-domain domain          Domain for the traefik labels
        --host-sed /from/to/         Sed ingress host  /\*/dev.*/
    -n, --no-env                     Deprecated
    -c, --configuration name         Configuration name
    -x, --context-endpoint host      Docker context host. Created if not exists
    -h, --help

https://rdoc.info/gems/dry-stack https://rubydoc.info/gems/dry-stack https://gemdocs.org/gems/dry-stack/

Installation

To install the gem

$ gem install dry-stack

Usage

Create the file stack.drs which describes the stack

Description <<~DSC
  Stack description
DSC

Labels 'stack.product': 'product A'

PublishPorts admin: 5000
Ingress admin: { host: 'admin.*' }
Deploy admin: { replica: 2, 'resources.limits': { cpus: '4', memory: '500M' } }

Service :admin,     image: 'frontend', env: {APP: 'admin'},     ports: 5000

Service :backend,   image: 'backend', ports: 3000 do
  env APP_PORT: 3000, NODE_ENV: 'development', SKIP_GZ: true, DB_URL: '$DB_URL'
  volume 'database:/var/lib/postgresql/data'
end

Volume :database, driver: 'zfs', name: 'tank/volume1', driver_opts: { compression: 'lz4', dedup: 'on' }

Use ENV!["KEY"] inside stack files as a shortcut for ENV.fetch("KEY"). Pass a second value to provide a default: ENV!["KEY", "default value"].

Then run in the current directory

$ dry-stack stack.drs -n --traefik to_compose

This will ...

version: '3.8'
services:
  admin:
    environment:
      APP: admin
      STACK_NAME: stack
      STACK_SERVICE_NAME: admin
    deploy:
      labels:
      - stack.product=product A
      - traefik.enable=true
      - traefik.http.routers.stack_admin-0.service=stack_admin-0
      - traefik.http.services.stack_admin-0.loadbalancer.server.port=5000
      - traefik.http.routers.stack_admin-0.rule=HostRegexp(`{name:admin\..*}`)
      replica: 2
      resources:
        limits:
          cpus: '4'
          memory: 500M
    networks:
      default:
      ingress_routing:
    init: true
    image: frontend
    ports:
    - 5000:5000
  backend:
    environment:
      APP_PORT: 3000
      NODE_ENV: development
      SKIP_GZ: 'true'
      DB_URL: "$DB_URL"
      STACK_NAME: stack
      STACK_SERVICE_NAME: backend
    deploy:
      labels:
      - stack.product=product A
    init: true
    image: backend
    volumes:
    - database:/var/lib/postgresql/data
volumes:
  database:
    driver: zfs
    name: tank/volume1
    driver_opts:
      compression: lz4
      dedup: 'on'
networks:
  ingress_routing:
    external: true
    name: ingress-routing

Mount an HTTP application under a path

Use mount: when an application serves its own root but should appear under a public URL prefix:

Options name: 'messaging', traefik: true

Ingress nui: {
  mount: '/map/nats',
  port: 31311,
  priority: 100,
  oauth_provider: 'oauth2_oauth2_slim:7000/auth'
}

Service :nui, image: 'ghcr.io/nats-nui/nui:0.10.0'

The same option works in a service's ingress block or ingress: options. /map/nats and /map/nats/ are equivalent mount declarations. After configured OAuth and BasicAuth middleware, the generated route:

  • Redirects /map/nats?x=1 to /map/nats/?x=1 with HTTP 302, preserving the request's scheme and authority.
  • Forwards /map/nats/ as / and /map/nats/api as /api, using Traefik StripPrefix middleware.
  • Excludes neighboring paths such as /map/natsextra.

One router uses the existing host/client restrictions, priority and TLS options. Global HTTP-to-HTTPS redirects remain the ingress proxy's responsibility. The application must support a URL prefix or relative asset/API URLs; mount: does not rewrite response bodies, cookies or application redirects.

Mount paths must be literal ASCII absolute paths with nonempty segments other than . or ... Root /, percent encoding, query strings, fragments and commas are not supported. mount: cannot be combined with path, path_sub, path_regexp or TCP host_sni on the same ingress rule. Existing ingress rules without mount: retain their behavior.

Choose the backend prefix

Use target_prefix: to select the path prefix received by the application:

Ingress options Incoming path Backend path
mount: '/public' /public/api /api
mount: '/public', target_prefix: '/internal' /public/api /internal/api
mount: '/app', target_prefix: '/app' /app/api /app/api
host: 'app.*', target_prefix: '/internal' /api /internal/api

Matching prefixes generate no path-rewriting middleware. Otherwise, dry-stack strips the mount and adds the target prefix as needed. Without a mount, the public route root is / and the target prefix is added to the complete path. target_prefix: '/' selects the backend root. Trailing slashes on prefixes are normalized; existing declarations without target_prefix: keep their behavior.

Redirect the route root

redirect_root: selects an absolute public URL path for an HTTP 302 redirect. It replaces the mount's default trailing-slash redirect and matches both forms of the mount root. Without a mount, it matches only /:

ingress mount: '/app', target_prefix: '/app', redirect_root: '/app/home/'

Both /app?x=1 and /app/?x=1 redirect to /app/home/?x=1. Child paths are unaffected. The scheme, authority and query string are preserved, and the destination's trailing slash is kept exactly as declared. Authentication runs before redirects, which run before prefix rewriting. A destination equal to the route root, with or without a trailing slash, is rejected to prevent loops.

Both options accept the same literal path characters as mount: and also allow /. They cannot combine with path, path_sub, path_regexp or TCP host_sni.

Route to an existing Traefik service

service: selects an existing HTTP service, optionally qualified with its provider, such as api@internal or shared@file. Names may contain ASCII letters, digits, underscores, dots and hyphens. The option cannot combine with an ingress port, url or TCP host_sni; container ports are not inferred for these routes. Other routers on the same container retain normal port inference.

When all HTTP routes use existing services and no HTTP service labels are already declared, dry-stack emits one unused service with port 9999 to satisfy Traefik's Docker/Swarm port detection. This does not publish a port or send requests to it; each router explicitly selects its declared service.

For a Traefik dashboard configured with --api.basePath=/traefik:

Options traefik: true

dashboard = {
  service: 'api@internal', target_prefix: '/traefik',
  redirect_root: '/traefik/dashboard/'
}

Service :traefik, image: 'traefik', oauth_provider: 'oauth2_oauth2_slim:7000/auth' do
  # Merge these ingress declarations into the existing Traefik service.
  ingress(**dashboard, mount: '/traefik', priority: 100)
  ingress(**dashboard, host: 'traefik.*', priority: 99)
end

The hostname route adds /traefik; the higher-priority mounted route preserves it. Keep OAuth's own routes above these priorities. The existing Traefik service must enable the API/dashboard and Swarm provider; do not use api.insecure with this base path. These ingress declarations enable provider discovery and join ingress-routing automatically, so remove any explicit traefik.enable=false label when replacing file-provider dashboard routes.

About

Dry Docker stack definition

Resources

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages