Click here to Skip to main content
15,877,533 members
Articles / Containers / Docker

Controlling Service Startup Order in Docker Compose

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
20 Mar 2023MIT10 min read 17.3K   12   2
Ensure your Docker Compose services start in the correct order



In case you’re using Docker Compose for running several containers on a machine, sooner or later, you’ll end-up in a situation where you’ll need to ensure service A runs before service B. The classic example is an application which needs to access a database; if both of these compose services are started via the docker-compose up command, there is a chance this will fail, since the application service might start before the database service and it will not find a database able to handle its SQL statements. The guys behind Docker Compose have thought about this issue and provided the depends_on directive for expressing dependency between services.

On the other hand, just because the database service was started before application service, it doesn’t mean that the database is ready to handle incoming connections (the ready state). Any relational database system needs to start its own services before being able to handle incoming connections (for instance, check a simplified view over SQL Server startup steps) and the startup might take a while, so we need a better mechanism of detecting the ready state of a particular compose service, in addition to specifying its dependents.

In this article, I will present several approaches inspired by the official recommendations and other sources. Each approach will use its own compose file and each of these compose files contains at least two services: a Java 8 console application and a MySQL v5.7 database; the former will connect to the latter using plain-old JDBC, will read some metadata and then will print them to the console.

All compose files will use the same Java application Docker image.

There is also a bonus section at the end of this article, so please check it out too!

Important Things

  • My environment
    • Windows 10 x64 Pro
    • Docker v18.03.1-ce-win65 (17513)
    • Docker Compose v1.21.1, build 7641a569
  • The source code used by this article can be found on GitHub
  • The .NET Core port of this article can be found here:
  • All commands below must be executed from a Powershell console run as admin
  • Also, since I’m lazy, I have embedded Linux shell commands inside the Docker Compose files, which is most definitely not a best practice, but since the point of this article is service startup order and not Docker Compose file best practices, please endure - please check the .NET Core port for the correct approach.
  • I’m using mvn, docker-compose down and docker-compose build commands before starting any compose service via docker-compose up to ensure:
    • I will run the lastest build of the Java application using default Maven goal; in my case, this is: clean compile assembly:single
    • Any running compose service will be stopped
    • Any Docker image declared in the compose file will be rebuilt
  • The aforementioned compose files make use of variables declared in a .env file, with the following content:



# Use "suspend=y" to ensure the JVM will pause the application, 
# waiting for a debugger to be attached
java_debug_settings=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9876

# The amount of time between two consecutive health state checks 
# (used by docker-compose-using-healthcheck.yml)

# The maximum amount of time each healthcheck state try must end in
# (used inside docker-compose-using-healthcheck.yml)

# The maximum amount of retries before giving up and considering 
# the Docker container in an unhealthy state
# (used by docker-compose-using-port-checking.yml and docker-compose-using-api.yml)

# The amount of time between two consecutive queries against the database
# (used by docker-compose-using-port-checking.yml)

# The maximum amount of retries before giving up and considering 
# the database is not able to process incoming connections
# (used by docker-compose-using-port-checking.yml)

# The Docker API version to use when querying for container metadata
# (used by docker-compose-using-api.yml)

Since the .env file contains sensitive things like database passwords, it should not be put under source control.

Solution #1: Use depends_on, condition and service_healthy

This solution uses this Docker compose file: docker-compose-using-healthcheck.yml.

Run it using the following commands:

