Intoxicated Penguin
Intoxicated Penguin

Reputation: 364

Wait for specific container when using docker compose up -d

I am trying to use docker to manage the behind the scenes stuff for testing an application, where a few things need to run: mongo, redis, localstack, localstack data initialization and then the application. I am running the application outside of the docker, natively. To do this I am running docker compose up -d && node server.js, but the problem is that I can't do it attached or the node command will never run, and if I do it detached the data initialization (setup-resources) won't be finished before the node application runs.

My docker compose:

services:
    mongo:
        image: mongo
        ports:
            - "27017:27017"
    redis:
        image: redis
        ports:
            - "6379:6379"
        depends_on:
            - setup-resources
    localstack:
        image: localstack/localstack
        ports:
            - "4566:4566"
        volumes:
            - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
            - "/var/run/docker.sock:/var/run/docker.sock"
    setup-resources:
        build: docker_dev/setup
        restart: "no"
        environment:
            - AWS_ACCESS_KEY_ID=hidden
            - AWS_SECRET_ACCESS_KEY=hidden
            - AWS_DEFAULT_REGION=hidden
            - ROOT_BUCKET
            - DPS_ARTIFACTS_ROOT_BUCKET
            - THUMBNAIL_BUCKET
        depends_on:
            - localstack    

Setup resources dockerfile:

FROM mesosphere/aws-cli
ENTRYPOINT []
ENV ROOT_BUCKET ""
CMD aws --endpoint-url=http://localstack:4566 s3 mb s3://$ROOT_BUCKET-stage

Upvotes: 8

Views: 8905

Answers (1)

Antonio
Antonio

Reputation: 91

You can use --wait flag (used along with -d flag) https://docs.docker.com/engine/reference/commandline/compose_up/

$ docker compose up -d --wait

It will wait until all the containers have the state RUNNING and HEALTHY before exiting (and continuing running in detached mode).

In case the container is marked as HEALTHY sooner than you need, you can define your own criteria to mark the container HEALTHY https://docs.docker.com/compose/compose-file/#healthcheck

services:
  grpc_wiremock:
    image: adven27/grpc-wiremock
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8888/__admin/mappings" ]
      interval: 10s
      timeout: 2s
      retries: 30
      start_period: 10s

Note that this will override the healthcheck from the image's Dockerfile if present.

Upvotes: 7

Related Questions