Order of containers in Docker Compose

If you have a simple setup, with Tomcat and Postgres, sometimes Postgres will start first, but Compose
will initialize Tomcat before Postgres has fully booted. When that happens, you may receive 401, 404, or other
application errors.

You can fix it by combining depends_on with a healthcheck. For example:

In the example docker-compose.yml, there are two containers, db and web. web is running
a Tomcat, and db is running Postgres. Web depends on db (see depends_on), and uses a condition
service_healthy. Which indicates it depends that that container is healthy.

The healthcheck entry under the db container settings define how to check whether Postgres
is running or not. In this case, we are using pg_isready, which is available in the
vanilla Postgres 9 container.

It will try 5 times, with a 10 seconds interval, and will time out after 5 seconds. You may
have to tune these parameters for your application.