mvn `
;docker-compose --file docker-compose-using-healthcheck.yml down --rmi local `
;docker-compose --file docker-compose-using-healthcheck.yml build `
;docker-compose --file docker-compose-using-healthcheck.yml up 

Starting with version 1.12, Docker has added the HEALTHCHECK Dockerfile instruction used for verifying whether a container is still working; Docker Compose file has added support for using the health check when expressing a service dependency since version 2.1, as documented inside the compatibility matrix.

My database service will define its health check as a My SQL client command which will periodically query whether the underlying MySQL database is ready to handle incoming connections via the USE SQL statement:

    image: mysql:5.7.20
      test: >
        mysql \
          --host='localhost' \
          --user='${mysql_database_user}' \
          --password='${mysql_database_password}' \
          --execute='USE ${mysql_database_name}' \
      interval: ${healthcheck_interval}
      timeout: ${healthcheck_timeout}
      retries: ${healthcheck_retries}

Keep in mind USE statement is not the only way of performing such check. For instance, one could periodically run a SQL script which would test whether the database is accessible and that the database user has been granted all the expected permissions (e.g., can perform INSERT against a particular table, etc.).

My application service will be started as soon as the database service has reached the “healthy” state:

    image: satrapu/jdbc-with-docker-console-runner
        condition: service_healthy

As you can see, stating the dependency between db and app services is pretty easy, same as doing a health check. Even better, these things are built-in Docker Compose.

And now the bad news: since Docker Compose file format is used by Docker Swarm too, the development team has decided to mark this feature as obsolete starting with compose file v3, as documented here; see more reasoning behind this decision here.

The depends_on, condition and service_healthy are usable only when using older compose file versions (v2.1 up to and including v2.4).

Keep in mind Docker Compose might remove support for these versions in a future release, but as long as you’re OK with using compose file versions before v3, this solution is very simple to understand and use.

Solution #2: Port Checking With a Twist

This solution uses this Docker compose file: docker-compose-using-port-checking.yml.

Run it using the following commands:

mvn `
;docker-compose --file docker-compose-using-port-checking.yml down --rmi local  `
;docker-compose --file docker-compose-using-port-checking.yml build `
;docker-compose --file docker-compose-using-port-checking.yml up --exit-code-from check_db_connectivity check_db_connectivity `
;if ($LASTEXITCODE -eq 0) { docker-compose --file docker-compose-using-port-checking.yml up app } `
else { echo "ERROR: Failed to start service due to one of its dependencies!" }

This solution was inspired by one of Dariusz Pasciak’s articles, but I’m not just checking whether MySQL port 3306 is open (port checking), as Dariusz is doing: I’m running the aforementioned USE SQL statement using a MySQL client found inside the check_db_connectivity compose service to ensure the underlying database can handle incoming connections (the twist); additionally, the exit code of the check_db_connectivity service will be evaluated due to the –exit-code-from check_db_connectivity compose option and if different than 0 (which marks the db service is in desired ready state), an error message will be printed and app service will not start.

  • Docker Compose will try starting check_db_connectivity service, but it will see that it has a dependency on db service:

        image: mysql:5.7.20
        image: activatedgeek/mysql-client:0.1
          - db
  • Docker Compose will start db service
  • Docker Compose will then start check_db_connectivity service, which will initiate a loop checking that the MySQL database can handle incoming connections
  • Docker Compose will wait for check_db_connectivity service to finish its loop, as the loop is part of the service entry point:
      image: activatedgeek/mysql-client:0.1
      entrypoint: >
        /bin/sh -c "
          echo \"Start checking whether MySQL database \
                "${mysql_database_name}\" is up & running\" \
                \"(able to process incoming connections) 
                each $$sleepingTime for a total amount of $$totalAttempts times\"
          while [ $$currentAttempt -le $$totalAttempts ]; do
            sleep $$sleepingTime
            mysql \
              --host='db' \
              --port='3306' \
              --user='${mysql_database_user}' \
              --password='${mysql_database_password}' \
              --execute='USE ${mysql_database_name}'
            if [ $$? -eq 0 ]; then
              echo \"OK: [$$currentAttempt/$$totalAttempts] MySQL database \
                    "${mysql_database_name}\" is up & running.\"
              return 0
              echo \"WARN: [$$currentAttempt/$$totalAttempts] MySQL database \"
                     ${mysql_database_name}\" is still NOT up & running ...\"
              currentAttempt=`expr $$currentAttempt + 1`
          echo 'ERROR: Could not connect to MySQL database \"
                ${mysql_database_name}\" in due time.'
          return 1"	    
  • Docker Compose will then start app service; by the time this service is running, the MySQL database is able to handle incoming connections.
        image: satrapu/jdbc-with-docker-console-runner
          - db

