我正在尝试对以前在Heroku上运行的node.js应用进行docker化,并使用postgres插件作为生产数据库。无法从容器连接到数据库。
Docker文件
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm","start"]
Docker Compose文件
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
- "5432:5432"
env_file: .env
extra_hosts:
- "iondev:172.27.27.59"
错误
web_1 | {"level":30,"time":1553864230376,"msg":"Narrafy listening on port 5000","pid":16,"hostname":"961f47819c3e","v":1}
web_1 | {"level":50,"time":1553864230399,"msg":"Failed to connect to DB.","pid":16,"hostname":"961f47819c3e","v":1}
web_1 | {"level":50,"time":1553864230399,"msg":"Connection terminated unexpectedly","pid":16,"hostname":"961f47819c3e","type":"Error","stack":"Error: Connection terminated unexpectedly\n at Connection.con.once (/usr/src/app/node_modules/pg/lib/client.js:200:9)\n at Object.onceWrapper (events.js:313:30)\n at emitNone (events.js:106:13)\n at Connection.emit (events.js:208:7)\n at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:130:10)\n at emitNone (events.js:111:20)\n at Socket.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1064:12)\n at _combinedTickCallback (internal/process/next_tick.js:139:11)\n at process._tickCallback (internal/process/next_tick.js:181:9)","v":1}
答案 0 :(得分:0)
没有dockerfile,就无法调试此问题。最有可能检查您的数据库信用,并确保将它们作为ENV变量传递。
您可以使用以下命令查看日志 Docker日志<容器ID>。
或使用docker exec命令检查您的环境变量。