前提
docker compose up -dを実行しても起動されない。
ホストの環境
ubuntu20.04.5LTS (wsl2)
発生している問題・エラーメッセージ
docker comoseでreact,mysql,nginx,railsのコンテナを一度に立ち上げようと思っています。
docker compose up -d で立ち上げようとすると
[+] Running 3/4 ⠿ Container front_contianer Started 1.2s ⠿ Container db_contianer Waiting 261.3s ⠿ Container api_contianer Recreated 0.1s ⠿ Container api_contianer Recreated
Container db_contianer Waiting の部分でタイムアウトしてしまいます。
コンテナの依存関係で、api_contianer ,api_contianer が必ず先に立ち上がるようにして
いるのでずっとwaitingになっていると予想しています。
ただエラー文も全く出てこないのでとっかかりがない状態です。
Started ではなくRecreated となっているapi_contianerの設定がどこか間違っている可能性が高いと思っています。
関係のありそうなファイルを書きます。api_contianerと
フォルダ構成
/app |--frontend/ | |--src/ //開発用フォルダ | |--build/ //バンドルファイル出力フォルダ | |--Dockerfile //node.js用 | |--package.json //npm依存パッケージ | |--package.lock.json //npm依存パッケージ | |--webpack.common.js //webpack共通設定ファイル | |--webpack.dev.js //webpack開発環境用設定ファイル | |--webpack.prod.js //webpack開発本番用設定ファイル | |--tsconfig.json //typescript設定ファイル | |--postcss.config.js //postcss設定ファイル | |--babel.config.js //babel(バンドル)用設定ファイル | |--.prettierrc.js //prettier(コード整形)用設定ファイル | |--.eslintrc.js //.eslintrc.js(コード検証)用設定ファイル | |--.browserslistrc //どのブラウザまで対応するか設定 | |--.dockerignore //biuld時に無視するファイルを設定 | |--front.env //node.jsコンテナ用で使用する環境変数(開発本番共通) | |--backend/ | |-src/ //開発用フォルダ | |--Dockerfile //ruby用 | |--Gemfile //bundle依存パッケージ | |--Gemfile.lock //bundle依存パッケージ | |--entorypoint.sh | |--api.env //rubyコンテナ用環境変数ファイル | |--.dockerignore //build時に無視するファイル設定 |--config/ | |--nginx/ | | |--nginx.conf | |--mysql/ | |--my.cnf |--.env //ホスト用環境変数ファイル |--db.env //mysqlコンテナ用環境変数ファイル |--web.env //nginxコンテナ用環境変数ファイル |--docker-cmpose.yml //複数コンテナ自動起動ファイル
/app/backend/Dockerfile
FROM ruby:alpine3.16 AS dev WORKDIR /app #RUN useradd ruby && chown -R ruby /app #User ruby COPY Gemfile Gemfile.lock . RUN bundle install COPY . . COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 4000
/app/frontend/Dockerfile
#開発環境用 FROM node:18.9.0-alpine3.15 AS dev ENV NODE_ENV=development ENV ROOT=/app WORKDIR /app #RUN useradd node && chown -R node /app #User node COPY package.json . RUN npm install --no-progress COPY . . EXPOSE 3000 CMD ["npm","start"] #本番環境パッケージインストール+ビルド FROM node:18.9.0-alpine3.15 AS build-stage ENV NODE_ENV=production WORKDIR /app RUN useradd node && chown -R node /app User node COPY package.json package-lock.json RUN npm install --production --no-progress RUN npm run build #本環境ランタイム用 FROM node:18.9.0-alpine3.15 AS prod WORKDIR /app RUN useradd node && chown -R node /app ENV NODE_ENV=production User node COPY --from=build-stage /app/build ./build COPY --from=build-stage /app/node_modules ./node_modules EXPOSE 3000 CMD ["npm","start"]
app/docker-compose.yml
version: '3.9' services: web: image: nginx:1.23-alpine container_name: web_contianer env_file: - web.env ports: - "8080:8080" volumes: - ./config/nginx:/etc/nginx/conf.d depends_on: frontend: condition: service_healthy api: condition: service_healthy db: image: mysql:8.0 container_name: db_contianer env_file: - db.env ports: - "3306:3306" volumes: - db-data:/var/lib/mysql - ./config/mysql:/etc/mysql healthcheck: test: exit 0 interval: 1s timeout: 1s retries: 3 start_period: 1s frontend: build: context: ./frontend dockerfile: Dockerfile target: ${BUILD_MODE} container_name: front_contianer env_file: - ./frontend/front.env ports: - "3000:3000" volumes: - ./fontend:/app healthcheck: test: exit 0 interval: 1s timeout: 1s retries: 3 start_period: 1s tty: true stdin_open: true api: build: context: ./backend dockerfile: Dockerfile target: ${BUILD_MODE} container_name: api_contianer env_file: - ./backend/api.env ports: - "4000:4000" volumes: - ./fontend:/app depends_on: db: condition: service_healthy healthcheck: test: exit 0 interval: 1s timeout: 1s retries: 3 start_period: 1s tty: true stdin_open: true volumes: db-data: driver: local
app/backend/entorypoint.sh
#!/bin/bash set -e rm -f /app/tmp/pids/server.pid exec "$@"
app/db.env
MYSQL_DATABASE=dev MYSQL_USER=mysql MYSQL_PASSWORD=password
app/.env
BUILD_MODE=dev
おそらくほかのファイルは空でも作成してあれば、動くと思います。
追記
あれから少し調べると、
app/docker-compose.ymlのapiコンテナの部分
api: build: context: ./backend dockerfile: Dockerfile target: ${BUILD_MODE} container_name: api_contianer env_file: - ./backend/api.env ports: - "4000:4000" volumes: - ./fontend:/app depends_on: db: condition: service_healthy healthcheck: test: exit 0 interval: 1s timeout: 1s retries: 3 start_period: 1s tty: true stdin_open: true
のボリュームが./fontend:/appとなっていてfrontendコンテナと被ってしまっていました。
volumes: - ./backend:/app
と修正し
docker compose up -dとしたら
[+] Running 3/4 ⠿ Container front_contianer Started 0.9s ⠿ Container db_contianer Waiting 207.6s ⠿ Container api_contianer Recreated 0.1s ⠿ Container web_contianer Recreated
api_containerが立ち上がり、front_containerも立ち上がっているので
web_containerも立ち上がりました。
なので原因は db_container周りの記述だと思います。

回答1件
あなたの回答
tips
プレビュー