docker compose to delay container build and start

Viewed 2861

i have couple of container running in sequence.

i am using depends on to make sure the next one only starts after current one running.

i realize one of container has some cron job to be finished , so the next container has the proper data to be imported....

in this case, i cannot just rely on depends on parameter.

how do i delay the next container to starts? say wait for 5 minutes.

sample docker compose:

  test1:
    networks:
      - test
    image: test1
    ports:
      - "8115:8115"
    container_name: test1

  test2:
    networks:
      - test
    image: test2
    depends_on:
      - test1
    ports:
      - "8160:8160"
2 Answers

You can use entrypoint script, something like this (need to install netcat):

until nc -w 1 -z test1 8115; do
  >&2 echo "Service is unavailable - sleeping"
  sleep 1
done
sleep 2
>&2 echo "Service is up - executing command"

And execute it by command instruction in service (in docker-compose file) or in the Dockerfile (CMD directive).

I added this in the Dockerfile (since it was just for a quick test):

CMD sleep 60 && node server.js

A 60 seconds sleep did the trick, since the node.js part was executing before a database dump init script could finish executing fully.

Related