docker compose 后立即关闭 Postgres?

Posted

技术标签:

【中文标题】docker compose 后立即关闭 Postgres?【英文标题】:Postgres Shutdown Immediately after docker compose up? 【发布时间】:2017-09-26 17:13:24 【问题描述】:

我在这里使用postgresql image 和node official image 和 我的docker-compose.yml 文件看起来像

version: '2'
services:
  postgres:
    restart: always
    image: sameersbn/postgresql:9.6-2
    ports:
      - "5432:5432"
    environment:
      - DB_USER=shorturl
      - DB_PASS=shorturl
      - DB_NAME=shorturl
  web:
    build: .
    ports:
     - "4000:4000"
    volumes:
     - .:/shortlr
    depends_on:
     - postgres
    command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]

当我运行 docker-compose up 时,我的日志看起来像

shubham@shuboy2014:~/shortlr$ docker-compose up
Recreating shortlr_postgres_1 ... 
Recreating shortlr_postgres_1 ... done
Recreating shortlr_web_1 ... 
Recreating shortlr_web_1 ... done
Attaching to shortlr_postgres_1, shortlr_web_1
postgres_1  | Initializing datadir...
web_1       | wait-for-it.sh: waiting 15 seconds for postgres:5432
postgres_1  | Initializing certdir...
postgres_1  | Initializing logdir...
postgres_1  | Initializing rundir...
postgres_1  | Setting resolv.conf ACLs...
postgres_1  | Creating database user: shorturl
postgres_1  | Creating database: shorturl...
postgres_1  | ‣ Granting access to shorturl user...
postgres_1  | Starting PostgreSQL 9.6...
postgres_1  | LOG:  database system was shut down at 2017-04-28 14:23:20 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  autovacuum launcher started
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  incomplete startup packet
web_1       | wait-for-it.sh: postgres:5432 is available after 2 seconds
web_1       | npm info it worked if it ends with ok
web_1       | npm info using npm@4.2.0
web_1       | npm info using node@v7.9.0
web_1       | npm info lifecycle shortlr@0.0.5~prestart: shortlr@0.0.5
web_1       | npm info lifecycle shortlr@0.0.5~start: shortlr@0.0.5
web_1       | 
web_1       | > shortlr@0.0.5 start /shortlr
web_1       | > node server.js
web_1       | 
web_1       | Listening on http://localhost:4000/
web_1       | Unhandled rejection SequelizeBaseError: connect ECONNREFUSED 127.0.0.1:5432
web_1       |     at /shortlr/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20
web_1       |     at Connection.<anonymous> (/shortlr/node_modules/pg/lib/client.js:186:5)
web_1       |     at emitOne (events.js:96:13)
web_1       |     at Connection.emit (events.js:191:7)
web_1       |     at Socket.<anonymous> (/shortlr/node_modules/pg/lib/connection.js:86:10)
web_1       |     at emitOne (events.js:96:13)
web_1       |     at Socket.emit (events.js:191:7)
web_1       |     at emitErrorNT (net.js:1283:8)
web_1       |     at _combinedTickCallback (internal/process/next_tick.js:80:11)
web_1       |     at process._tickCallback (internal/process/next_tick.js:104:9)

当我运行docker-compose up 时,postgresql 立即关闭,任何有用的答案将不胜感激。

shubham@shuboy2014:~/shortlr$ docker-compose ps
       Name                     Command               State           Ports          
------------------------------------------------------------------------------------
shortlr_postgres_1   /sbin/entrypoint.sh              Up      0.0.0.0:5432->5432/tcp 
shortlr_web_1        ./wait-for-it.sh postgres: ...   Up      0.0.0.0:4000->4000/tcp 

【问题讨论】:

***.com/q/37259584/905902 @wildplasser 我的问题没有解决 你没有在这里链接任何东西。首先在您的网络应用程序中添加一个链接部分。然后在您的网络应用程序中使用该链接。 Web 容器的 localhost 接口上没有 db。 Pgsql 已启动,只是不在您在节点应用程序中指定的位置。链接容器后,您的数据库位置将类似于 postgres:5432 而不是 localhost。 【参考方案1】:

在你的 web 和 postgres 图像之间添加一个链接,如下所示:

version: '2'
services:
  postgres:
    restart: always
    image: sameersbn/postgresql:9.6-2
    ports:
      - "5432:5432"
    environment:
      - DB_USER=shorturl
      - DB_PASS=shorturl
      - DB_NAME=shorturl
  web:
    build: .
    ports:
     - "4000:4000"
    volumes:
     - .:/shortlr
    depends_on:
     - postgres
    links:
     - postgres
    command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]

尽管您应该改用网络,因为链接是一个旧选项。阅读网络上的文档here

【讨论】:

以上是关于docker compose 后立即关闭 Postgres?的主要内容,如果未能解决你的问题,请参考以下文章

docker-compose:如何立即查看文件更改(开发时)

Docker Compose 保持容器运行

当 docker-compose 的一部分时,自定义 nginx 容器立即退出

docker-compose 和优雅的 Celery 关闭

docker之docker-compose——容器管理

如何使用 pycharm 调试在 docker-compose 中运行的进程