简体   繁体   English

Docker NodeJS 应用程序无法连接到 postgres 数据库

[英]Docker NodeJS app cant connect to postgres database

I am farely new to docker and docker-compose.我对 docker 和 docker-compose 非常陌生。 I tried to spin up a few services using docker which contain of a nodejs (Nest.js) api, a postgres db and pgadmin.我尝试使用包含 nodejs (Nest.js) api、postgres db 和 pgadmin 的 docker 启动一些服务。 Without the API (nodejs) app beeing dockerized I could connect to the docker database containers, but now that I also have dockerized the node app, it is not connecting anymore and I am clueless why.如果没有 API (nodejs) 应用程序被 docker 化,我可以连接到 docker 数据库容器,但现在我也已经对节点应用程序进行了 docker 化,它不再连接,我不知道为什么。 Is there anything wrong with the way I have set it up?我设置的方式有什么问题吗?

Here is my docker-compose file这是我的 docker-compose 文件

 version: "3" services: nftapi: env_file: -.env build: context: . ports: - '5000:5000' depends_on: - postgres volumes: -.:/app - /app/node_modules networks: - postgres postgres: container_name: postgres image: postgres:latest ports: - "5432:5432" volumes: - /data/postgres:/data/postgres env_file: - docker.env networks: - postgres pgadmin: links: - postgres:postgres container_name: pgadmin image: dpage/pgadmin4 ports: - "8080:80" volumes: - /data/pgadmin:/root/.pgadmin env_file: - docker.env networks: - postgres networks: postgres: driver: bridge

This is the nodejs app Dockerfile which builds successfully and in the logs I see the app is trying to connect to the databse but it cant (no specific error) just that it doesnt find the db.这是成功构建的nodejs应用程序Dockerfile,在日志中我看到该应用程序正在尝试连接到数据库,但它不能(没有特定错误)只是找不到数据库。

 # Image source FROM node:14-alpine # Docker working directory WORKDIR /app # Copying file into APP directory of docker COPY./package.json /app/ RUN apk update && \ apk add git # Then install the NPM module RUN yarn install # Copy current directory to APP folder COPY. /app/ EXPOSE 5000 CMD ["npm", "run", "start:dev"]

I have 2 env files in my projecs root directory.我的项目根目录中有 2 个 env 文件。

  1. .env .env
  2. docker.env docker.env

As mentioned above, when I remove the "nftapi" service from docker and run the nodejs up with a simple npm start it is connecting to the postgres container.如上所述,当我从 docker 中删除“nftapi”服务并使用简单的 npm 启动运行 nodejs 时,它正在连接到 postgres 容器。

 TypeOrmModule.forRoot({ type: 'postgres', host: process.env.POSTGRES_HOST, port: Number(process.env.POSTGRES_PORT), username: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, database: process.env.POSTGRES_DB, synchronize:true, entities: ['dist/**/*.entity{.ts,.js}'], }),

The host from the.env file that is used in the typeorm module is localhost typeorm 模块中使用的 .env 文件中的主机是 localhost

When using networks with docker-compose you should use the name of the service as you hostname.当使用具有 docker-compose 的网络时,您应该使用服务名称作为主机名。

so in your case the hostname should be postgres and not localhost所以在你的情况下,主机名应该是postgres而不是localhost

You can read more about at here: https://docs.docker.com/compose/networking/您可以在此处阅读更多信息: https://docs.docker.com/compose/networking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM