2016-09-27 5 views
1

私は、アプリケーションを起動する前にMongoサーバーがリッスンを開始するのを待つノードアプリケーション、Mongoデータベース、およびwaisbrot/waitという3つのサービスを持つdocker-compose.ymlを持っています。depends_onによって確立された起動順序を無視してdocker-composeを作成する

私がdocker-compose upを実行すると、まずコンパイルが開始され、依存関係を最初に起動するのではなく、すぐにアプリケーションサービスが開始されます。他のコンテナのいずれかを起動しようとしていることを示唆するために、コンソールに出力は表示されません。

Mongoに接続できないとアプリケーションサーバーがクラッシュするため、スタックの起動に失敗します。

version: '2' 
services: 
    wait: 
    image: waisbrot/wait 
    depends_on: 
     - mongo 
    environment: 
     - TARGETS=mongo:27017 
    app: 
    build: . 
    depends_on: 
     - wait 
    mongo: 
    image: mongo:3.2 
    volumes: 
     - /data/db 

私の意図、最初のmongoサービスを開始する第二の待機サービスを開始し、最後にアプリを起動することです。

は、ここに私のdocker-compose.ymlです。出力を見て、docker statsを見ても、アプリサービスだけが起動しているようです。ここdocker-compose upの出力です:

Building app 
Step 1 : FROM node:6.6 
---> c0d8845263e3 
Step 2 : COPY ./package.json /app/package.json 
---> Using cache 
---> db47c6c65663 
Step 3 : WORKDIR /app/ 
---> Using cache 
---> 30c3cadc2680 
Step 4 : RUN npm i 
---> Using cache 
---> 5ae6a18b2100 
Step 5 : COPY ./src/ /app/src/ 
---> Using cache 
---> dd64329a5fe2 
Step 6 : COPY ./config/ /app/config/ 
---> Using cache 
---> 1945706cdaac 
Step 7 : COPY ./public/ /app/public/ 
---> Using cache 
---> af6f6b7075c1 
Step 8 : EXPOSE 3030 
---> Using cache 
---> 4ecec8df6ef7 
Step 9 : RUN npm start 
---> Running in 04a543b58d85 
npm info it worked if it ends with ok 
npm info using [email protected] 
npm info using [email protected] 
npm info lifecycle [email protected]~prestart: [email protected] 
npm info lifecycle [email protected]~start: [email protected] 

> [email protected] start /app 
> node src/ 

Feathers application started on localhost:3030 

/app/node_modules/mongodb/lib/server.js:261 
     process.nextTick(function() { throw err; }) 
            ^
MongoError: failed to connect to server [mongo:27017] on first connect 
    at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:313:35) 
    at emitOne (events.js:96:13) 
    at Pool.emit (events.js:188:7) 
    at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:260:12) 
    at Connection.g (events.js:291:16) 
    at emitTwo (events.js:106:13) 
    at Connection.emit (events.js:191:7) 
    at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:162:49) 
    at Socket.g (events.js:291:16) 
    at emitOne (events.js:96:13) 
    at Socket.emit (events.js:188:7) 
    at connectErrorNT (net.js:1015:8) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

npm info lifecycle [email protected]~start: Failed to exec start script 
npm ERR! Linux 4.4.20-moby 
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start" 
npm ERR! node v6.6.0 
npm ERR! npm v3.10.3 
npm ERR! code ELIFECYCLE 
npm ERR! [email protected] start: `node src/` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node src/'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the family-communication package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  node src/ 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs family-communication 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls family-communication 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  /app/npm-debug.log 
ERROR: Service 'app' failed to build: The command '/bin/sh -c npm start' returned a non-zero code: 1 

私は期待通り、これは動作しないのはなぜ?あなたのDockerfileにコマンド

答えて

2
RUN npm start 

は、実際に他のコンテナがまだ存在していないとき、ビルド時にWebサーバを開始しています。おそらくあなたが望むでしょう

CMD npm start 
+0

また、「待機」コンテナはあなたが望むことをしません。 1つのコマンドでこれを使用して、コンテナが開始されたことを確認し、次のコマンドで別のコンテナを実行します。 Docker-composeは、終了を待たずに起動します。再試行/接続ロジックをWebappに追加するか、Webappコンテナのエントリポイントを追加する必要があります。 –

+0

あなたは絶対に正しいです。私は完全にそれを逃した。ありがとうございました!私は待っているコンテナであなたに従っているかどうかはわかりません。あなたは、そのコンテナがアプリケーションが動作する前に私のmongoコンテナが確実に起動しているとは限りません。私はこの例に従った:https://medium.com/@edgar/how-to-wait-for-a-container-to-be-ready-before-starting-another-container-using-docker-compose-92bab2fc1633# .fmp63xl5h – raddevon

+0

これはアプリの前に開始されますが、接続を受け入れる準備ができていてもいなくてもかまいません。気がついたら、彼は別のコマンドで 'wait'コンテナを使用します。彼は 'up'を実行し、' wait'を実行してからhistテストを実行します。 –

関連する問題