Skip to content

NOTE20190915

Somkiat Puisungnoen edited this page Sep 15, 2019 · 5 revisions

Working with MySQL

docker container run -d --name db \
 -e MYSQL_ROOT_PASSWORD=pass \
 -e MYSQL_DATABASE=first \
 -e MYSQL_USER=name \
 -e MYSQL_PASSWORD=pass \
 mysql:8

docker container exec -it db bash
mysql -uname -ppass
show databases;
use first
show tables;

Data :: data.sql

USE first;

CREATE TABLE USER (
  id INT(11),
  name char(60)
) ENGINE=INNODB;

INSERT INTO USER VALUES(1, 'Sample name 01');
INSERT INTO USER VALUES(2, 'Sample name 02');
INSERT INTO USER VALUES(3, 'Sample name 03');

Dockerfile_db

FROM mysql:8
COPY data.sql /docker-entrypoint-initdb.d/

Build image

docker image build -t mydb:1.0.0 -f Dockerfile_db .

Docker compose :: docker-compose.yml

version: '3.7'
services:
  first:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9999:80"

  mydb:
    build:
      context: .
      dockerfile: Dockerfile_db
    environment:
      - MYSQL_ROOT_PASSWORD=pass 
      - MYSQL_DATABASE=first
      - MYSQL_USER=name
      - MYSQL_PASSWORD=pass

Run

docker-compose build
docker-compose up -d
docker-compose ps
docker-compose logs --follow
Clone this wiki locally