This solution is similar with the previous one in the sense that the application service waits till the database service enters a specific state, but then without using a Docker Compose obsolete feature.

Solution #3: Invoke Docker Engine API

This solution uses this Docker compose file: docker-compose-using-api.yml.

Run it using the following commands:

;mvn `
;docker-compose --file docker-compose-using-api.yml down --rmi local  `
;docker-compose --file docker-compose-using-api.yml build `
;docker-compose --file docker-compose-using-api.yml up


Running the above commands without including COMPOSE_CONVERT_WINDOWS_PATHS environment variable will fail:

Creating jdbc-with-docker_app_1 ... error

ERROR: for jdbc-with-docker_app_1  Cannot create container for service app: 
b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis 
not a valid Windows path'

This issue and its fix are documented here.

I really like the idea of expressing dependencies between compose services via health checks. Since condition form of depends_on will be gone sooner or later, I thought about implementing something conceptually similar and one way is using Docker Engine API.

My approach is to periodically query the health state of the database service from within the application service entry point by making an HTTP request to the Docker API endpoint and parse the response using jq, a command-line JSON processor; the Java application will start as soon as the database service has reached the “healthy” state.

First, I will get the JSON document containing information about all running containers via a simple curl command. The special thing is to use the unix-socket curl option, since this kind of socket is used by Docker daemon. Additionally, I need to expose the docker.sock as a volume to the container running curl command to allow it communicate with the local Docker daemon.


Sharing your local Docker daemon socket should be done with care, as it can lead to security issues, as very clearly presented here, so carefully consider all things before using this approach!

Now that the security ad has been played, below you may find an example of what the stand-alone command used for listing all Docker containers running on the local host would look like - please note I’m running curl from within a Docker container, byrnedo/alpine-curl, while the actual command is executed from a container based on openjdk:8-jre-alpine Docker image:

# Ensure db service is running before querying its metadata
docker-compose --file docker-compose-using-api.yml up -d db `
;docker container run `
       --rm `
       -v /var/run/docker.sock:/var/run/docker.sock `
       byrnedo/alpine-curl `
          --silent `
          --unix-socket /var/run/docker.sock `

The output would look similar to this:

      "Command":" mysqld",
      "Status":"Up 6 seconds (healthy)",

Secondly, I will extract the health state of the database service using various jq operators and functions:

jq '.[] | select(.Names[] | contains("_db_")) | 
select(.State == "running") | .Status | contains("healthy")'

# The output should be "true" in case the db service has reached the healthy state
  • .[]: This will select all records from the given JSON document.
  • select(.Names[] | contains(“_db_”)): This will select the records whose “Names” array property has a record containing the “_db_string - the name of a Docker container created by Docker Compose contains the service name; in our case it is “db”.
  • select(.State == “running”): This will select only running Docker containers.
  • .Status | contains(“healthy”): This will select the value of the “Status” property, which, in case the container has reached healthy state, should be “true”.

In order to reach the final jq command found inside the Docker Compose file, I have experimented using jq Playground. Please note this is not the only way of extracting the health status out of the Docker JSON - use your imagination to come up with better jq commands.


Controlling service startup order in Docker Compose is something we cannot ignore, but I hope the approaches presented in this article will help anybody understand where to start from.

I’m fully aware these are not the only options - for instance, ContainerPilot, which implement the autopilot pattern, looks very interesting. Another option is moving the delayed startup logic inside the dependent service (e.g., have my Java console application use a connection pool with a longer timeout used for fetching connections to MySQL database), but this requires glue code for checking each dependency (one approach for MySQL, another one for a cache provider, like Memcache, etc.). The good news is that there are many options, you just need to identify which one is more/most suitable for your use case.


While working on the Java console application, I have encountered several challenges and I thought I should also mention them here, along with their solutions, as this may help others too.

Maven Assembly Plugin

