4

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
  • Docker compose doesn't wait until a container is 'ready' before starting its dependencies. It only waits until they're running. So the recommended approach is to write your programs so they can retry connections until their dependencies are ready. – Hans Kilian Jun 02 '21 at 13:35
  • Does this answer your question? [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) – Rajesh Paudel Jun 02 '21 at 15:07

1 Answers1

3

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.

Antonio
  • 51
  • 6