Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose to delay container build and start

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"
like image 391
user2201789 Avatar asked Oct 16 '25 16:10

user2201789


1 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).

like image 162
funnydman Avatar answered Oct 18 '25 07:10

funnydman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!