繁体   English   中英

带有 docker 和 postgress 的 nodejs:错误:getaddrinfo ENOTFOUND

[英]nodejs with docker and postgress: Error: getaddrinfo ENOTFOUND

您好,我不知道还有什么办法可以解决此错误:

    ci-api    | Error: getaddrinfo ENOTFOUND ${DB_HOST}
    ci-api    |     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
    ci-api    |   errno: 'ENOTFOUND',
    ci-api    |   code: 'ENOTFOUND',
    ci-api    |   syscall: 'getaddrinfo',
    ci-api    |   hostname: '${DB_HOST}'
    ci-api    | }

我有一个包含以下变量的.env文件:

.env文件:

    SERVER_PORT=4000
    DB_HOST=db-pg
    DB_PORT=5432
    DB_USER=spirit
    DB_PASS=api
    DB_NAME=emasa_ci

DockerFile

    #building code
    FROM node:lts-alpine

    ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
    RUN chmod +x /wait

    RUN mkdir -p /home/node/api && chown -R node:node /home/node/api

    WORKDIR /home/node/api

    COPY ormconfig.json .env package.json yarn.* ./

    USER node

    RUN yarn

    COPY --chown=node:node . .
    EXPOSE 4000

**My `docker-compose`:**

    version: '3.7'
    services:
      db-pg:
        image: postgres:12
        container_name: db-pg
        ports:
          - '${DB_PORT}:5432'
        environment:
          ALLOW_EMPTY_PASSWORD: 'no'
          POSTGRES_USER: ${DB_USER}
          POSTGRES_PASSWORD: ${DB_PASS}
          POSTGRES_DB: ${DB_NAME}
        volumes:
          - ci-postgres-data:/data

      ci-api:
        build: .
        container_name: ci-api
        volumes:
          - .:/home/node/api
        ports:
          - '${SERVER_PORT}:${SERVER_PORT}'
        depends_on:
          - db-pg
        command: sh -c "/wait && yarn dev"
        environment:
          WAIT_HOSTS: db-pg:5432
        logging:
          driver: 'json-file'
          options:
            max-size: '10m'
            max-file: '5'

    volumes:
      ci-postgres-data:

而且,这是我的ORM CONFIG

    {
      "type": "postgres",
      "host": "${DB_HOST}",
      "port": "${DB_PORT}",
      "username": "${DB_USER}",
      "password": "${DB_PASS}",
      "database": "${DB_NAME}",
      "synchronize": true,
      "logging": false,
      "entities": ["dist/src/entity/**/*.js"],
      "migrations": ["dist/src/migration/**/*.js"],
      "subscribers": ["dist/src/subscriber/**/*.js"],
      "cli": {
        "entitiesDir": "dist/src/entity",
        "migrationsDir": "dist/src/migration",
        "subscribersDir": "dist/src/subscriber"
      }
    }

我无法想象这个错误的原因,我基本上添加了一个脚本让节点在我的 Postgres 启动后启动,其中db-pg是我的 Postgres 图像名称。

我无法想象出了什么问题,有人可以帮我解决这个问题吗?

PS:我使用typescript/typeorm

{
  "type": "postgres",
  "host": "db-pg",
  "port": 5432,
  "username": "spirit",
  "password": "api",
  "database": "emasa_ci",
  "synchronize": true,
  "logging": false,
  "entities": ["dist/src/entity/**/*.js"],
  "migrations": ["dist/src/migration/**/*.js"],
  "subscribers": ["dist/src/subscriber/**/*.js"],
  "cli": {
    "entitiesDir": "dist/src/entity",
    "migrationsDir": "dist/src/migration",
    "subscribersDir": "dist/src/subscriber"
  }
}

问题出在我的 ormconfig.json (正在使用.env)

暂无
暂无

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

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