archura
archura

Reputation: 1283

Docker container dependency

I have container A that depends on the data downloaded by container B.

To implement this dependency, I tried using depends_on in docker-compose and though it ensures that container B starts before A, the download might still be in progress.

Is there a way to specify a condition in the entrypoint of container A that it has to wait until the completion of download by container B?

Upvotes: 1

Views: 2968

Answers (2)

John
John

Reputation: 11831

This basically depends on what your execution environment is.

Kubernetes: If it is Kubernetes, you're in luck, what you describe is basically init container. A pod within Kubernetes can have multiple containers and init containers run to completion before the main application container(s) are started. Here is what the latest version of the docs state:

They run to completion before any app Containers start, whereas app Containers run in parallel, so Init Containers provide an easy way to block or delay the startup of app Containers until some set of preconditions are met.

Docker Compose: You're not so lucky. As your question states, depends_on simply means that one container is started before another, not that it completes before the other is started. If you want to achieve this, then you'll need to put the logic in yourself.

In this case, I would have the download container download whatever it is you want, then write to some known location.

e.g. within the download container's entrypoint script:

#!/bin/bash
curl -o /path/to/my/download http://example.com/download/path
touch /some/shared/file

and within the main container's entrypoint:

#!/bin/bash
while [ ! -f /some/shared/file ]; do
# endless loop
done

# now perform your normal start

This assumes that both /path/to/my/download and /some/shared/file are accessible from both containers, either through a volume mount or a shared data container (and in particular, /some/shared/file must appear at the same location in both containers).

Upvotes: 2

mchawre
mchawre

Reputation: 12238

I would suggest to make a change in containerA entrypoint.

As both the container are using same data they will be pointing to some common directory or mountpoint.

in containerB after completing the download operation. Create some event that will mark completion of this process like touching a file /path/xyz.txt

In containerA entrypoint script it will keep on checking for existence of file /path/xyz.txt and if it exists then it will proceed further.

Upvotes: 1

Related Questions