dockerでmysqlコンテナを起動させてもExitになり、落ちるので解消してみた

Laravel/PHP
スポンサーリンク

はじめに

Laravel Mixで開発中に、dockerでmysqlコンテナを起動させてもExitになり、落ちる問題が発生した。対処するのにかなーーーーーーーーーりの時間を要したので、備忘録として記載しています。

開発状況(Version)

macOS Big Sur
Laravel Mix 6.0
Laravel 8.83.13
PHP 7.3.29
node.js 16.13.2
Vue.js 2.6.14
MySQL 8.0.30

※ココから先のシェル名の「Bash」となっている箇所は全て「zsh」です。
ブログのコードエディターの言語設定に「zsh」がないため、「Bash」にしています。

現状の状態

実施コマンド

// イメージをキャッシュを使用せずに、ビルド 
docker-compose build --no-cache

// 起動
docker compose up -d

//コンテナの状態を表示
docker-compose ps

//docker内のMySQLコンテナのログを50件出す ※〇〇〇〇はコンテナ名
docker logs --tail 50 --follow --timestamps 〇〇〇〇    

docker-compose ps、コンテナの状態

              Name                            Command               State                 Ports              
-------------------------------------------------------------------------------------------------------------
portfolio_app_1                 docker-php-entrypoint php-fpm    Up       9000/tcp                        
portfolio_container_mailhog_1   MailHog                          Up       1025/tcp, 0.0.0.0:8025->8025/tcp
portfolio_db_1                  docker-entrypoint.sh mysqld      Exit 1                                   
portfolio_web_1                 /docker-entrypoint.sh ngin ...   Up       0.0.0.0:80->80/tcp              
phpmyadmin-la                   /docker-entrypoint.sh apac ...   Up       0.0.0.0:8080->80/tcp     

docker内のMySQLコンテナのログ

