docker-compose:nodejs容器不与postgres容器通信 [英] docker-compose: nodejs container not communicating with postgres container

查看:37
本文介绍了docker-compose:nodejs容器不与postgres容器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实找到了一些设置略有不同但问题相同的人.所以我希望这不会是一个重复的问题.我的设置非常简单直接.我有一个用于我的节点应用程序的容器和一个用于我的 Postgres 数据库的容器.当我运行 docker-compose up 并看到日志时,两个容器都已启动并正在运行.问题是我的节点应用程序没有连接到数据库.我可以使用 Postbird 连接到数据库,它可以正常工作.

I did find a few people with a slightly different setup but with the same issue. So I hope this doesn't feel like a duplicated question. My setup is pretty simple and straight-forward. I have a container for my node app and a container for my Postgres database. When I run docker-compose up and I see the log both containers are up and running. The problem is my node app is not connecting to the database. I can connect to the database using Postbird and it works as it should.

如果我只为数据库创建一个 docker 容器并直接在我的机器上运行节点应用程序,那么一切正常.所以这不是数据库或应用程序的问题,而是设置的问题.

If I create a docker container only for the database and run the node app directly on my machine everything works fine. So it's not and issue with the DB or the app but with the setup.

这里有一些有用的信息:

Here's a few useful information:

为数据库运行 docker(连接并完美运行):

Running a docker just for the DB (connects and works perfectly):

> vigna-backend@1.0.0 dev /Users/lucasbittar/Dropbox/Code/vigna/backend
> nodemon src/server.js

[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node -r sucrase/register src/server.js`
Initializing database...
Connecting to DB -> vignadb | PORT: 5432
Executing (default): SELECT 1+1 AS result
Connection has been established successfully -> vignadb

使用 docker-compose 为每个容器运行一个容器:

Running a container for each using docker-compose:

Creating network "backend_default" with the default driver
Creating backend_db_1 ... done
Creating backend_app_1 ... done
Attaching to backend_db_1, backend_app_1
db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2020-07-24 13:23:32.875 UTC [1] LOG:  starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-07-24 13:23:32.876 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-07-24 13:23:32.876 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-07-24 13:23:32.881 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-07-24 13:23:32.955 UTC [27] LOG:  database system was shut down at 2020-07-23 13:21:09 UTC
db_1   | 2020-07-24 13:23:32.999 UTC [1] LOG:  database system is ready to accept connections
app_1  |
app_1  | > vigna-backend@1.0.0 dev /usr/app
app_1  | > npx sequelize db:migrate && npx sequelize db:seed:all && nodemon src/server.js
app_1  |
app_1  |
app_1  | Sequelize CLI [Node: 14.5.0, CLI: 5.5.1, ORM: 5.21.3]
app_1  |
app_1  | Loaded configuration file "src/config/database.js".
app_1  |
app_1  | Sequelize CLI [Node: 14.5.0, CLI: 5.5.1, ORM: 5.21.3]
app_1  |
app_1  | Loaded configuration file "src/config/database.js".
app_1  | [nodemon] 2.0.2
app_1  | [nodemon] to restart at any time, enter `rs`
app_1  | [nodemon] watching dir(s): *.*
app_1  | [nodemon] watching extensions: js,mjs,json
app_1  | [nodemon] starting `node -r sucrase/register src/server.js`
app_1  | Initializing database...
app_1  | Connecting to DB -> vignadb | PORT: 5432

我的数据库类:

class Database {
  constructor() {
    console.log('Initializing database...');
    this.init();
  }

  async init() {
    let retries = 5;
    while (retries) {
      console.log(`Connecting to DB -> ${databaseConfig.database} | PORT: ${databaseConfig.port}`);
      const sequelize = new Sequelize(databaseConfig);
      try {
        await sequelize.authenticate();
        console.log(`Connection has been established successfully -> ${databaseConfig.database}`);
        models
          .map(model => model.init(sequelize))
          .map( model => model.associate && model.associate(sequelize.models));
        break;
      } catch (err) {
        console.log(`Error: ${err.message}`);
        retries -= 1;
        console.log(`Retries left: ${retries}`);
        // Wait 5 seconds before trying again
        await new Promise(res => setTimeout(res, 5000));
      }
    }
  }
}

Dockerfile:

Dockerfile:

FROM node:alpine

WORKDIR /usr/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3333

CMD ["npm", "start"]

docker-compose.yml:

docker-compose.yml:

version: "3"

services: 
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: vignadb
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  app:
    build: .
    depends_on:
      - db
    ports:
      - "3333:3333"
    volumes:
      - .:/usr/app
    command: npm run dev

package.json(仅脚本):

package.json (scrips only):

"scripts": {
  "dev-old": "nodemon src/server.js",
  "dev": "npx sequelize db:migrate && npx sequelize db:seed:all && nodemon src/server.js",
  "build": "sucrase ./src -d ./dist --transforms imports",
  "start": "node dist/server.js"
  },

.env:

# Database
DB_HOST=db
DB_USER=postgres
DB_PASS=postgres
DB_NAME=vignadb
DB_PORT=5432

数据库配置:

require('dotenv/config');

module.exports = {
  dialect: 'postgres',
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  port: process.env.DB_PORT,
  define: {
    timestamp: true,
    underscored: true,
    underscoredAll: true,
  },
};

我知道我搞砸了我只是不知道在哪里.如果我能提供更多信息,请告诉我.

I know I'm messing up something I just don't know where. Let me know if I can provide more information.

谢谢!

推荐答案

你应该把你的 2 个容器放在同一个网络中 https://docs.docker.com/compose/networking/

You should put your 2 containers in the same network https://docs.docker.com/compose/networking/

并在你的 nodejs 连接字符串中调用你的数据库服务.

And call your db service inside your nodejs connexion string.

类似:postgres://db:5432/vignadb

这篇关于docker-compose:nodejs容器不与postgres容器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