Go to the root directory of the project. Create a new folder and file. After creating the file, put the sql content.
mkdir docker
cd docker
touch docker_mysql-init.sql
-- Put this inside the docker_mysql-init.sql.
CREATE DATABASE IF NOT EXISTS example-db;
Go back to the root directory of the project and create a new file. After creating the file put the yml content.
touch docker-compose.yml
version: '3.6'
services:
example-db:
image: mysql:8.0.28
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=example-db
ports:
- '3310:3310'
volumes:
- ./docker/docker_mysql_init.sql:/docker-entrypoint-initdb.d/docker_mysql_init.sql
Run the following commands below.
docker-compose up --build -d
Use a database client to connect to the project.
DB_HOST=127.0.0.1
DB_PORT=3310
DB_NAME=example-db
DB_USER=root
DB_ROOT_PASSWORD=root