title: Docker compose wait for database service initialisation
body: <p>I have a spring boot project which I'd like to containerize using docker.
  I have a couple of spring boot applications which connect to same MySql server.</p>
  
  <p>My spring applications requires the database to be completely setup (i.e. all the tables to be created and some data to be inserted in some of the tables) in order to start.</p>
  
  <p>I am using <strong>Docker</strong> version <strong>18.09.0</strong> and <strong>docker-compose</strong> version <strong>1.23.1</strong> and ubuntu <strong>16.04</strong> LTS</p>
  
  <hr>
  
  <p>I have two files <strong><code>create.sql</code></strong> and <strong><code>insert.sql</code></strong>, which I use to initialise the database to be used by the application.</p>
  
  <p>I create the images using the command <strong><code>docker-compose.yml</code></strong> and it runs successfully and creates the images.</p>
  
  <p>I have the following questions.</p>
  
  <ol>
  <li><p>I assume when using docker-compose, a container starts as soon as all its dependent containers have started. Is there a way to wait for the mysql server to be up and ready to accept connections, before my API container gets started?</p></li>
  <li><p>If I chose to create containers separately for the applications and mysql, and not use docker-compose, how do I make sure that my applications connect to the mysql container?</p></li>
  <li><p>Is there any other tool which might help me achieve this?</p></li>
  </ol>
  
  <p><strong>Note:</strong>
  I have tried to use <strong><code>docker inspect &lt;container_id&gt;</code></strong> to find the the IpAddress for the mysql container and use it to connect, but it doesn't work as well.</p>
  
  <hr>
  
  <p>The following are the files I am using to create images.</p>
  
  <p><strong><code>docker-compose.yml</code></strong> file.</p>
  
  <pre><code>version: '3'
  
  services:
    demo-mysql:
      image: demo-mysql
      build: ./demo-mysql
      volumes:
        - /mnt/data/mysql-data:/var/lib/mysql
      ports:
        - 3306:3306
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=demo
        - MYSQL_PASSWORD=root
  
    demo-api:
      image: demo-api-1.0
      build: ./api
      depends_on:
        - demo-mysql
      ports:
        - 8080:8080
      environment:
        - DATABASE_HOST=demo-mysql
        - DATABASE_USER=root
        - DATABASE_PASSWORD=root
        - DATABASE_NAME=demo
        - DATABASE_PORT=3306
  
    demo1-app:
      image: demo1-app-1.0
      build: ./demo1
      depends_on:
        - demo-mysql
      ports:
        - 8090:8090
      environment:
        - DATABASE_HOST=demo-mysql
        - DATABASE_USER=root
        - DATABASE_PASSWORD=root
        - DATABASE_NAME=demo
        - DATABASE_PORT=3306
  </code></pre>
  
  <p>The following is the <strong><code>Dockerfile</code></strong> for the spring boot project</p>
  
  <pre><code>FROM java:8
  
  VOLUME /tmp
  
  ARG DATA_PATH=/src/main/resources
  ARG APP_PORT=8080
  
  EXPOSE ${APP_PORT}
  
  ADD /build/libs/demo-api.jar demo-api.jar
  
  ENTRYPOINT ["java","-jar","demo-api.jar"]
  </code></pre>
  
  <p>The following is the <strong><code>Dockerfile</code></strong> I used to create my mysql image</p>
  
  <pre><code>FROM mysql:5.7
  
  ENV MYSQL_DATABASE=demo \
      MYSQL_USER=root \
      MYSQL_ROOT_PASSWORD=root
  
  ADD ./1.0/create.sql /docker-entrypoint-initdb.d
  ADD ./1.0/insert.sql /docker-entrypoint-initdb.d
  
  EXPOSE 3306
  </code></pre>