解決したいこと
Dockerを使用し、rails API + MySQL + Reactによりポートフォリオを作成中です。
docker-compose up
でrails API + MySQL + React用それぞれのコンテナを起動しようとしたところ、docker-compose.yml内のコマンドが実行できずにコンテナが停止してしまい、解決できずに困っています。
以下のエラー解決へのヒントをいただけると幸いです。
エラー内容
・docker compose ps
~/Documents/project/hanger-talk % docker-compose ps NAME COMMAND SERVICE STATUS PORTS hanger-talk-api-1 "entrypoint.sh sh -c…" api exited (1) hanger-talk-db-1 "docker-entrypoint.s…" db running 0.0.0.0:3306->3306/tcp hanger-talk-front-1 "docker-entrypoint.s…" front exited (2)
・docker compose logs
~/Documents/project/hanger-talk/front % docker-compose up [+] Running 3/3 ⠿ Container hanger-talk-db-1 Created 0.0s ⠿ Container hanger-talk-front-1 Recreated 0.2s ⠿ Container hanger-talk-api-1 Recreated 0.2s Attaching to hanger-talk-api-1, hanger-talk-db-1, hanger-talk-front-1 hanger-talk-front-1 | sh: 1: cd: can't cd to front hanger-talk-db-1 | 2022-03-13 21:41:27+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. hanger-talk-front-1 exited with code 2 hanger-talk-db-1 | 2022-03-13 21:41:27+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' hanger-talk-db-1 | 2022-03-13 21:41:27+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. hanger-talk-db-1 | 2022-03-13T12:41:27.611241Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. hanger-talk-db-1 | 2022-03-13T12:41:27.611316Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1 hanger-talk-db-1 | 2022-03-13T12:41:27.621779Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. hanger-talk-db-1 | 2022-03-13T12:41:27.802598Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. hanger-talk-db-1 | 2022-03-13T12:41:28.067553Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. hanger-talk-db-1 | 2022-03-13T12:41:28.067668Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. hanger-talk-db-1 | 2022-03-13T12:41:28.071121Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. hanger-talk-db-1 | 2022-03-13T12:41:28.117369Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock hanger-talk-db-1 | 2022-03-13T12:41:28.117658Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. hanger-talk-api-1 | => Booting Puma hanger-talk-api-1 | => Rails 6.1.5 application starting in development hanger-talk-api-1 | => Run `bin/rails server --help` for more startup options hanger-talk-api-1 | A server is already running. Check /hanger_talk/tmp/pids/server.pid. hanger-talk-api-1 | Exiting hanger-talk-api-1 exited with code 1
開発環境および前提事項
開発環境
・macOS Monterey v12.1
・ruby v3.0.3
・Rails v6.1.4.4
・mysql 8.0.28
・Docker 20.10.12
・Docker Compose v2.2.3
コードおよびディレクトリ構造
・docker-compose.yml
version: '3' services: db: image: mysql:8.0.28 volumes: - ./etc/my.cnf:/etc/mysql/conf.d/my.cnf - hanger_talk_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: <%= ENV['MYSQL_ROOT_PASSWORD'] %> TZ: 'Asia/Tokyo' command: --default-authentication-plugin=mysql_native_password ports: - 3306:3306 env_file: - ./api/.env tty: true api: build: context: ./api/ dockerfile: Dockerfile command: sh -c "rm -f /hanger_talk/api/tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'" image: rails:dev volumes: - ./api:/hanger_talk - ./api/vendor/bundle:/hanger_talk/vendor/bundle environment: TZ: 'Asia/Tokyo' RAILS_ENV: development ports: - 3001:3001 depends_on: - db links: - db front: build: context: ./front/ dockerfile: Dockerfile volumes: - ./front:/usr/src/app command: sh -c "cd front && yarn start" ports: - "3000:3000" tty: true volumes: hanger_talk_data: external: true
考えられる原因と確認したこと
上記のログから、React(front)とrails(=api)はそれぞれ、docker-compose.yml
の
command: sh -c "cd front && yarn start"
と
command: sh -c "rm -f /hanger_talk/api/tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'"
が動いていないとの結論にいたりました。
Reactはcdに失敗し、railsはコンテナ起動時にserver.pid
の削除ができずに怒られています。
しかし、なぜコマンドが動かないのかがわからずに困っています。
# docker compose logs ... hanger-talk-front-1 | sh: 1: cd: can't cd to front ... hanger-talk-api-1 | => Booting Puma hanger-talk-api-1 | => Rails 6.1.5 application starting in development hanger-talk-api-1 | => Run `bin/rails server --help` for more startup options hanger-talk-api-1 | A server is already running. Check /hanger_talk/tmp/pids/server.pid. hanger-talk-api-1 | Exiting # docker-compose.yml**ボールドテキスト** version: '3' services: db: ... command: --default-authentication-plugin=mysql_native_password <=おそらくこれも動いていない ... api: ... command: sh -c "rm -f /hanger_talk/api/tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'" ... front: ... command: sh -c "cd front && yarn start" ...
イメージのbuildからやり直したりなど簡単なことは試しましたが、同じような現象の記事が少なく未だ解決には至っておりません。
お忙しいところ申し訳ありませんが、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー