0

I'm running a docker-compose container for postgresql. The postgresql database in the container is running on the standard port 5432, and I am publishing that port out to port 5444 on the host (since the host's postgresql default port is in use).

I am using the same configuration on the host and in the container (a .env file that provides config settings for cli commands and the app as a whole). Unfortunately, whichever port I choose, one system will lose access. For example, I cannot run:

[k@host]$ pgsql -p 5444 # Connects

in the host and still have work inside the container:

[k@db-container]$ pgsql -p 5444 # Errors in container

The container's postgresql-server is running on 5432:

[k@db-container]$ pgsql -p 5432 # Connects successfully in container

and the ports are published via the docker-compose.yml via: - ports: - "5444:5432"

So currently I don't know how to configure the same port everywhere simply via the docker-compose.yml! expose command exposes the port but does not allow remapping, ports forwards host<-->container, but does not remap the internal port. I have thought of remapping the postgresql default port inside the postgresql container configuration, but fully reconfiguring postgresql seems non-trivial to do via docker-compose on every docker-compose up.

How can I remap the ports inside the container so that I can use port 5444 everywhere, in the host & container?

2 Answers 2

2

The standard PostgreSQL client library supports several environment variables that tell it where the server is. In the same way that you can configure the host using $PGHOST, you can configure the port using $PGPORT.

In a Docker Compose context, it should be straightforward to set these:

version: '3'
services:
  postgres:
    image: postgres:11
    ports: ['5444:5432']
    volumes: ['./postgres:/var/lib/postgresql/data']
  myapp:
    build: .
    ports: ['8888:8888']
    env:
      PGHOST: postgres
      # default PGPORT=5432 will work fine

Similarly, if you're running the application in a development environment on the host, you can set

PGHOST=localhost PGPORT=5444 ./myapp
Sign up to request clarification or add additional context in comments.

1 Comment

That works, I can modify the commands on my local development (by passing ENV vars), I'll do that, thanks.
0

You can't use the same port on the host.

Nothing prevents you to run multiple instances if they has different IP-addresses.

psql -p 5444

Defaults to psql --host=127.0.0.1 -p 5444. If you want several instances - obviously you must make them different in some way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.