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
(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.)
In my test ENV I have DATABASE_URL=postgresql://postgres:@db.local:5433/test_agile_self
Everything worked when I was using port 5432 (in DATABASE_URL and docker-compose.yml)
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)>'
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",
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"
If I change 5433 back to 5432 in those two spots (ENV and docker-compose.yml) it works again.
This is on a Mac running Mohave 10.14.3, and Docker 18.09.2
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"
In your docker-compose
you can add this: (Long-Syntax)
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
Where,
- target: the port inside the container
- published: the publicly exposed port
- protocol: the port protocol (tcp or udp)
- mode: host for publishing a host port on each node, or ingress for a swarm mode port to be load balanced.
Alternatively a one-liner: (Short-Syntax)
ports:
- "4040:5432" # HOST:CONTAINER
Where,
- 4040 is port to be exposed on the HOST
- 5432 is port exposed on container
NOTE:
You may experience erroneous results when using a container port lower than 60, because YAML parses numbers in the format xx:yy as a base-60 value. For this reason, we recommend always explicitly specifying your port mappings as strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With