我正在构建一个节点,express,postgres Web应用程序,
npm run dev
工作正常,但是我尝试使用docker在容器中注册我的应用程序,我有2个文件Dockerfile和docker-compose.yml,下面都列出了内容,并且出现了以下错误。
有人可以帮助我解决此错误。另外,在启动docker-compose up
之前,请确保端口5432中没有正在运行的进程。
node_1 | (node:28) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5432
node_1 | at Object._errnoException (util.js:992:11)
node_1 | at _exceptionWithHostPort (util.js:1014:20)
node_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
node_1 | (node:28) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
node_1 | (node:28) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Dockerfile
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]
docker-compose.yml
version: '2'
services:
node:
build: .
command: npm run dev
volumes:
- .:/usr/src/app/
- /usr/src/app/node_modules
ports:
- "3000:3000"
depends_on:
- postgres
links:
- postgres
environment:
DATABASE_URL: postgres://localhost:5432/dreamhouse
postgres:
image: postgres:9.6.2-alpine
ports:
- "5432:5432"
environment:
POSTGRES_USER:
POSTGRES_DB: dreamhouse`
答案 0 :(得分:0)
好像您的节点服务器在准备接受连接之前尝试连接到postgres。即使您使用.apk
仅保证postgres服务器正在运行。
您可以做的是使用脚本使节点服务器在启动之前等待postgres准备就绪。一种方法是将bash脚本与wait-for-it一起使用。您可以使用类似这样的内容:
start_node.sh
depends_on
docker-compose.yml
#!/bin/bash
# This line says for the "wait-for-it" script to wait for localhost:5432 without timeout (-t 0)
./wait-for-it.sh localhost:5432 -t 0
# When "wait-for-it" is done, you can start your server
npm run dev