Wait for Docker Container to load
1.1K
wait is a really small Docker utility that blocks until another container is accepting TCP connections.
The default operation looks for a single exported port. Use it like this:
$ docker run -d --name mycontainer some-image-or-other
$ docker run --link mycontainer:mycontainer n3llyb0y/wait
waiting for TCP connection to 172.17.0.105:5432......ok
note: it doesn't matter what the link alias is.
If the container you are linking to has multiple port exports then you will need
to let wait know which ones you are interested in. To do so, set the PORTS environment
variable at run:
$ docker run -d --name mycontainer some-image-or-other
$ docker run -e PORTS=80,443 --link mycontainer:mycontainer n3llyb0y/wait
waiting for TCP connection to 172.17.0.105:80......ok
waiting for TCP connection to 172.17.0.105:443......ok
wait will also interrogate linked services that share the same port (as within a cluster, for example)
$ docker run -d -p 9200 --name cluster1 some-image-or-other
$ docker run -d -p 9200 --name cluster2 some-image-or-other
$ docker run -e PORTS=9200 --link cluster1:w1 --link cluster2:w2 n3llyb0y/wait
waiting for TCP connection to 172.17.0.105:9200......ok
waiting for TCP connection to 172.17.0.106:9200......ok
By default each connection attempt will bail after 30 seconds. You can specify an override using the TIMEOUT env variable:
$ docker run -d -p 9200 -p 32000 --name myservice some-image-or-other
$ docker run -e PORTS="9200 32000" -e TIMEOUT=10 --link myservice:w1 n3llyb0y/wait
waiting for TCP connection to 172.17.0.105:9200...ok
waiting for TCP connection to 172.17.0.105:32000............WARN: unable to connect
experimental usage in docker-compose.yml
I've used this utility in docker-compose yaml files in order to orchestrate a wait period when running automated tests. I don't know how it will behave in other situations!
example docker-compose.yml:
db:
image: postgres
ports:
- "5432"
es:
image: dockerfile/elasticsearch
ports:
- "9200"
wait:
image: n3llyb0y/wait
environment:
PORTS: "5432 9200"
links:
- es
- db
Here the wait container explicitly links to the db and es containers and passes on the ports required to wait for.
Rather than using docker-compose up, instead use
docker-compose run wait
This launches the linked services and backgrounds them, then returns control after the wait container finishes. It looks like this might need to be tweaked for future versions of docker-compose but as of version 1.1.0 it does just what I need.
TODO
The echo output seems to drop the initial waiting for... for subsequent hosts. This needs a workaround or better explanation of why that happens and a proper fix.
credits
The single port usage idea and dockerfile was pulled from aanand/wait: https://github.com/aanand/docker-wait
Content type
Image
Digest
Size
658.7 kB
Last updated
almost 11 years ago
docker pull ggriffin924/docker-wait