Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres in Docker; two instances clashing ports

I've created a docker container which hosts a postgres server. I'm trying to get two instances of this running which index two completely different databases, and thus rely on a different set of volumes.

I'm running the following two commands one after the other:

docker run -v ... -p 5432:9001 -P --name psql-data postgres-docker
docker run -v ... -p 5432:9002 -P --name psql-transactions postgres-docker

The first container is created and runs, but the second call throws the following error:

Error response from daemon: failed to create endpoint psql-transactions on network bridge: Bind for 0.0.0.0:5432 failed.  Port already in use.

I'm finding this a little confusing, because I though the point of containers was to isolate port binding. I could understand if I'd had both containers map 5432 onto the same port on the host machine, but I'm trying to mount them to 9001 and 9002 respectively.

How do I prevent this issue?

like image 949
Andy Avatar asked Oct 26 '25 10:10

Andy


1 Answers

The order of the ports should be reversed. It should be -p host_port:container_port

like image 161
Xiongbing Jin Avatar answered Oct 28 '25 02:10

Xiongbing Jin