Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag to indicate a service completes for docker compose up --wait #11774

Open
robinovitch61 opened this issue Apr 27, 2024 · 0 comments
Open

Comments

@robinovitch61
Copy link

robinovitch61 commented Apr 27, 2024

Description

Say you have a docker compose file that has 2 services:

  1. Stand up a database container
  2. Run migrations against that database container
services:
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_USER: leo
      POSTGRES_PASSWORD: 123
      POSTGRES_DB: db
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 1s

  postgres-migrations:
    build:
      context: .
      dockerfile: Dockerfile-goose
    volumes:
      - ./migrations:/migrations
    depends_on:
      postgres:
        condition: service_healthy
    command: >
      sh -c " goose -dir /migrations postgres 'host=postgres user=leo password=123 dbname=db' up && touch /tmp/done &&
      sleep 2"
    healthcheck:
      test: ["CMD", "test", "-f", "/tmp/done"]
      interval: 1s

And a test script to run some integration tests after the migrations have been run on the db:

#!/usr/bin/env sh

set -e

docker compose up --wait
echo "docker compose up complete"

# the "integration tests"
PGPASSWORD=123 psql -h localhost -p 5432 -U leo -d db -c "SELECT * FROM movies" || echo "failed"

# clean up
docker compose down

Dockerfile-goose

FROM golang:alpine as builder
RUN apk add --no-cache git
RUN go install github.com/pressly/goose/v3/cmd/goose@latest

migrations/20240221040043_run.sql

-- +goose Up
-- +goose StatementBegin
SELECT pg_sleep(2);  -- simulate migrations taking longer than they do
CREATE TABLE movies (
                        id SERIAL PRIMARY KEY,
                        title VARCHAR(255) NOT NULL
);
INSERT INTO movies (title) VALUES ('Woohoo');
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS movies;
-- +goose StatementEnd

Notice how the docker compose file has to do some acrobatics with a "done" file flag. This is because docker compose up --wait does the following: Wait for services to be running|healthy. Implies detached mode.

The done file flag is used to make the migrations service only report healthy when the migrations are complete, so up --wait blocks until then and exits successfully so the test script continues.

Feature Request

It would be nice if you could indicate to up --wait that certain services will complete and up --wait should exit successfully if they do so, maybe with a new annotation in the compose file like:

services:
  postgres-migrations:
    will_complete: true

That way the test script can remain agnostic to the names of particular services or containers and the compose file doesn't have to implement the "done" file creation and healthcheck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant