2017-07-12 80 views
0

在我的項目中,我有一個Dockerfilenodejs圖像。在docker-compose.yml文件我有兩個服務:serverDockerfiledb建立,它使用postgres圖像。docker-compose執行命令在主要服務取決於其他服務

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-migratedb-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 [email protected]~migrate-up: Failed to exec migrate-up script 
npm ERR! code ELIFECYCLE 
npm ERR! errno 1 
npm ERR! [email protected] migrate-up: `db-migrate up` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] 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 [email protected] 
3 info using [email protected] 
4 verbose run-script [ 'premigrate-up', 'migrate-up', 'postmigrate-up' ] 
5 info lifecycle [email protected]~premigrate-up: [email protected] 
6 silly lifecycle [email protected]~premigrate-up: no script for premigrate-up, continuing 
7 info lifecycle [email protected]~migrate-up: [email protected] 
8 verbose lifecycle [email protected]~migrate-up: unsafe-perm in lifecycle true 
9 verbose lifecycle [email protected]~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 [email protected]~migrate-up: CWD: /usr/src/app 
11 silly lifecycle [email protected]~migrate-up: Args: [ '-c', 'db-migrate up' ] 
12 silly lifecycle [email protected]~migrate-up: Returned: code: 1 signal: null 
13 info lifecycle [email protected]~migrate-up: Failed to exec migrate-up script 
14 verbose stack Error: [email protected] 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 [email protected] 
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 [email protected] migrate-up: `db-migrate up` 
23 error Exit status 1 
24 error Failed at the [email protected] 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 ] 

回答

0

從視server容器點,Postgres的是在這裏:

db:5432 

每容器有自己的網絡接口,因此每個容器都有自己的本地主機。
docker-compose爲您的容器創建一個網絡並將其放入其中。作爲獎勵,您可以獲得DNS解析,然後他們可以使用他們的服務名稱互相交談。


Q. I don't know how to access the file that says the error because the container is stopped after it ends the execution.

this

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爲了得到一些dependecy啓動:

depends_on: 
    - db 

而且,我可以SE e,在服務器啓動剛剛開始時,您正在運行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 
+0

是否需要將端口添加到'links'部分的'db'行? 我更新了我的問題以添加文件的輸出。 – abaracedo

+0

@abaracedo,不,你沒有。我也發佈了 – Robert

+0

我也得到了同樣的錯誤:( – abaracedo