如何为Docker postgres实例指定其他端口? [英] How to specify different port for a Docker postgres instance?
问题描述
我正在尝试让每个这样的几个在线线程在不同的端口(5433而不是默认的5432)上运行一个postgres容器: 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.)
在我的测试ENV中,我有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.在 docker-compose
您只使用给定的端口将现有端口5432映射为5433到外部世界
Inside the container, so in DATABASE_URL
the port needs to stay the same 5432. 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屋!