Docker仅在服务器上编写网络问题

vlju58qv  于 2023-10-16  发布在  Docker
关注(0)|答案(1)|浏览(99)

我正在尝试配置一个docker compose来连接一个nodejs应用程序(使用nest)和另一个postgres容器。从本地它工作正常,但当尝试在服务器上它不工作,它像postgres容器不接受连接。

services:
  fe:
    image: "feimage-dockerhub"
    ports:
      - 4800:80
  api:
    image: "apiimage-dockerhub"
    depends_on:
      - db
    environment:
      - POSTGRES_DB=dbname
      - POSTGRES_USER=dbuser
      - POSTGRES_PASSWORD=dbpass
      - POSTGRES_HOST=db
      - POSTGRES_PORT=5432
      - HOST_URL=http://localhost:3000
      - BCRYPT_SALT=10
    restart: always
  db:
    container_name: db
    image: "postgres"
    environment:
      POSTGRES_DB: dbname
      POSTGRES_USER: dbuser
      POSTGRES_PASSWORD: dbpass
    restart: unless-stopped
    volumes:
      - db:/var/lib/postgresql/data

volumes:
  db:
    driver: local

我试过运行两个容器,并把它们放在同一个网络中,但连接也会中断,ping到postgres服务时,它会正确回复。
也许这将是有用的,知道服务器已安装portainer,和其他堆栈具有相当类似的配置工程没有问题。
也许我漏掉了什么

Client: Docker Engine - Community
 Version:           23.0.2
 API version:       1.42
 Go version:        go1.19.7
 Git commit:        569dd73
 Built:             Mon Mar 27 16:16:30 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.2
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.7
  Git commit:       219f21b
  Built:            Mon Mar 27 16:16:30 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.20
  GitCommit:        2806fc1057397dbaeefbea0e4e17bddfbd388f38
 runc:
  Version:          1.1.5
  GitCommit:        v1.1.5-0-gf19387a
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

谢谢
我尝试了一个简单的配置来检查容器之间的通信,使用psql从一个postgres主机连接到另一个,它只在本地工作,在服务器上同样的问题

version: "3"

services:
  db:
    image: "postgres"
    environment:
      POSTGRES_DB: vault
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
  db2:
    image: "postgres"
    environment:
      POSTGRES_DB: vault
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root

编辑
我已经打开了服务器端口并暴露了postgres端口,它工作了,但我不想暴露数据库,这似乎只是docker网络的问题

scyqe7ek

scyqe7ek1#

这是配置数据库连接的部分

import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { DataSourceOptions, EntitySchema } from 'typeorm';

export function typeOrmConfig(
  entities: EntitySchema[] = [],
  migrations: string[] = [],
): TypeOrmModuleOptions & Partial<DataSourceOptions> {
  console.log(process.env)
  const res: TypeOrmModuleOptions = {
    type: 'postgres',
    host: process.env.POSTGRES_HOST,
    synchronize: true,
    username: process.env.POSTGRES_USER,
    password: process.env.POSTGRES_PASSWORD,
    database: process.env.POSTGRES_DB,
    dropSchema:
      process.env.DROP_SCHEMA === undefined
        ? false
        : Boolean(process.env.DROP_SCHEMA),
    migrationsTableName: '__migrations__',
    migrations,
    entities,
  };

  if (process.env.POSTGRES_PORT) {
    return {
      ...res,
      port: process.env.POSTGRES_PORT
        ? Number(process.env.POSTGRES_PORT)
        : 5432,
    };
  }
  return res;
}

这就是从process.env打印的内容

{
  JWT_EXPIRATION_TIME: '100000000000000',
  NODE_VERSION: '18.18.0',
  HOSTNAME: '30bd4fc3d1af',
  ENV_FILE: 'prod',
  YARN_VERSION: '1.22.19',
  HOME: '/root',
  HOST_URL: 'http://localhost:3000',
  BCRYPT_SALT: '10',
  POSTGRES_PASSWORD: 'dbpass',
  PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  POSTGRES_HOST: 'db',
  POSTGRES_USER: 'dbuser',
  PWD: '/usr/app',
  POSTGRES_DB: 'dbname',
  POSTGRES_PORT: 5432
}

无论如何,连接不工作使用psql -h.也会超时

相关问题