You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ENOTFOUND与ECONNREFUSED:如何配置Docker内外可访问的数据源?

解决TypeORM在Docker内部和外部同时访问PostgreSQL的配置方案

针对你遇到的「Docker内部用服务名连不上本地迁移、本地用localhost连不上容器内数据库」的矛盾,以下是几种可行的兼容方案:

方案1:通过环境变量动态切换数据库Host

这是最灵活且符合开发/部署最佳实践的方案,通过环境变量区分本地运行和Docker容器内的数据库地址。

TypeORM配置修改

import type { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";

export default function generateTypeORM_Configuration(): PostgresConnectionOptions {
  return {
    type: "postgres",
    host: process.env.DATABASE_HOST || "localhost",
    port: parseInt(process.env.DATABASE_PORT || "5432"),
    username: process.env.DATABASE_USERNAME || "postgres",
    password: process.env.DATABASE_PASSWORD || "pass1234",
    entities: []
  };
}

Docker Compose配置调整

给你的应用服务添加环境变量,指定数据库Host为Docker服务名:

version: "3.5"

services:
  # 你的应用服务(示例名为app)
  app:
    # ... 镜像、端口等其他配置
    environment:
      - DATABASE_HOST=Database
      - DATABASE_PORT=5432
      - DATABASE_USERNAME=postgres
      - DATABASE_PASSWORD=${DATABASE_PASSWORD}
    depends_on:
      - Database

  Database:
    image: postgres
    container_name: Example-Local-Database
    ports:
      - "${DATABASE_PORT}:5432" # 内部端口固定为Postgres默认的5432
    environment:
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
    volumes:
      - DatabaseData:/var/lib/postgresql/data

volumes:
  DatabaseData:
    name: Example-Local-DatabaseData
    driver: local

本地运行迁移

在本地创建.env文件,指定Host为localhost:

DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=pass1234

运行迁移命令时会自动读取该环境变量,直接连接本地映射的PostgreSQL端口。

方案2:配置Docker网络+本地Hosts映射

如果不想修改代码逻辑,可以通过自定义Docker网络+本地Hosts文件映射,让本地也能解析Docker服务名。

Docker Compose添加自定义网络

version: "3.5"

networks:
  app-network:
    driver: bridge

services:
  Database:
    image: postgres
    container_name: Example-Local-Database
    ports:
      - "${DATABASE_PORT}:5432"
    environment:
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
    volumes:
      - DatabaseData:/var/lib/postgresql/data
    networks:
      - app-network

  app:
    # ... 应用配置
    networks:
      - app-network

volumes:
  DatabaseData:
    name: Example-Local-DatabaseData
    driver: local

修改本地Hosts文件

  • Linux/macOS:编辑/etc/hosts
  • Windows:编辑C:\Windows\System32\drivers\etc\hosts(需管理员权限)

添加一行映射:

127.0.0.1 Database

TypeORM固定配置

直接使用Docker服务名作为Host,本地和容器内都能正常解析:

import type { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";

export default function generateTypeORM_Configuration(): PostgresConnectionOptions {
  return {
    type: "postgres",
    host: "Database",
    port: parseInt(process.env.DATABASE_PORT || "5432"),
    username: "postgres",
    password: process.env.DATABASE_PASSWORD || "pass1234",
    entities: []
  };
}

方案3:使用Docker Host网络模式(仅开发环境)

通过让数据库容器共享主机网络,实现localhost直接访问,但该方案会破坏容器网络隔离性,不建议生产环境使用。

Docker Compose配置修改

services:
  Database:
    image: postgres
    container_name: Example-Local-Database
    network_mode: host
    environment:
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
    volumes:
      - DatabaseData:/var/lib/postgresql/data

TypeORM配置

直接使用localhost作为Host,本地和容器内应用都能连接:

import type { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";

export default function generateTypeORM_Configuration(): PostgresConnectionOptions {
  return {
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "postgres",
    password: "pass1234",
    entities: []
  };
}

内容的提问来源于stack exchange,提问作者Takeshi Tokugawa YD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:13:12