如何为 Docker postgres 实例指定不同的端口? [英] How to specify different port for a Docker postgres instance?
问题描述
我试图让一个 postgres 容器运行在不同的端口(5433 而不是默认的 5432)上,每个在线线程都像这样:https://github.com/docker-library/postgres/issues/196#issuecomment-310209118
I am trying to get a postgres container running on a different port (5433 instead of the default 5432) per several online threads like this one: https://github.com/docker-library/postgres/issues/196#issuecomment-310209118
(端口更改的原因是因为我离开运行的一个无关项目已经在使用端口 5432,所以我希望能够在多个项目上同时运行 rspec.)
(The reason for the port change is because an unrelated project that I leave running is already using port 5432 so I'd like to be able to run rspec simultaneously on multiple projects.)
在我的测试环境中,我有DATABASE_URL=postgresql://postgres:@db.local:5433/test_agile_self
In my test ENV I have DATABASE_URL=postgresql://postgres:@db.local:5433/test_agile_self
当我使用端口 5432 时一切正常(在 DATABASE_URL 和 docker-compose.yml 中)
Everything worked when I was using port 5432 (in DATABASE_URL and docker-compose.yml)
将端口更改为 5433 后,当我运行 rspec 时,我得到:
After changing the port to 5433, when I run rspec I get:
PG::ConnectionBad:
could not connect to server: Connection refused
Is the server running on host "db.local" (172.22.0.2) and accepting
TCP/IP connections on port 5433?
# ./spec/spec_helper.rb:62:in `block (2 levels) in <top (required)>'
容器似乎确实在端口 5433 和 IP 地址 172.22.0.2 上运行:
The container does indeed seem to be running on port 5433 and IP address 172.22.0.2:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8f5dee227e8 postgres:10.5 "docker-entrypoint.s…" 27 minutes ago Up 14 seconds 0.0.0.0:5433->5432/tcp zarc_db.local_1
$ docker inspect a8f5dee227e8 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.22.0.2",
我的 docker-compose.yml 使用 ports: "5433:5432"
根据上面链接的讨论.
My docker-compose.yml uses the ports: "5433:5432"
as per the discussion linked above.
#docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "3010:3010"
volumes:
- .:/my_app
links:
- db.local
depends_on:
- db.local
db.local:
image: postgres:10.5
ports:
- "5433:5432"
如果我在这两个位置(ENV 和 docker-compose.yml)将 5433 改回 5432,它会再次工作.
If I change 5433 back to 5432 in those two spots (ENV and docker-compose.yml) it works again.
这是在运行 Mohave 10.14.3 和 Docker 18.09.2 的 Mac 上
This is on a Mac running Mohave 10.14.3, and Docker 18.09.2
推荐答案
在容器内部,所以在 DATABASE_URL
中的端口需要保持相同的 5432,即它必须匹配内部的 DB 配置容器.在 docker-compose
中,您只使用给定的
Inside the container, so in DATABASE_URL
the port needs to stay the same 5432, i.e. it has to match the DB config inside the container. In the docker-compose
you only map the existing port 5432 to the outside world as 5433 using the given
ports:
- "5433:5432"
这篇关于如何为 Docker postgres 实例指定不同的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!