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 Daemon not running
  • docker.io - Docker linking between application & database containers
  • Connect to remote Docker via Node.js
  • How to access to docker config file in S3 bucket from Elastic Beanstalk instance
  • How to expose docker container from Rancher's private network?
  • Updating docker containers on swarm?
  • docker-compose.yml

    mysql:
      image: mysql:5.6.26
      environment:
       - MYSQL_ROOT_PASSWORD=root
      command: "mysql -uroot -proot < createDB.sql"
      ports:
        - "3306:3306"
    

    createDB.sql

    CREATE DATABASE IF NOT EXISTS bignibou;
    

    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?

  • What benefits does Docker Compose have over Docker Swarm and Docker Stack?
  • WordPress, Docker and nginx
  • Get Docker Container Names
  • Docker: Build your own image issue
  • Using docker-compose to start web and backend. How can I get the IP of backend from web?
  • Difference between Docker and OpenVZ
  • 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:

    MYSQL_DATABASE

    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:

    mysql:
      image: mysql:5.6.26
      environment:
       - MYSQL_ROOT_PASSWORD=root
       - MYSQL_DATABASE=bignibou
      ports:
        - "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
    container.

    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
    command):

    $ 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.