Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

postgresql - Changing a postgres containers server port in Docker Compose

I am trying to deploy a second database container on a remote server using Docker compose. This postgresql server runs on port 5433 as opposed to 5432 as used by the first postgresql container.

When I set up the application I get this error output:

web_1  | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1  |    Is the server running on host "db" (172.17.0.2) and accepting
web_1  |    TCP/IP connections on port 5433?

and my docker compose file is:

db:
  image: postgres:latest
  environment:
    POSTGRES_PASSWORD: route_admin
    POSTGRES_USER: route_admin
  expose:
    - "5433"
  ports:
    - "5433"
  volumes:
    - ./backups:/home/backups
   
web:
  build: .
  command:  bash -c "sleep 5 && python -u application/manage.py runserver 0.0.0.0:8081"
  volumes:
    - .:/code
  ports:
    - "81:8081"
  links:
    - db
  environment:
    - PYTHONUNBUFFERED=0

I feel the issue must be the postgresql.conf file on the server instance having set the port to 5432 causing the error when my app tries to connect to it. Is there a simple way of changing the port using a command in the compose file as opposed to messing around with volumes to replace the file?

I am using the official postgresql container for this job.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm assuming postgres is running on port 5432 in the container and you want to expose it on the host on 5433.

This ports strophe:

ports:
    - "5433:5432"

will expose the server on port 5433 on the host. You can get rid of your existing expose strophe in this scenario.

If you only want to expose the service to other services declared in the compose file (and NOT localhost), just use the expose strophe and point it to the already internally exposed port 5432.

Bear in mind, the EXPOSE directive doesn't actually do anything (it's more of a hint for you). Port 5432 will be exposed to the other services declared in the compose file with or without the directive.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...