docker-compose mysql init sql is not executed










0














I am trying to set up a mysql docker container and execute init sql script. Unfortunately the sql script is not executed. What am I doing wrong?



version: '3.3'
services:
api:
container_name: 'api'
build: './api'
ports:
- target: 8080
published: 8888
protocol: tcp
mode: host
volumes:
- './api:/go/src/app'
depends_on:
- 'mysql'
mysql:
image: 'mysql:latest'
container_name: 'mysql'
volumes:
- ./db_data:/var/lib/mysql:rw
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: always
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
ports:
- '3306:3306'
volumes:
db_data:


I execute file with docker-compose up -d --build










share|improve this question

















  • 1




    The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
    – Pramodh Valavala
    Nov 11 at 14:02










  • Thank you! Deleting db_data folder solved it!
    – dqmis
    Nov 11 at 14:08















0














I am trying to set up a mysql docker container and execute init sql script. Unfortunately the sql script is not executed. What am I doing wrong?



version: '3.3'
services:
api:
container_name: 'api'
build: './api'
ports:
- target: 8080
published: 8888
protocol: tcp
mode: host
volumes:
- './api:/go/src/app'
depends_on:
- 'mysql'
mysql:
image: 'mysql:latest'
container_name: 'mysql'
volumes:
- ./db_data:/var/lib/mysql:rw
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: always
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
ports:
- '3306:3306'
volumes:
db_data:


I execute file with docker-compose up -d --build










share|improve this question

















  • 1




    The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
    – Pramodh Valavala
    Nov 11 at 14:02










  • Thank you! Deleting db_data folder solved it!
    – dqmis
    Nov 11 at 14:08













0












0








0







I am trying to set up a mysql docker container and execute init sql script. Unfortunately the sql script is not executed. What am I doing wrong?



version: '3.3'
services:
api:
container_name: 'api'
build: './api'
ports:
- target: 8080
published: 8888
protocol: tcp
mode: host
volumes:
- './api:/go/src/app'
depends_on:
- 'mysql'
mysql:
image: 'mysql:latest'
container_name: 'mysql'
volumes:
- ./db_data:/var/lib/mysql:rw
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: always
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
ports:
- '3306:3306'
volumes:
db_data:


I execute file with docker-compose up -d --build










share|improve this question













I am trying to set up a mysql docker container and execute init sql script. Unfortunately the sql script is not executed. What am I doing wrong?



version: '3.3'
services:
api:
container_name: 'api'
build: './api'
ports:
- target: 8080
published: 8888
protocol: tcp
mode: host
volumes:
- './api:/go/src/app'
depends_on:
- 'mysql'
mysql:
image: 'mysql:latest'
container_name: 'mysql'
volumes:
- ./db_data:/var/lib/mysql:rw
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: always
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
ports:
- '3306:3306'
volumes:
db_data:


I execute file with docker-compose up -d --build







mysql sql database docker docker-compose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 13:35









dqmis

235




235







  • 1




    The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
    – Pramodh Valavala
    Nov 11 at 14:02










  • Thank you! Deleting db_data folder solved it!
    – dqmis
    Nov 11 at 14:08












  • 1




    The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
    – Pramodh Valavala
    Nov 11 at 14:02










  • Thank you! Deleting db_data folder solved it!
    – dqmis
    Nov 11 at 14:08







1




1




The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
– Pramodh Valavala
Nov 11 at 14:02




The db_data folder probably has data from a previous run of the container. Try cleaning up the containers, removing the data folder and start again
– Pramodh Valavala
Nov 11 at 14:02












Thank you! Deleting db_data folder solved it!
– dqmis
Nov 11 at 14:08




Thank you! Deleting db_data folder solved it!
– dqmis
Nov 11 at 14:08












2 Answers
2






active

oldest

votes


















0














The docker-entrypoint-initdb.d folder will only be run once while the container is created (instantiated) so you actually have to do a docker-compose down -v to re-activate this for the next run.



If you want to be able to add sql files at any moment you can look here at a specialized MySql docker image... https://github.com/IvoNet/ivonet-docker-images/tree/master/databases/mysql



Hope it helps






share|improve this answer




























    0














    Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.



    And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.



    Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.






    share|improve this answer




















      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%2f53249276%2fdocker-compose-mysql-init-sql-is-not-executed%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The docker-entrypoint-initdb.d folder will only be run once while the container is created (instantiated) so you actually have to do a docker-compose down -v to re-activate this for the next run.



      If you want to be able to add sql files at any moment you can look here at a specialized MySql docker image... https://github.com/IvoNet/ivonet-docker-images/tree/master/databases/mysql



      Hope it helps






      share|improve this answer

























        0














        The docker-entrypoint-initdb.d folder will only be run once while the container is created (instantiated) so you actually have to do a docker-compose down -v to re-activate this for the next run.



        If you want to be able to add sql files at any moment you can look here at a specialized MySql docker image... https://github.com/IvoNet/ivonet-docker-images/tree/master/databases/mysql



        Hope it helps






        share|improve this answer























          0












          0








          0






          The docker-entrypoint-initdb.d folder will only be run once while the container is created (instantiated) so you actually have to do a docker-compose down -v to re-activate this for the next run.



          If you want to be able to add sql files at any moment you can look here at a specialized MySql docker image... https://github.com/IvoNet/ivonet-docker-images/tree/master/databases/mysql



          Hope it helps






          share|improve this answer












          The docker-entrypoint-initdb.d folder will only be run once while the container is created (instantiated) so you actually have to do a docker-compose down -v to re-activate this for the next run.



          If you want to be able to add sql files at any moment you can look here at a specialized MySql docker image... https://github.com/IvoNet/ivonet-docker-images/tree/master/databases/mysql



          Hope it helps







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 14:56









          Ivonet

          1,167518




          1,167518























              0














              Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.



              And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.



              Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.






              share|improve this answer

























                0














                Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.



                And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.



                Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.






                share|improve this answer























                  0












                  0








                  0






                  Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.



                  And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.



                  Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.






                  share|improve this answer












                  Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.



                  And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.



                  Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 2:32









                  Pramodh Valavala

                  47127




                  47127



























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53249276%2fdocker-compose-mysql-init-sql-is-not-executed%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

                      Kleinkühnau

                      Makov (Slowakei)

                      Deutsches Schauspielhaus