MySQL コンテナが永遠に Restarting 状態になる問題が起きた時に対処

## 環境

- PHP: 8.0.28
- Laravel: 9.52.5
- MySQL: 8.0.32

## 現象

```shell-session
$ docker compose ps
NAME           IMAGE      COMMAND                  SERVICE             CREATED             STATUS                         PORTS
db-server      mysql      "docker-entrypoint.s…"   mysql               7 minutes ago       Restarting (1) 3 seconds ago
```

STATUS が常に Restarting 状態になっていて、60秒おきに再起動され続ける問題が起きました。

```shell-session
$ docker compose logs mysql
...
db-server  | 2023-03-30 18:42:37+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-server  | 2023-03-30 18:42:37+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
db-server  | 2023-03-30 18:42:38+09:00 [Note] [Entrypoint]: Initializing database files
...
```

↑のログが永遠に表示され続けている(謎)

```shell-session
$ php artisan migrate

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = xxxxxx and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +39 vendor frames
  40  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
```

試しにmigrateするとホストが見つからない時のエラーが発生しました。
(mysqlコンテナ動いてないから当然ですが)

## 試したが改善されず

docker compose down & up でコンテナ再作成
docker compose down -v でボリュームの破棄
Docker Desktop の再起動
Mac PCの再起動

## Dockerイメージを再ビルド

```shell-session
$ docker compose build --no-cache

 => [internal] load build context                                                                                                                      0.0s
 => => transferring context: 69B                                                                                                                       0.0s
 => ERROR [stage-0 2/8] RUN apt-get update     && apt install -y lsb-release ca-certificates apt-transport-https software-properties-common bash-comp  0.6s
------
 > [stage-0 2/8] RUN apt-get update     && apt install -y lsb-release ca-certificates apt-transport-https software-properties-common bash-completion     && apt-get install -y git unzip libzip-dev libicu-dev libonig-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev libjpeg-dev vim lsof     && docker-php-ext-install intl pdo_mysql zip bcmath:
#0 0.395 Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
#0 0.462 Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
#0 0.495 Err:1 http://deb.debian.org/debian bullseye InRelease
#0 0.495   At least one invalid signature was encountered.
#0 0.515 Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
#0 0.543 Err:2 http://deb.debian.org/debian-security bullseye-security InRelease
#0 0.543   At least one invalid signature was encountered.
#0 0.586 Err:3 http://deb.debian.org/debian bullseye-updates InRelease
#0 0.586   At least one invalid signature was encountered.
#0 0.591 Reading package lists...
#0 0.599 W: GPG error: http://deb.debian.org/debian bullseye InRelease: At least one invalid signature was encountered.
#0 0.599 E: The repository 'http://deb.debian.org/debian bullseye InRelease' is not signed.
#0 0.599 W: GPG error: http://deb.debian.org/debian-security bullseye-security InRelease: At least one invalid signature was encountered.
#0 0.599 E: The repository 'http://deb.debian.org/debian-security bullseye-security InRelease' is not signed.
#0 0.599 W: GPG error: http://deb.debian.org/debian bullseye-updates InRelease: At least one invalid signature was encountered.
#0 0.599 E: The repository 'http://deb.debian.org/debian bullseye-updates InRelease' is not signed.
------
failed to solve: executor failed running [/bin/sh -c apt-get update     && apt install -y lsb-release ca-certificates apt-transport-https software-properties-common bash-completion     && apt-get install -y git unzip libzip-dev libicu-dev libonig-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev libjpeg-dev vim lsof     && docker-php-ext-install intl pdo_mysql zip bcmath]: exit code: 100
```

apt-get update で落ちるのはさすがにおかしい。

disk容量いっぱいだとエラーになるらしいという記事を見つける。
https://note.yu9824.com/error/2022/03/19/docker-debian-gpg-error.html

特にディスク圧迫してなかったが試してみる価値はありそう。

## 解決

```shell-session
$ docker system prune --all
$ docker compose up -d
```

5GBくらいしか溜まってなかったけど、とりあえずきれいにしました。

```shell-session
$ docker compose ps

NAME                IMAGE      COMMAND                  SERVICE             CREATED            STATUS             PORTS
db-server           mysql      "docker-entrypoint.s…"   mysql               1 minutes ago      Up 1 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp
```

mysqlのコンテナが無事に起動しました。

```shell-session
$ docker compose logs mysql
db-server  | 2023-03-30 19:02:56+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
db-server  | 2023-03-30 19:02:56+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-server  | 2023-03-30 19:02:57+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
db-server  | 2023-03-30 19:02:57+09:00 [Note] [Entrypoint]: Initializing database files
db-server  | 2023-03-30 19:03:02+09:00 [Note] [Entrypoint]: Database files initialized
db-server  | 2023-03-30 19:03:02+09:00 [Note] [Entrypoint]: Starting temporary server
db-server  | 2023-03-30 19:03:03+09:00 [Note] [Entrypoint]: Temporary server started.
db-server  | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
db-server  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db-server  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db-server  | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
db-server  | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
db-server  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db-server  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db-server  | 2023-03-30 19:03:06+09:00 [Note] [Entrypoint]: Creating database xxxxxx
db-server  | 2023-03-30 19:03:06+09:00 [Note] [Entrypoint]: Creating user user
db-server  | 2023-03-30 19:03:06+09:00 [Note] [Entrypoint]: Giving user user access to schema xxxxx
db-server  |
db-server  | 2023-03-30 19:03:06+09:00 [Note] [Entrypoint]: Stopping temporary server
db-server  | 2023-03-30 19:03:08+09:00 [Note] [Entrypoint]: Temporary server stopped
db-server  |
db-server  | 2023-03-30 19:03:08+09:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
db-server  |
```

mysqlもちゃんと起動してくれたっぽいです。
めでたしめでたし。