MySQL with Docker

docker-compose.yml

version: '3'
services:
    db:
        image: mysql:5.7
        ports:
            - "3306:3306"
        volumes:
            - ./db/mysql_init:/docker-entrypoint-initdb.d
            - ./db/mysql_data:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_USER: root
            MYSQL_PASSWORD: root

docker-entrypoint-initdb.d automaticly execute sql on mysql_init directory.
And also, mysql_data will store database’s data.

├── docker-compose.yml
└── db
    ├── mysql_init
    │   └── init.sql
    └── mysql_data

init.sql

create database sample;
use sample;

Create table todo (id int, name varchar(20));
Insert into todo (id, name) values (1, 'sample');

関連記事