how to run docker exec on a docker-compose.yml

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)