Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wait for a docker container to be up and running?

Tags:

docker

mongodb

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 10in 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?

like image 342
Gravis Avatar asked Jan 17 '14 10:01

Gravis


People also ask

How do I start a docker container and keep it running?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.

What is the use of docker wait command?

The 'docker wait' is a command that is used to wait or block until one or more containers stop, and then it outputs their exit codes which means you cannot use your terminal if you are running the command on the terminal.

How can I tell if a docker container is running?

The operating-system independent way to check whether Docker is running is to ask Docker, using the docker info command. You can also use operating system utilities, such as sudo systemctl is-active docker or sudo status docker or sudo service docker status , or checking the service status using Windows utilities.

Is a docker container always running?

A Docker container runs a process (the "command" or "entrypoint") that keeps it alive. The container will continue to run as long as the command continues to run.


1 Answers

Found this simple solution, been looking for something better but no luck...

until [ "`docker inspect -f {{.State.Running}} CONTAINERNAME`"=="true" ]; do     sleep 0.1; done; 

or if you want to wait until the container is reporting as healthy (assuming you have a healthcheck)

until [ "`docker inspect -f {{.State.Health.Status}} CONTAINERNAME`"=="healthy" ]; do     sleep 0.1; done; 
like image 63
superhero Avatar answered Oct 13 '22 00:10

superhero