2022-09-12T21:37:29.256580451Z chown: changing ownership of './sys/module/xen_privcmd/uevent': Read-only file system
2022-09-12T21:37:29.256582721Z chown: changing ownership of './sys/module/xen_privcmd/parameters': Read-only file system
2022-09-12T21:37:29.256584961Z chown: changing ownership of './sys/module/xen_privcmd/parameters/dm_op_max_nr_bufs': Read-only file system
2022-09-12T21:37:29.256587304Z chown: changing ownership of './sys/module/xen_privcmd/parameters/dm_op_buf_max_size': Read-only file system
2022-09-12T21:37:29.256589564Z chown: changing ownership of './sys/module/virtio_blk': Read-only file system
2022-09-12T21:37:29.256718857Z chown: changing ownership of './sys/module/virtio_blk/uevent': Read-only file system
2022-09-12T21:37:29.256729325Z chown: changing ownership of './sys/module/virtio_blk/parameters': Read-only file system
2022-09-12T21:37:29.256731880Z chown: changing ownership of './sys/module/virtio_blk/parameters/queue_depth': Read-only file system
2022-09-12T21:37:29.256734202Z chown: changing ownership of './sys/module/ip_vs': Read-only file system
2022-09-12T21:37:29.256736421Z chown: changing ownership of './sys/module/ip_vs/uevent': Read-only file system
2022-09-12T21:37:29.256738776Z chown: changing ownership of './sys/module/ip_vs/parameters': Read-only file system
2022-09-12T21:37:29.256740972Z chown: changing ownership of './sys/module/ip_vs/parameters/conn_tab_bits': Read-only file system
2022-09-12T21:37:29.256743151Z chown: changing ownership of './sys/module/dm_bufio': Read-only file system
2022-09-12T21:37:29.256745360Z chown: changing ownership of './sys/module/dm_bufio/uevent': Read-only file system
2022-09-12T21:37:29.256747561Z chown: changing ownership of './sys/module/dm_bufio/parameters': Read-only file system
2022-09-12T21:37:29.256784207Z chown: changing ownership of './sys/module/dm_bufio/parameters/allocated_vmalloc_bytes': Read-only file system
2022-09-12T21:37:29.256793775Z chown: changing ownership of './sys/module/dm_bufio/parameters/current_allocated_bytes': Read-only file system
2022-09-12T21:37:29.256796542Z chown: changing ownership of './sys/module/dm_bufio/parameters/allocated_kmem_cache_bytes': Read-only file system
2022-09-12T21:37:29.256801607Z chown: changing ownership of './sys/module/dm_bufio/parameters/max_cache_size_bytes': Read-only file system
2022-09-12T21:37:29.256803878Z chown: changing ownership of './sys/module/dm_bufio/parameters/max_age_seconds': Read-only file system
2022-09-12T21:37:29.256806095Z chown: changing ownership of './sys/module/dm_bufio/parameters/retain_bytes': Read-only file system
2022-09-12T21:37:29.256808286Z chown: changing ownership of './sys/module/dm_bufio/parameters/peak_allocated_bytes': Read-only file system
2022-09-12T21:37:29.256810490Z chown: changing ownership of './sys/module/dm_bufio/parameters/allocated_get_free_pages_bytes': Read-only file system
2022-09-12T21:37:29.256812774Z chown: changing ownership of './sys/module/virtio_net': Read-only file system
2022-09-12T21:37:29.256814971Z chown: changing ownership of './sys/module/virtio_net/uevent': Read-only file system
2022-09-12T21:37:29.256919412Z chown: changing ownership of './sys/module/virtio_net/parameters': Read-only file system
2022-09-12T21:37:29.256934910Z chown: changing ownership of './sys/module/virtio_net/parameters/napi_weight': Read-only file system
2022-09-12T21:37:29.256938344Z chown: changing ownership of './sys/module/virtio_net/parameters/csum': Read-only file system
2022-09-12T21:37:29.256940726Z chown: changing ownership of './sys/module/virtio_net/parameters/napi_tx': Read-only file system
2022-09-12T21:37:29.256943037Z chown: changing ownership of './sys/module/virtio_net/parameters/gso': Read-only file system
2022-09-12T21:37:29.256945242Z chown: changing ownership of './sys/module/workqueue': Read-only file system
2022-09-12T21:37:29.256947423Z chown: changing ownership of './sys/module/workqueue/uevent': Read-only file system
2022-09-12T21:37:29.256949794Z chown: changing ownership of './sys/module/workqueue/parameters': Read-only file system
2022-09-12T21:37:29.256952008Z chown: changing ownership of './sys/module/workqueue/parameters/watchdog_thresh': Read-only file system
2022-09-12T21:37:29.256954511Z chown: changing ownership of './sys/module/workqueue/parameters/disable_numa': Read-only file system
2022-09-12T21:37:29.256956707Z chown: changing ownership of './sys/module/workqueue/parameters/debug_force_rr_cpu': Read-only file system
2022-09-12T21:37:29.256958903Z chown: changing ownership of './sys/module/workqueue/parameters/power_efficient': Read-only file system
2022-09-12T21:37:29.256961093Z chown: changing ownership of './sys/module/tpm': Read-only file system
2022-09-12T21:37:29.256963221Z chown: changing ownership of './sys/module/tpm/uevent': Read-only file system
2022-09-12T21:37:29.256965353Z chown: changing ownership of './sys/module/tpm/parameters': Read-only file system
2022-09-12T21:37:29.256967585Z chown: changing ownership of './sys/module/tpm/parameters/suspend_pcr': Read-only file system
2022-09-12T21:37:29.256969782Z chown: changing ownership of './sys/module/tpm/version': Read-only file system
2022-09-12T21:37:29.256971956Z chown: changing ownership of './sys/module/sr_mod': Read-only file system
2022-09-12T21:37:29.256974093Z chown: changing ownership of './sys/module/sr_mod/uevent': Read-only file system
2022-09-12T21:37:29.256976228Z chown: changing ownership of './sys/module/sr_mod/parameters': Read-only file system
2022-09-12T21:37:29.256978368Z chown: changing ownership of './sys/module/sr_mod/parameters/xa_test': Read-only file system
2022-09-12T21:37:29.256980534Z chown: changing ownership of './sys/module/ip_vs_ftp': Read-only file system
2022-09-12T21:37:29.256994181Z chown: changing ownership of './sys/module/ip_vs_ftp/uevent': Read-only file system
2022-09-12T21:37:29.257003463Z chown: changing ownership of './sys/module/ip_vs_ftp/parameters': Read-only file system
2022-09-12T21:37:29.257006169Z chown: changing ownership of './sys/module/ip_vs_ftp/parameters/ports': Read-only file system

