Skip to content

Instantly share code, notes, and snippets.

@cqsd
Last active August 19, 2021 23:11
Show Gist options
  • Save cqsd/35c20921d348f645117ed5399df0e247 to your computer and use it in GitHub Desktop.
Save cqsd/35c20921d348f645117ed5399df0e247 to your computer and use it in GitHub Desktop.
howto: docker init containers

howto: docker init containers

what is it

a container that sets up for other containers. for example, you could populate a database with seed data and share it with your real db container through a volume

support was added in this PR but i haven't seen easily google-able docs yet (maybe im just bad at searching)

how to use it

here's some imaginary node service backed by postgres which needs to run knex migrations before starting for some reason. It waits for the postrges service to be ready, waits for migrations to complete, and only then starts the node service

version: "3.9"
services:
  server:
    build: .
    environment:
      POSTGRES_HOST: db
    depends_on:
      db:
        condition: service_healthy
      db_migrations:
        condition: service_completed_successfully
  db_migrations:
    build: .
    environment:
      POSTGRES_HOST: db
    depends_on:
      db:
        condition: service_healthy
    command: "yarn knex migrate:latest --knexfile src/knexfile.ts"
  db:
    image: postgres:alpine
    environment:
      POSTGRES_PASSWORD: postgres
    ports:
      - 5432:5432
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment