質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

1回答

4053閲覧

docker compose up -d でコンテナが立ち上がらない。

ryomannn

総合スコア16

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2022/09/28 13:25

編集2022/09/29 00:41

前提

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周りの記述だと思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kazto

2022/09/29 00:46

docker compose up を -d なしで起動したときにログが出ますが、そこに何か情報はありませんか?
ryomannn

2022/09/29 01:30 編集

docker compose upでログが出るんですね。ありがとうございます。 ``` Attaching to api_contianer, db_contianer, front_contianer, web_contianer db_contianer | 2022-09-29 00:47:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started. db_contianer | 2022-09-29 00:47:22+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config db_contianer | command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.SbUX6kZRJf db_contianer | mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory) db_contianer | mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36. db_contianer | mysqld: [ERROR] Fatal error in defaults handling. Program aborted! db_contianer exited with code 1 front_contianer | npm ERR! code ENOENT front_contianer | npm ERR! syscall open front_contianer | npm ERR! path /app/package.json front_contianer | npm ERR! errno -2 front_contianer | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' front_contianer | npm ERR! enoent This is related to npm not being able to find a file. front_contianer | npm ERR! enoent front_contianer | front_contianer | npm ERR! A complete log of this run can be found in: front_contianer | npm ERR! /root/.npm/_logs/2022-09-29T00_47_22_561Z-debug-0.log front_contianer exited with code 254 ``` エラーが多数出てました。 mysqld: Can't read dir of '/etc/mysql/conf.d/ ここが原因かと思い、 https://stackoverflow.com/questions/49958575/docker-mysqld-cant-read-dir-of-etc-mysql-mysql-conf-d-os-errno-2-no-suc を参考に docker compose.ymlのdb-containerに ``` 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 command: ["mkdir", "/etc/mysql/conf.d"] healthcheck: test: ["CMD", "false"] interval: 1s timeout: 1s retries: 3 start_period: 1s ``` command: ["mkdir", "/etc/mysql/conf.d"] を追加してみましたが、 ``` Attaching to api_contianer, db_contianer, front_contianer, web_contianer db_contianer | mkdir: cannot create directory '/etc/mysql/conf.d': File exists db_contianer exited with code 1 front_contianer | npm ERR! code ENOENT front_contianer | npm ERR! syscall open front_contianer | npm ERR! path /app/package.json front_contianer | npm ERR! errno -2 front_contianer | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' front_contianer | npm ERR! enoent This is related to npm not being able to find a file. front_contianer | npm ERR! enoent front_contianer | front_contianer | npm ERR! A complete log of this run can be found in: front_contianer | npm ERR! /root/.npm/_logs/2022-09-29T01_08_27_314Z-debug-0.log front_contianer exited with code 254 ``` ログには mkdir: cannot create directory '/etc/mysql/conf.d': File existst とあり、既にあるとのことです。 front のエラーは解決できそう+多分今回のエラーには関係ないと思うので大丈夫です。
guest

回答1

0

ベストアンサー

docker-compose.ymlの内容から、以下のことが起きていると読み取れます。

  • build時、image内には/etc/mysql/conf.d が存在しており、作成しようとするとエラーとなる
  • build後、ボリュームをマウントする際、config/mysql には conf.d がないので、マウントされた後の /etc/mysql/conf.d は存在しないことになる
  • 起動時、上記ディレクトリがないのでエラーとなる
|--config/ | |--nginx/ | | |--nginx.conf | |--mysql/ | |--my.cnf

解決策としては、以下のいずれかになるかと思います。

  • config/mysql/conf.d という空のディレクトリを作成しておく
  • config/mysql/my.cnf を、/etc/mysql/conf.d/my.cnf にコピーする

投稿2022/09/29 02:12

kazto

総合スコア7196

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ryomannn

2022/09/29 04:39 編集

わかりやすい解説ありがとうございます。frontのエラーも同じでした。 - 対策① docker-compose.ymlのdbcontainer ``` - ./config/mysql:/etc/mysql/conf.d ``` のボリュームの部分をこのように書き換えることで対応しようとしましたが、 ``` docker compose runを実行すると ``` [+] Running 2/0 ⠿ Container front_contianer Running 0.0s ⠿ Container db_contianer Running 0.0s Attaching to api_contianer, db_contianer, front_contianer, web_contianer container for service "db" is unhealthy ``` とログも出てこずに終了してしまいます。 # 追記 db.envの環境変数を適当に書き換えて、書き換える間の状態に戻してから docker compose runするとログが出てきましたが対策➁と同じでした。 - 対策➁ ``` - ./config/mysql:/etc/mysql ``` のままでホストに/app/config/mysql/conf.d という空フォルダを作成してから、 docker compse up を実行すると ``` [+] Running 4/4 ⠿ Container front_contianer Running 0.0s ⠿ Container db_contianer Recreated 1.7s ⠿ Container api_contianer Recreated 0.1s ⠿ Container web_contianer Recreated 0.1s Attaching to api_contianer, db_contianer, front_contianer, web_contianer db_contianer | 2022-09-29 04:04:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started. db_contianer | 2022-09-29 04:04:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' db_contianer | 2022-09-29 04:04:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started. db_contianer | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock' db_contianer | 2022-09-29T04:04:59.259951Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead. db_contianer | 2022-09-29T04:04:59.261496Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.30) starting as process 1 db_contianer | 2022-09-29T04:04:59.270245Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. db_contianer | 2022-09-29T04:04:59.511238Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. db_contianer | 2022-09-29T04:04:59.849840Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. db_contianer | 2022-09-29T04:04:59.849902Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. db_contianer | 2022-09-29T04:04:59.853735Z 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. db_contianer | 2022-09-29T04:04:59.880904Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock db_contianer | 2022-09-29T04:04:59.881007Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.30' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. container for service "db" is unhealthy ``` 現在my.cnfはとりあえず空ファイルでやっているのですが、そこがダメなのでしょうか。 応急処置で https://github.com/songdeveloper/my.cnf/blob/master/my.cnf を使用してみても同じエラーが出ました。 Warningで強制的に停止はされないと思うのでよくわかりません。 なぜかマークダウンが利いていなくて見にくくなって申し訳ないです。
kazto

2022/09/29 05:11

コメント欄はマークダウンが効かないので、質問文を編集するのが良いでしょう。 my.cnfは空だと確かに問題が出そうですね。 可能であれば、同バージョンのmysqlのパッケージから取得したmy.cnfを使うのがベストですが、ログを見る限り提示されたURLから取得したものでも問題なさそうです。 つまり、ログからは、起動に成功しているように読めます。 この状態で、まだ接続できない感じでしょうか?
ryomannn

2022/09/29 06:36

いろいろ試しましたが、 docker-compose.ymlの healthcheckの理解が甘かったようです。 healthcheckのtestの部分に書くコマンドが、何でもいいかと思っていましたが 実際に実行できるコマンドじゃないといけなかったようです。(特に起動しているかの確認ができればいいと思っていたので、testに書くコマンドは何でもいいだろうと思ってどこかの記事のを参考にしていました。) おっしゃる通りそこ以外の部分では軌道は成功していました。 時間を割いていただいてありがとうございます!。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問