[英]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.