简体   繁体   English

PostgreSQL 数据库未暴露给我期望在 docker-compose 中的端口

[英]PostgreSQL database is not exposed to the port I expect in docker-compose

I am trying to set up multiservices architecture in my Node.js backend with docker.我正在尝试使用 docker 在我的 Node.js 后端设置多服务架构。 I have currently two services with two separate databases我目前有两个服务和两个独立的数据库

version: "3"
services:
  server-api-getaway:
    build:
      context: "."
      dockerfile: "./server-api-gateway/Dockerfile"
    depends_on:
      - db
      - redis
    ports:
      - "7100:7100"
    volumes:
      - ./server-api-gateway:/usr/src/app/server-api-gateway
    environment:
      - CHOKIDAR_USEPOLLING=true
      - REDIS_URL=redis://cache
      - DATABASE_URL=postgres://postgres:postgres@db:3336/dbname
      - DATABASE_PORT=3336
      - DATABASE_HOST=host.docker.internal
      - POSTGRES_DB=dbname
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PORT=7100

  db:
    image: postgres
    restart: always
    environment:
      - POSTGRES_DB=dbname
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "3336:3306"

  server-user-service:
    build:
      context: "."
      dockerfile: "./server-user-service/Dockerfile"
    depends_on:
      - user-service-db
    ports:
      - "7000:7000"
    volumes:
      - ./server-user-service:/usr/src/app/server-user-service
    environment:
      - CHOKIDAR_USEPOLLING=true
      - REDIS_URL=redis://cache
      - DATABASE_URL=postgres://postgres:postgres@user-service-db:3307/dbname
      - DATABASE_PORT=3307
      - DATABASE_HOST=host.docker.internal
      - POSTGRES_DB=dbname
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PORT=7000

  user-service-db:
    image: postgres
    restart: always
    environment:
      - POSTGRES_DB=dbname
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "3307:3306"

  redis:
    image: redis
    container_name: cache
    restart: always
    ports:
      - 6379:6379

volumes:
  pgdata:
    driver: local
  pgconf:
    driver: local
  pglog:
    driver: local

I expect both databases to run on different ports, but when I run docker-compose up they are exposed to 5432 port我希望两个数据库都在不同的端口上运行,但是当我运行docker-compose 时,它们会暴露于5432端口

2021-03-05 07:12:10.111 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 2021-03-05 07:12:10.111 UTC [1] 日志:侦听 IPv4 地址“0.0.0.0”,端口 5432

When I connect my service to port 5432 everything works fine.当我将服务连接到端口5432时,一切正常。 But I need two databases to be exposed to different ports.但是我需要将两个数据库暴露给不同的端口。 How can I achieve it?我怎样才能实现它?

Update更新

I've changed port mapping as was suggested in comments.我已经按照评论中的建议更改了端口映射。

  user-service-db:
    image: postgres
    restart: always
    environment:
      - POSTGRES_DB=dbname
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:3336"

And made a fresh start after docker-compose down并在docker-compose down后重新开始

Result remained the same结果保持不变

user-service-db_1 |用户服务-db_1 | 2021-03-05 10:01:23.023 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 2021-03-05 10:01:23.023 UTC [1] 日志:侦听 IPv4 地址“0.0.0.0”,端口 5432

probably there is a detail I am missing可能有一个细节我错过了

As an implementation detail, containers have their own private IP addresses.作为实现细节,容器有自己的私有 IP 地址。 You can't usually access these addresses from outside Docker, but when your container tries to connect to the host name db , that maps to one of these internal addresses.您通常无法从 Docker 外部访问这些地址,但是当您的容器尝试连接到主机名db时,它会映射到这些内部地址之一。 This means that it's okay for processes in different containers to listen on the same port, since each process is in its own isolated network space.这意味着不同容器中的进程可以监听同一个端口,因为每个进程都在自己的隔离网络空间中。

That means it's standard for a packaged process to listen on its "normal" port.这意味着打包进程在其“正常”端口上侦听是标准的。 The output you're seeing is the output of the PostgreSQL startup;你看到的output是PostgreSQL启动的output; this will always listen on 0.0.0.0:5432 (unless you make tricky modifications to its configuration file).这将始终在 0.0.0.0:5432 上侦听(除非您对其配置文件进行了棘手的修改)。

Connections between containers also always connect to the "normal" port:容器之间的连接也总是连接到“正常”端口:

 # Use ordinary port, ports: are ignored       vvvv
- DATABASE_URL=postgres://postgres:postgres@db:5432/dbname

Publishing a port makes it accessible from outside Docker, using the host's IP address (and DNS name).使用主机的 IP 地址(和 DNS 名称)发布端口使其可以从 Docker 外部访问。 The second ports: number needs to match the port number inside the container, the first one can be anything that's not already in use.第二个ports:编号需要与容器内的端口号匹配,第一个可以是任何尚未使用的端口。 Connections between containers never use this.容器之间的连接从不使用它。

ports:
  #       vvvv the ordinary port, fixed by the image
  - "3336:5432"

"Expose" as a verb means almost nothing in modern Docker. “暴露”作为动词在现代 Docker 中几乎没有任何意义。 EXPOSE in a Dockerfile is mostly documentation and there's no reason to use the Compose expose: setting. EXPOSE中的 EXPOSE 主要是文档,没有理由使用 Compose expose:设置。

You need to expose the correct port.您需要公开正确的端口。
Postgres default port is 5432 : ie the postgres service in your docker container will listen to port 5432 . Postgres 默认端口是5432 :即 docker 容器中的 postgres 服务将侦听端口5432
I guess, on the host, you want to listen 3336 (or 3306 ?).我猜,在主机上,你想听3336 (或3306 ?)。

in this case the mapping must be:在这种情况下,映射必须是:

ports:
  - "3336:5432"

ie the format is HOST:CONTAINER即格式为HOST:CONTAINER

see also:也可以看看:

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

相关问题 docker-compose up && docker-compose build:PostgreSQL错误 - docker-compose up && docker-compose build : error with PostgreSQL docker-compose使用的端口如何稳定? - How to stabilize the port used by docker-compose? NodeJS API 不与 Docker-compose 中的 PostgreSQL 通信 - NodeJS API not communicating with PostgreSQL in Docker-compose 如何为Redis / RethinkDB指定备用暴露端口(使用Docker Compose)? - How can I specify an Alternate Exposed Port for Redis / RethinkDB (using Docker Compose)? 连接Postgresql数据库远程服务器ubuntu服务器(Docker-Compose)和Node.js express后端 - Connect Postgresql database remote server ubuntu server (Docker-Compose) and Node.js express backend 当我尝试使用 docker-compose 为 nodejs 和 mysql 应用程序运行 docker 容器时出现端口使用错误 - I get port in use error when I try to run the docker container for the nodejs and mysql application using the docker-compose Docker组成的网络,从容器访问主机端口 - Docker-compose networking, access host port from container docker-compose 中的 ECONNREFUSED 错误,在谷歌云中使用 NodeJS 和 postgresql - ECONNREFUSED error in docker-compose with NodeJS and postgresql in google cloud 带有Docker-compose的NodeJS - NodeJS with Docker-compose Vite 上的 Docker-compose - Docker-compose on Vite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM