Dockerのテスト用DBが立ち上がらない

## 概要

なぜかDockerのテスト用DBがExit1で終了してしまう現象に見舞われ、ハマったのでメモ。

```cmd
src $ docker-compose ps
         Name                     Command           State            Ports
-----------------------------------------------------------------------------------
db                        docker-entrypoint.sh      Up       0.0.0.0:3307->3306/tcp
                          mysqld                             , 33060/tcp
db-testing                docker-entrypoint.sh      Exit 1
                          mysqld
docker-laravel_app_1      docker-php-entrypoint     Up       0.0.0.0:18000->8000/tc
                          php-fpm                            p, 9000/tcp
docker-laravel_https-     /init                     Exit 0
portal_1
docker-laravel_web_1      nginx -g daemon off;      Up       0.0.0.0:10080->80/tcp
```


## 環境

MacBook Air 2019 (core-i5, RAM16GB)
Laravel7
Docker for Desktop 2.3.0.4
Docker version 19.03.12
Docker-compose version 1.26.2

## 該当のDocker-compose.yml

```docker-compose.yml
version: "3"
services:

  db:
    build:
      context: ./docker/mysql
    container_name: db
    volumes:
      - db-store:/var/lib/mysql
      - db-logs-store:/var/log/mysql
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      - MYSQL_DATABASE=${DB_NAME:-JETmysql}
      - MYSQL_USER=${DB_USERNAME:-root}
      - MYSQL_PASSWORD=${DB_PASS:-secret}
      - MYSQL_ROOT_PASSWORD=${DB_PASS:-secret}
      - TZ=${TZ:-Asia/Tokyo}
    ports:
      - ${DB_PORT}:3306

  db-testing:
    image: mysql:8.0
    container_name: db-testing
    volumes:
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    tmpfs:
      - /var/lib/mysql
      - /var/log/mysql
    environment:
      - MYSQL_DATABASE=${DB_NAME:-JETmysql}
      - MYSQL_USER=${DB_USERNAME:-root}
      - MYSQL_PASSWORD=${DB_PASS:-secret}
      - MYSQL_ROOT_PASSWORD=${DB_PASS:-secret}
      - TZ=${TZ:-Asia/Tokyo}
    ports:
      - ${DB_TESTING_PORT}:3306
 
volumes:
  db-logs-store:
  db-store:
  redis-store:

```

一見問題ないように見えたのだが、これだとlogが書き込めず権限エラーが出る。

```cmd
src $ docker logs db-testing
2020-09-05 16:31:21+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-09-05 16:31:22+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-09-05 16:31:22+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-09-05 16:31:22+09:00 [Note] [Entrypoint]: Initializing database files
2020-09-05T16:31:22.412681+09:00 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 43
2020-09-05T16:31:22.420005+09:00 0 [ERROR] [MY-010187] [Server] Could not open file '/var/log/mysql/mysql-error.log' for error logging: No such file or directory
2020-09-05T16:31:22.420889+09:00 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2020-09-05T16:31:22.421233+09:00 0 [ERROR] [MY-010119] [Server] Aborting
2020-09-05T16:31:22.421572+09:00 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.21)  MySQL Community Server - GPL.
```
これを回避するには、Dockerfileに権限付与のcmdを記載する。

```Dockerfile
FROM mysql:8.0
RUN mkdir /var/log/mysql
RUN chown mysql:mysql /var/log/mysql
```
イメージをコメントアウトし、Dockerfileを読み込む。

```docker-compose.yml
db-testing:
    # image: mysql:8.0
    build:
      context: ./docker/mysql
      dockerfile: Dockerfile.test
```
これで直るかと思いきや、別のエラーが発生。

```cmd
2020-09-05 16:37:43+09:00 [Note] [Entrypoint]: Temporary server started.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2020-09-05 16:37:53+09:00 [Note] [Entrypoint]: Creating database JETmysql
2020-09-05 16:37:53+09:00 [Note] [Entrypoint]: Creating user root
ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
```
タイムゾーンがおかしくて、USERが作れない？？
と思ったのだが、空振り。

## USERNAMEとtmpfsのlogを指定しないことで解決した

```docker-compose.yml
db-testing:
    # image: mysql:8.0
    build:
      context: ./docker/mysql
      dockerfile: Dockerfile.test
    container_name: db-testing
    volumes:
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    tmpfs:
      - /var/lib/mysql
      # - /var/log/mysql
    environment:
      - MYSQL_DATABASE=${DB_NAME:-JETmysql}
      # - MYSQL_USER=${DB_USERNAME:-root}
      - MYSQL_PASSWORD=${DB_PASS:-secret}
      - MYSQL_ROOT_PASSWORD=${DB_PASS:-secret}
      - TZ=${TZ:-Asia/Tokyo}
    ports:
      - ${DB_TESTING_PORT}:3306
   ```
Warningが出たままだが。。一応起動した。

```cmd
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2020-09-05 16:45:50+09:00 [Note] [Entrypoint]: Creating database JETmysql

2020-09-05 16:45:50+09:00 [Note] [Entrypoint]: Stopping temporary server
2020-09-05 16:45:53+09:00 [Note] [Entrypoint]: Temporary server stopped

2020-09-05 16:45:53+09:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
```

下記によると、すでにどこかでrootユーザを指定しているため、
再度指定するとエラーになるということ。
確かにdbの方では指定しているけれど、コンテナ同士干渉しないはずなので理屈がよくわからん。。
https://github.com/docker-library/mysql/issues/129#issuecomment-178265632

分かる人がいればぜひ教えて下さい。