0

I was looking to set a initialization sequence for a docker-compose services to start. Like service 'web' should wait for 'db' to complete in docker-compose.

I guess previously 'depends_on' was used for this purpose but since version 3, 'depends_on' no longer imposes the docker services start-up sequence due to docker wants to start services at any moment when they fail independently. I guess that's a good reason to remove the previous waiting feature.

But, if that's true, what is the alternative for implementing initialization sequence where I don't want to start 'web' service until 'db' is fully initialization and completes the startup.

Please correct me if I'm wrong in my understanding about this topic.

Dev3100
  • 83
  • 1
  • 9
  • One way is to make the web service container wait until the db is available before fully starting up. For example a litte shell script can be used for that. – Henry Jul 23 '18 at 04:03
  • @Henry yes, I think that would be a good alternative if there is no out of the box docker option available – Dev3100 Jul 23 '18 at 17:43

1 Answers1

0

The depends on support in docker compose is a dependency only on start order, so it's not much use really.

The best way to do this is to share a volume between the dependent containers, and have the dependent container wait for a file on the shared volume to be touched by the other container before continuing with what it needs to do.

For example, imagine you are restoring a database on one container and don't want an app server to start in another container until the database is up:

In the dependent container wrap your entrypoint with a waiting entrypoint:

#!/bin/bash

echo "Waiting for the other container"

Here use inotify to wait for a touch on a shared volume file

echo "Other container is up"

source /entrypoint.sh

and at the end of the other container's entrypoint just touch the shared volume file

PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • Shared volume is a great idea to implement services to wait for each other. Thanks – Dev3100 Jul 23 '18 at 17:44
  • No problem. Bear in mind some distros of Linux don't support inotify, I ended up writing a file monitor in Java (PSif you think my answer is correct, any chance of marking it as such ?) – PaulNUK Jul 24 '18 at 07:49