Using docker-compose in order to create a MySQL schema/database

I am trying to create a mysql database/schema if it doesn’t already exist.

Here is what I have tried:

  • Docker container restarting on defining an entrypoint
  • Routing paths with Traefik
  • Docker volume is empty for jenkins compose file, not copying any files
  • Docker/Jupyter notebook setting Base URL
  • How to name a volume using a docker-compose.yml file?
  • Map ports so you can access docker running apps from OSX host
  • docker-compose.yml

      image: mysql:5.6.26
      command: "mysql -uroot -proot < createDB.sql"
        - "3306:3306"



    It does not work. What would be the best way to use docker/docker-compose in order to create a schema if it does not exist?

  • Restarting Docker Container after reboot
  • Docker port binding error
  • How containerd compares to runC
  • Mounting container volume from the hosts' drive?
  • docker updates AWS ELB ports
  • How to debug a non-working passwordless RSA certificate in OpenSSH on Alpine?
  • 3 Solutions collect form web for “Using docker-compose in order to create a MySQL schema/database”

    I finally found the beginning of a solution.

    The MySQL image takes an environment variable i.e. MYSQL_DATABASE that initialize the container with the name of the database on image startup See here for full documentation.

    Or read the excerpt below:


    This variable is optional and allows you to specify the name of a
    database to be created on image startup. If a user/password was
    supplied (see below) then that user will be granted superuser access
    (corresponding to GRANT ALL) to this database.

    Here is what I came up with:

      image: mysql:5.6.26
       - MYSQL_DATABASE=bignibou
        - "3306:3306"

    I now need a way to specify the default collation but that is another story…

    edit: For those interested in specifying a different collation from the default, here are the instructions to use another config file that will override the default one. See below:

    Using a custom MySQL configuration file The MySQL startup
    configuration is specified in the file /etc/mysql/my.cnf, and that
    file in turn includes any files found in the /etc/mysql/conf.d
    directory that end with .cnf. Settings in files in this directory will
    augment and/or override settings in /etc/mysql/my.cnf. If you want to
    use a customized MySQL configuration, you can create your alternative
    configuration file in a directory on the host machine and then mount
    that directory location as /etc/mysql/conf.d inside the mysql

    If /my/custom/config-file.cnf is the path and name of your custom
    configuration file, you can start your mysql container like this (note
    that only the directory path of the custom config file is used in this

    $ docker run –name some-mysql -v /my/custom:/etc/mysql/conf.d -e
    MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag This will start a new
    container some-mysql where the MySQL instance uses the combined
    startup settings from /etc/mysql/my.cnf and
    /etc/mysql/conf.d/config-file.cnf, with settings from the latter
    taking precedence.

    probably what you are trying to do needs an additional script. So if building an image instead of directly using a prebuilt image is an option for you, you need to use a Dockerfile and use a script file which first imports the script in MySql and then runs the service itself.

    take a look at this answer: Docker – Initialize mysql database with schema

    From the docker-compose documentation – see Define Services – you can tell which Dockerfile it will use to build the image. Therefore you can create a Dockerfile based on the mysql image and create the database inside it using standard Dockerfile commands.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.