Docker compose wait for database service initialisation












4















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.



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.



I am using Docker version 18.09.0 and docker-compose version 1.23.1 and ubuntu 16.04 LTS





I have two files create.sql and insert.sql, which I use to initialise the database to be used by the application.



I create the images using the command docker-compose.yml and it runs successfully and creates the images.



I have the following questions.




  1. 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?


  2. 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?


  3. Is there any other tool which might help me achieve this?



Note:
I have tried to use docker inspect <container_id> to find the the IpAddress for the mysql container and use it to connect, but it doesn't work as well.





The following are the files I am using to create images.



docker-compose.yml file.



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


The following is the Dockerfile for the spring boot project



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"]


The following is the Dockerfile I used to create my mysql image



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









share|improve this question



























    4















    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.



    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.



    I am using Docker version 18.09.0 and docker-compose version 1.23.1 and ubuntu 16.04 LTS





    I have two files create.sql and insert.sql, which I use to initialise the database to be used by the application.



    I create the images using the command docker-compose.yml and it runs successfully and creates the images.



    I have the following questions.




    1. 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?


    2. 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?


    3. Is there any other tool which might help me achieve this?



    Note:
    I have tried to use docker inspect <container_id> to find the the IpAddress for the mysql container and use it to connect, but it doesn't work as well.





    The following are the files I am using to create images.



    docker-compose.yml file.



    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


    The following is the Dockerfile for the spring boot project



    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"]


    The following is the Dockerfile I used to create my mysql image



    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









    share|improve this question

























      4












      4








      4








      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.



      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.



      I am using Docker version 18.09.0 and docker-compose version 1.23.1 and ubuntu 16.04 LTS





      I have two files create.sql and insert.sql, which I use to initialise the database to be used by the application.



      I create the images using the command docker-compose.yml and it runs successfully and creates the images.



      I have the following questions.




      1. 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?


      2. 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?


      3. Is there any other tool which might help me achieve this?



      Note:
      I have tried to use docker inspect <container_id> to find the the IpAddress for the mysql container and use it to connect, but it doesn't work as well.





      The following are the files I am using to create images.



      docker-compose.yml file.



      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


      The following is the Dockerfile for the spring boot project



      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"]


      The following is the Dockerfile I used to create my mysql image



      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









      share|improve this question














      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.



      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.



      I am using Docker version 18.09.0 and docker-compose version 1.23.1 and ubuntu 16.04 LTS





      I have two files create.sql and insert.sql, which I use to initialise the database to be used by the application.



      I create the images using the command docker-compose.yml and it runs successfully and creates the images.



      I have the following questions.




      1. 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?


      2. 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?


      3. Is there any other tool which might help me achieve this?



      Note:
      I have tried to use docker inspect <container_id> to find the the IpAddress for the mysql container and use it to connect, but it doesn't work as well.





      The following are the files I am using to create images.



      docker-compose.yml file.



      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


      The following is the Dockerfile for the spring boot project



      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"]


      The following is the Dockerfile I used to create my mysql image



      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






      mysql spring-boot docker docker-compose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 7:39









      greenPadawangreenPadawan

      8261917




      8261917
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Use the healthcheck feature of docker-compose (https://docs.docker.com/compose/compose-file/#healthcheck).
          Something like this:



          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
          healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10


          The depending containers will not start until the demo-mysql container is healthy






          share|improve this answer


























          • The API container doen't contain a MySql client. How is the select query going to execute in that container?

            – greenPadawan
            Nov 22 '18 at 4:41











          • @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

            – Simonluca Landi
            Nov 22 '18 at 8:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407287%2fdocker-compose-wait-for-database-service-initialisation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Use the healthcheck feature of docker-compose (https://docs.docker.com/compose/compose-file/#healthcheck).
          Something like this:



          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
          healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10


          The depending containers will not start until the demo-mysql container is healthy






          share|improve this answer


























          • The API container doen't contain a MySql client. How is the select query going to execute in that container?

            – greenPadawan
            Nov 22 '18 at 4:41











          • @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

            – Simonluca Landi
            Nov 22 '18 at 8:28
















          0














          Use the healthcheck feature of docker-compose (https://docs.docker.com/compose/compose-file/#healthcheck).
          Something like this:



          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
          healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10


          The depending containers will not start until the demo-mysql container is healthy






          share|improve this answer


























          • The API container doen't contain a MySql client. How is the select query going to execute in that container?

            – greenPadawan
            Nov 22 '18 at 4:41











          • @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

            – Simonluca Landi
            Nov 22 '18 at 8:28














          0












          0








          0







          Use the healthcheck feature of docker-compose (https://docs.docker.com/compose/compose-file/#healthcheck).
          Something like this:



          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
          healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10


          The depending containers will not start until the demo-mysql container is healthy






          share|improve this answer















          Use the healthcheck feature of docker-compose (https://docs.docker.com/compose/compose-file/#healthcheck).
          Something like this:



          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
          healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10


          The depending containers will not start until the demo-mysql container is healthy







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 8:29

























          answered Nov 21 '18 at 8:09









          Simonluca LandiSimonluca Landi

          639518




          639518













          • The API container doen't contain a MySql client. How is the select query going to execute in that container?

            – greenPadawan
            Nov 22 '18 at 4:41











          • @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

            – Simonluca Landi
            Nov 22 '18 at 8:28



















          • The API container doen't contain a MySql client. How is the select query going to execute in that container?

            – greenPadawan
            Nov 22 '18 at 4:41











          • @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

            – Simonluca Landi
            Nov 22 '18 at 8:28

















          The API container doen't contain a MySql client. How is the select query going to execute in that container?

          – greenPadawan
          Nov 22 '18 at 4:41





          The API container doen't contain a MySql client. How is the select query going to execute in that container?

          – greenPadawan
          Nov 22 '18 at 4:41













          @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

          – Simonluca Landi
          Nov 22 '18 at 8:28





          @greenPadawan: you are right, need to use "mysqladmin ping" for the healthcheck.

          – Simonluca Landi
          Nov 22 '18 at 8:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407287%2fdocker-compose-wait-for-database-service-initialisation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)