Adding a dependency in a Maven pom.xml file is trivialwell documented, but then you need to ensure that the dependency JAR file(s) will be correctly packaged with your console application.

One way of packing all files in one JAR is using Maven Assembly plugin and use its assembly:single goal, like I did.

Running this goal will create an jdbc-with-docker-jar-with-dependencies.jar file under the ./target folder instead if the usual jdbc-with-docker.jar, that’s why I’m renaming the JAR file inside the Dockerfile to a shorter name.

Debug Dockerized Java Application

Debugging a Java process means launching the process with several debugging related parameters.

Two of these parameters are crucial for debugging:

  • address, representing the port where the JVM listens for a debugger; the same port must be configured on IDE side when starting the debug session
  • suspend, which specifies whether the JVM should block and wait until a debugger is attached

Since I’m using Visual Studio Code for developing this particular Java application, I need to create a debug configuration and set the port which is specified inside the .env file via key java_debug_port (e.g., java_debug_port=9876). On the other hand, since the application will run inside a container, this port needs to be published to the Docker host where the IDE is running on.

Launch the application and see the JVM waiting for a debugger:

>> ;mvn `
>> ;docker-compose --file docker-compose-using-api.yml down --rmi local  `
>> ;docker-compose --file docker-compose-using-api.yml build `
>> ;docker-compose --file docker-compose-using-api.yml up
# ...
# Creating jdbc-with-docker_db_1 ... done
# Creating jdbc-with-docker_app_1 ... done
# Attaching to jdbc-with-docker_db_1, jdbc-with-docker_app_1
# ...
# db_1   | 2018-05-12T20:46:19.560436Z 0 [Note] Beginning of list of non-natively partitioned tables
# db_1   | 2018-05-12T20:46:19.574074Z 0 [Note] End of list of non-natively partitioned tables
# app_1  | Start checking whether MySQL database jdbcwithdocker is up & running 
# (able to process incoming connections) each 2s for a total amount of 20 times
# app_1  | OK: [1/20] MySQL database jdbcwithdocker is up & running.
# app_1  | Listening for transport dt_socket at address: 9876

Docker Compose can get the host port via the following command:

docker-compose --file docker-compose-using-api.yml  port --protocol=tcp app 9876

Visual Studio Code needs to have its debug configuration use port 32809:

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit:
    "version": "0.2.0",
    "configurations": [
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 32809

Then launch the debug configuration and see the following output generated by the Java application:

app_1  | JDBC_URL="jdbc:mysql://address=(protocol=tcp)(host=db)(port=3306)/jdbcwithdocker?useSSL=false"
app_1  |
app_1  | JDBC_USER="satrapu"
app_1  |
app_1  | JDBC_PASSWORD="********"
app_1  |
app_1  | --------------------------------------------------------------------------------------------------------------
app_1  | |              TABLE_SCHEMA |                                         TABLE_NAME |                TABLE_TYPE |
app_1  | --------------------------------------------------------------------------------------------------------------
app_1  | |        information_schema |                                     CHARACTER_SETS |               SYSTEM VIEW |
app_1  | |        information_schema |                                         COLLATIONS |               SYSTEM VIEW |
app_1  | |        information_schema |              COLLATION_CHARACTER_SET_APPLICABILITY |               SYSTEM VIEW |
app_1  | |        information_schema |                                              VIEWS |               SYSTEM VIEW |
app_1  | --------------------------------------------------------------------------------------------------------------
app_1  | Application was successfully able to fetch data out of the underlying database!
jdbc-with-docker_app_1 exited with code 0



  • 15th September, 2018 - Initial version


This article, along with any associated source code and files, is licensed under The MIT License

Written By
Technical Lead
Romania Romania
Passionate about programming, driven by excellence.

Comments and Discussions

QuestionGood job Pin
Sacha Barber18-Sep-18 5:32
Sacha Barber18-Sep-18 5:32 
AnswerRe: Good job Pin
Bogdan Marian18-Sep-18 19:30
professionalBogdan Marian18-Sep-18 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.