At Reverb we’ve been using Docker more and more. As part of this I’ve had to update my getting to work start-up script to start a Docker container, wait for the container to be up, and then execute some commands.
Unlike my vagrant-based start-up script that could execute
vagrant up and wait for the command to complete, I’ll be running the
docker-compose-up command and leaving that running in a tmux pane while working. This means that my previous tip of asking tmux to wait for the command to complete would not work because the command doesn’t actually ever complete.
Luckily, my coworker showed me a trick to check if the Docker container is up and available.
What this does is start the docker container, and then repeatedly trying to run
select 1 against the postgres database inside the container. The command is meaningless (and the output is directed to
/dev/null so I don’t see any error text from trying to run
psql before the database is available) but if it successfully completes it means that the database is up and running inside the docker container and other commands that assume the container is running can be successful.
I hope this little trick is helpful to someone in writing some shell scripts that involve Docker.