Spring Boot で Rest API 構築 その3 - Docker で MySQL 構築

# Outline

[前回](https://qiita.com/sawa-@github/items/37d8d5679cf7f9533a4d)確認したエラーの対策として、Docker で MySQL を構築する

※タイトルに Spring Boot とありますが、今回の話には一切出てきません

# 環境

[Docker Desktop for Mac](https://hub.docker.com/editions/community/docker-ce-desktop-mac) を使用

![191006-0001.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/21296/452138a0-1b8a-f7a1-c3c2-560ace70895d.png)

docker のバージョン

```shell
$ docker version
Client: Docker Engine - Community
 Version:           19.03.2
 API version:       1.40
 Go version:        go1.12.8
 Git commit:        6a30dfc
 Built:             Thu Aug 29 05:26:49 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.2
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.8
  Git commit:       6a30dfc
  Built:            Thu Aug 29 05:32:21 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
```

docker-compose のバージョン

```shell
$ docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j  20 Nov 2018
```

# 構築

docker-compose を使って MySQL 環境を構築する

## ディレクトリ構成

```shell
docker
├── README.md
├── docker-compose.yml
└── mysql
    ├── Dockerfile
    └── conf.d
        └── my.cnf
```

## Dockerfile

```dockerfile:Dockerfile
FROM mysql:5.7

# MySQLの設定
COPY ./conf.d/my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]
```

https://docs.docker.com/engine/reference/builder/

## conf.d/my.cnf

```conf:my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

[client]
default-character-set=utf8mb4
```

## docker-compose.yml

```yml:docker-compose.yml
version: '3'

services:
  mysql:
    build: ./mysql
    container_name: rest-api-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: rest_api
      MYSQL_USER: root
      MYSQL_PASSWORD: mysql
      TZ: 'Asia/Tokyo'
    volumes:
      - rest-api-mysql-data:/var/lib/mysql
    ports:
      - 3306:3306

volumes:
  rest-api-mysql-data:
    driver: local
```

https://docs.docker.com/compose/compose-file/

# Docker の MySQL 起動

## 起動コマンド

```shell
$ docker-compose up -d
```

https://docs.docker.com/compose/reference/up/

## コンテナの確認

```shell
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d45e26ad12fc        docker_mysql        "docker-entrypoint.s…"   2 minutes ago       Up 11 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   rest-api-mysql
```

https://docs.docker.com/compose/reference/ps/

## シェルから Docker の MySQL に接続する

説明を簡略化するためにコマンドで直接パスワードを指定していますが、セキュリティ上良くないのでやめましょう

```shell
$ docker exec -it rest-api-mysql mysql -uroot rest_api -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
```

https://docs.docker.com/compose/reference/exec/

# 続きの予告

- [Spring Boot から MySQL への接続設定を行う](https://qiita.com/sawa-@github/items/fa9f604c4005a86ab73f)
- Swagger を使って API の設計を行う