How can I create a mysql db with Docker compose?

后端 未结 2 1674
攒了一身酷
攒了一身酷 2021-01-31 20:06

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:             


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 20:22

    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
    

提交回复
热议问题