20

I'm trying to host on docker an application which uses MySQL db. I'm using docker compose. my yml file looks like this:

version: '2'
volumes:
  data_sql:    
services:  
medical-mysql:
    image: mysql
    hostname: medical-mysql    
    volumes:    
     - data_sql:/dbcreation.sql
    environment:
      MYSQL_DATABASE: Medical
      MYSQL_ROOT_PASSWORD: root
      STARTUP_SQL: data_sql
    ports:
    - "3306:3306"
  medical-main:
    build: .
    ports:
     - "8080:8080"    
    depends_on:
     - medical-mysql

I have a dbcreation.sql which creates the db schema and is kept in the yml folder. When I run the yml file it seems as if the file is not running.

What have I missed?

3
  • So what is data_sql and when you call dbcreation.sql ? Commented Mar 7, 2016 at 15:51
  • data_sql is simply the volume I created to host the creation file. I couldn't find any other way to do it, and this one also doesn't work. Commented Mar 8, 2016 at 4:33
  • It seems you are mounting data_sql in dbcreation.sql. You never create the db. You need to copy in /var/lib/mysql (and not dbcreation.sql if you want to use it you need to execute something like that : mysql -u user db < dbcreation.sql ) Commented Mar 8, 2016 at 10:13

2 Answers 2

31

Simply just put your .sql file (dbcreation.sql) in a folder (ie. /mysql_init) and add the folder as a volume like this:

volumes:
  - /mysql_init:/docker-entrypoint-initdb.d

The MySQL image will execute all .sql, .sh and .sql.gz files in /docker-entrypoint-initdb.d on startup.

Sign up to request clarification or add additional context in comments.

1 Comment

where this folder should be? At project level or a docker container?
26

1) Create dump file dbcreation.sql

2) Create import.sh file:

#!/usr/bin/env bash
mysql -u root -p$MYSQL_ROOT_PASSWORD < /tmp/dbcreation.sql

3) Create docker-compose.yaml

database:
  image: mysql
  container_name: database.dev
  command: mysqld --user=root --verbose
  volumes:
    - ./dbcreation.sql:/tmp/dbcreation.sql
    - ./import.sh:/tmp/import.sh
  ports:
    - "3306:3306"
  environment:
    MYSQL_DATABASE: "test"
    MYSQL_USER: "test"
    MYSQL_PASSWORD: "test"
    MYSQL_ROOT_PASSWORD: "root"
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes"

"- ./dbcreation.sql:/tmp/dbcreation.sql" - "- local_path:path_inside_container"

4) Run

docker-compose up
docker exec database.dev bash /tmp/import.sh

4 Comments

I had a problem implementing your answer since once my main container is up, it needs the db constructed. It did give me many clues of getting where I wanted. I split the mysql container creation into a seperate yml file. I made an sh file with the following: docker-compose -f mysql.yml up -d sleep 10; docker exec medical-mysql bash /tmp/import.sh docker-compose up It works. Is there a better way to make sure the mysql container is ready than the sleep?
@IzharLotem it is not about container, it is about application inside. Container is ready, but application inside the container doesn't. You need to resolve it on application level. You can check health of MySQL database in your "main container" or you can overwrite CMD inside Dockerfile and put flag somewhere after import will be done. Anyway it is not docker job.
Running against 8.0.11, I needed to change "command" to: command: mysqld --user=root --default-authentication-plugin=mysql_native_password This allowed me to connect via MySQL Workbench (v6.3)
I get: /tmp/import.sh: /tmp/import.sh: Is a directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.