When running a service inside a container, let's say mongodb, the command
docker run -d myimage
will exit instantly, and return the container id.
In my CI script, I run a client to test mongodb connection, right after running the mongo container.
The problem is: the client can't connect because the service is not up yet.
Apart from adding a big sleep 10
in my script, I don't see any option to wait for a container to be up and running.
Docker has a command wait
which doesn't work in that case, because the container doesn't exist.
Is it a limitation of docker?
Thanks
test/test_runner
$ docker run -v /tmp/mnt:/mnt myimage ruby mnt/test/test_runner
I'm testing like this whether the port is listening or not. In this case I have test running from inside container, but it's also possible from outside whether mongodb is ready or not.
$ docker run -p 37017:27017 -d myimage
And check whether the port 37017 is listening or not from host container.
If the containerized service you started doesn't necessarily respond well to curl or wget requests (which is quite likely for many services) then you could use
nc
instead.Here's a snippet from a host script which starts a Postgres container and waits for it to be available before continuing:
Edit - This example does not require that you EXPOSE the port you are testing, since it accesses the Docker-assigned 'private' IP address for the container. However this only works if the docker host daemon is listening on the loopback (127.x.x.x). If (for example) you are on a Mac and running the boot2docker VM, you will be unable to use this method since you cannot route to the 'private' IP addresses of the containers from your Mac shell.
Assuming that you know the host+port of your MongoDB server (either because you used a
-link
, or because you injected them with-e
), you can just usecurl
to check if the MongoDB server is running and accepting connections.The following snippet will try to connect every second, until it succeeeds:
As commented in a similar issue for docker 1.12
This is available since docker 1.12rc3 (2016-07-14)
docker-compose
is in the process of supporting a functionality to wait for specific conditions.You can use it in Dockerfile like this:
Official docs: https://docs.docker.com/engine/reference/builder/#/healthcheck
Found this simple solution, been looking for something better but no luck...
or if you want to wait until the container is reporting as healthy (assuming you have a healthcheck)
If you don't want to expose the ports, as is the case if you plan to link the container and might be running multiple instances for testing, then I found this was a good way to do it in one line :) This example is based on waiting for ElasticSearch to be ready:
This requires wget to be available, which is standard on Ubuntu. It will retry 5 times, 3 seconds between tries, even if the connection is refused, and also does not download anything.