Docker Community Forums

Share and learn in the Docker community.

Volume initdb.d parsed and executed before database is created

I have the following mysql service in docker-compose.

services:
  db-mysql:
    image: mysql
    volumes:
      - C:\Users\svirl\Documents\workspace\docker\app\db:/docker-entrypoint-initdb.d
    env_file:
      - app/mysql.env
    ports:
      - 3306:32787

and in mysql.env this vars:

MYSQL_DATABASE_NAME=app
MYSQL_USER_NAME=app_user
MYSQL_PASSWORD=test123
MYSQL_ROOT_PASSWORD=root123

The problem is the user app_user with the password test123 and the database app are not yet created when the initdb.d is parsed and the query will fail at line.

USE app; //database does not exist

A solution would be to manually create that user and database but I don’t wan’t this, I want to use that mysql.env file.

So what would be the solution for this?

The metadata for the official mysql image is at https://github.com/docker-library/official-images/blob/master/library/mysql, which points at this entrypoint script. That entrypoint script says two important things (lines 155-156; line 175):

  1. The CREATE DATABASE call does in fact happen before running any scripts out of the docker-entrypoint-initdb.d directory.
  2. Subsequent calls to mysql include the database name as a command-line parameter, so you don’t need a USE statement in your script.