Search code examples
mysqldockerpercona

mysql docker image support multiply databases creation at start


I need to have to database created at startup in one service. F.e.: services:

  db:
    image: percona:5.7.24-centos
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: db1
      MYSQL_DATABASE_1: db2
      MYSQL_PASSWORD: password
    network_mode: "host"

But unfortunatelly only one is supported using image/environment variable: MYSQL_DATABASE.

I am using this in docker-compose

Can I ask for help?


Solution

  • So write your own Dockerfile and copy the sql script which will create a new database onto the image. Something like below

    FROM mysql
    
    ENV MYSQL_DATABASE=test \
        MYSQL_ROOT_PASSWORD=password \
    
    ADD yourscript.sql /docker-entrypoint-initdb.d/ 
    
    EXPOSE 3306
    

    So all the scipts inside /docker-entrypoint-initdb.d/ will be executed at container startup. In effect, your second database creation script would go inside yourscript.sql which will be executed on container startup

    You can build the image using the command docker build -t masterdanny/percona:5.7.24-centos

    and then use that new image in your docker-compose file

    Cheers. Let me know if you have any questions.