[英]docker-compose execute command in main service which depends on other services
在項目中,我有一個Dockerfile
用nodejs
圖像。 在docker-compose.yml
文件中,我有兩個服務:從Dockerfile
構建的server
和使用postgres
映像的db
。
Dockerfile
獲取映像,為項目創建目錄,運行npm install
並設置CMD ["npm", "start"]
。
我docker-compose.yml
的內容:
version: "2"
services:
server:
build: .
ports:
- "5000:5000"
volumes:
- ./:/usr/src/app
links:
- db
db:
image: postgres:9.6.2
ports:
- "5432:5432"
volumes:
- db_data:/var/lib/postgres
restart: always
environment:
POSTGRES_PASSWORD: devel
POSTGRES_USER: devel
POSTGRES_DB: devel
volumes:
db_data:
在我的項目中,我將db-migrate
與db-migrate-pg
驅動程序配合使用,當我嘗試執行遷移時,出現下一個錯誤:7
[ERROR] Error: connect ECONNREFUSED 127.0.0.1:5432
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1136:14)
npm info lifecycle project-js@1.0.0~migrate-up: Failed to exec migrate-up script
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project-js@1.0.0 migrate-up: `db-migrate up`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project-js@1.0.0 migrate-up script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-07-12T17_31_10_049Z-debug.log
數據庫的配置正確,因為我可以在PgAdmin上使用它。 我不知道如何訪問顯示錯誤的文件,因為容器在執行結束后停止了運行。
編輯:
這是錯誤中命名文件的完整輸出:
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'run',
1 verbose cli 'migrate-up' ]
2 info using npm@5.0.0
3 info using node@v8.0.0
4 verbose run-script [ 'premigrate-up', 'migrate-up', 'postmigrate-up' ]
5 info lifecycle project-js@1.0.0~premigrate-up: project-js@1.0.0
6 silly lifecycle project-js@1.0.0~premigrate-up: no script for premigrate-up, continuing
7 info lifecycle project-js@1.0.0~migrate-up: project-js@1.0.0
8 verbose lifecycle project-js@1.0.0~migrate-up: unsafe-perm in lifecycle true
9 verbose lifecycle project-js@1.0.0~migrate-up: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/src/app/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
10 verbose lifecycle project-js@1.0.0~migrate-up: CWD: /usr/src/app
11 silly lifecycle project-js@1.0.0~migrate-up: Args: [ '-c', 'db-migrate up' ]
12 silly lifecycle project-js@1.0.0~migrate-up: Returned: code: 1 signal: null
13 info lifecycle project-js@1.0.0~migrate-up: Failed to exec migrate-up script
14 verbose stack Error: project-js@1.0.0 migrate-up: `db-migrate up`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:283:16)
14 verbose stack at emitTwo (events.js:125:13)
14 verbose stack at EventEmitter.emit (events.js:213:7)
14 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack at emitTwo (events.js:125:13)
14 verbose stack at ChildProcess.emit (events.js:213:7)
14 verbose stack at maybeClose (internal/child_process.js:887:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:208:5)
15 verbose pkgid project-js@1.0.0
16 verbose cwd /usr/src/app
17 verbose Linux 4.8.0-58-generic
18 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "migrate-up"
19 verbose node v8.0.0
20 verbose npm v5.0.0
21 error code ELIFECYCLE
22 error errno 1
23 error project-js@1.0.0 migrate-up: `db-migrate up`
23 error Exit status 1
24 error Failed at the project-js@1.0.0 migrate-up script.
24 error This is probably not a problem with npm. There is likely additional logging output above.
25 verbose exit [ 1, true ]
從server
容器的角度來看,postgres在這里:
db:5432
每個容器都有自己的網絡接口,因此每個容器都有自己的本地主機。
docker-compose
為您的容器創建一個網絡並將其放入其中。 作為獎勵,您可以獲得DNS解析,然后他們可以使用他們的服務名稱互相交談。
問:我不知道如何訪問顯示錯誤的文件,因為容器在執行結束后停止了。
做這個 :
last_stopped_container=$(docker ps -q -l)
docker cp $last_stopped_container:/root/.npm/_logs/2017-07-12T17_31_10_049Z-debug.log debug.log
cat debug.log
另外,請使用depends_on
而不是links
以獲得一些依賴啟動:
depends_on:
- db
正如我所看到的,您在服務器啟動的一開始就在運行db-migrations。 這可能導致在短時間內無法使用PostgreSQL。 如果是這樣,請執行以下操作:
docker-compose up -d db
# wait just a few seconds
docker-compose up server
這不是很優雅。 因此,您將需要使用更強大的解決方案: https : //github.com/vishnubob/wait-for-it 。 下載該腳本,將其放在服務器映像中並按以下方式使用:
server:
build: .
command: /wait-for-it.sh db:5432 -- npm run #or whatever node command you have
ports:
- "5000:5000"
volumes:
- ./:/usr/src/app
links:
- db
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.