35

I am trying to create a mysql database schema during the docker-compose.yml file is getting executed

  version: "2"
  services:
  web:
    build: docker
    ports:
      - "8080:8080"

  environment:
    - MYSQL_ROOT_PASSWORD=root

   mysql:
   image: mysql:latest
    environment:
     - MYSQL_ROOT_PASSWORD=root
     - MYSQL_DATABASE=test

     ports:
      - "3306:3306"
   links:
    - web

 onrun:
 command: "docker exec -i test_mysql_1 mysql -uroot -proot test <dummy1.sql"

I tried onrun but this is not working . i am building the first image but pulling the second image from the docker hub. kindly help in how to execute the following command after the docker-compose up

2 Answers 2

35

There is nothing like onrun in docker-compose. It will only bring up the containers and execute the command. Now you have few possible options

Use mysql Image Initialization

mysql:
 image: mysql:latest
 environment:
   - MYSQL_ROOT_PASSWORD=root
   - MYSQL_DATABASE=test
  volumes: 
    - ./dummy1.sql:/docker-entrypoint-initdb.d/dummy1.sql
 ports:
  - "3306:3306"

You may your sql files inside /docker-entrypoint-initdb.d inside the container

Use bash script

docker-compose up -d
# Give some time for mysql to get up
sleep 20
docker-compose exec mysql mysql -uroot -proot test <dummy1.sql

Use another docker service to initialize the DB

version: "2"
services:
  web:
    build: docker
    ports:
      - "8080:8080"

  environment:
    - MYSQL_ROOT_PASSWORD=root

   mysql:
   image: mysql:latest
    environment:
     - MYSQL_ROOT_PASSWORD=root
     - MYSQL_DATABASE=test

     ports:
      - "3306:3306"
   mysqlinit:
     image: mysql:latest
     volumes:
       - ./dummy1.sql:/dump/dummy1.sql
     command: bash -c "sleep 20 && mysql -h mysql -uroot -proot test < /dump/dummy1.sql"

You run another service which will init the DB for you, like mysqlinit in the above one

2
  • i want to expose different ports for different version of the application which are independent of each other and uses different db schema.how should i expose the ports for that?
    – raj19
    Aug 30, 2017 at 20:15
  • @raj19, that is separate question and not related to your original question. Please make a new post Aug 31, 2017 at 6:54
8

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.

From https://hub.docker.com/_/mysql/

That is the convenient way how many databases (postgresql, mysql, ...) are initializing themselves on container-creation. You should create a *.sql / *.sh file and bind it via volume into the new container:

db:
  image: mysql:latest
  volumes: 
    - ./db/entrypoint:/docker-entrypoint-initdb.d
  environment:
    - MYSQL_ROOT_PASSWORD=iamgroot
    - MYSQL_DATABASE=gotg

This loads all your sql / sh files into the container which are then automatically executed.

1
  • They only get automatically executed if the data folder is empty
    – Steve
    Jan 11, 2021 at 18:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.