構造

Portfoilo
│    ├─ php
│    │   └─ Dockerfile
│    │   └─ php.ini
│    ├─ nginx
│    │    └─ Dockerfile
│    │    └─ default.conf
│    └─ mysql
│         └─ Dockerfile
│         └─ my.cnf
│    
├─ src
│    └─ Laravelのディレクトリ
│─ .env
│─ .gitignore   
└─ docker-compose.yml

ファイル各種

FROM mysql:8.0.30

ENV TZ=UTC

COPY ./docker/mysql/my.cnf /etc/my.cnf
[mysqld]
user=mysql
character_set_server = utf8mb4
collation_server = utf8mb4_0900_ai_ci

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# Error Log
log-error = mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file = mysql-slow.log
long_query_time = 1.0
log_queries_not_using_indexes = 0

# General Log
general_log = 1
general_log_file = mysql-general.log

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4
version: '3.8'

------------ 省略 ------------

  db:
    image: mysql:8.0.30
    build:
      context: .
      dockerfile: ./docker/mysql/Dockerfile
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      TZ: 'Asia/Tokyo'
    volumes:
      - mysql-volume:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    links:
      - db
    ports:
      - ${PMA_PORT}:80
    environment:
      PMA_ARBITRARY: ${PMA_ARBITRARY}
      PMA_HOST: ${PMA_HOST}
      PMA_USER: ${PMA_USER}
      PMA_PASSWORD: ${PMA_PASSWORD}
    container_name: "phpmyadmin-la"

 ------------ 省略 ------------

結論

./docker/mysql/my.cnfのログに関する記述を全て削除(コメントアウト)したら、エラーなく起動することできました。
こんな感じで、削除(コメントアウト)

[mysqld]
user=mysql
character_set_server = utf8mb4
collation_server = utf8mb4_0900_ai_ci

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# Error Log                              ←ここから
# log-error = mysql-error.log

# Slow Query Log
# slow_query_log = 1
# slow_query_log_file = mysql-slow.log
# long_query_time = 1.0
# log_queries_not_using_indexes = 0

# General Log
# general_log = 1
# general_log_file = mysql-general.log    ←ここまで

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

ちなみに試したこと

今まで問題なく起動し使用できていた為、dockerの問題だと思い、下記コマンドを実行しました。
この時は、まだ、dockerのログを見ることをしていなかった。。。(恥ずかしい)

//キャシュを使用しないビルド
% docker-compose build --no-cache

//コンテナの削除
% docker rm `docker ps -a -q`

//コンテナの最新化
% docker-compose stop
% docker-compose rm --force
% docker-compose build
% docker images | awk '/<none/{print $3}' | xargs docker rmi
% docker-compose up

//Docker for Macの再起動

//PCの再起動

ちなみに、phpmyadminを使用しているのですが、こちらではこの様なエラーメッセージが表示されていました。このメッセージからは、絶対にわからん。
Connection refused。。。ググると、DB_PORTが異なる場合に表示されるメッセージですやん。

最後に

よくエラーが出た際は、ログ見ろ!!!と言われますが、まさにその通りですね。エラーメッセージを信用して、ずっとPORTを確かめていました。。。。2週間ぐらい戦っていましたがやっと解消し、脳汁ブシャー状態です。これが、癖になるんですよね\(^o^)/

参考記事

コメント