postgresql 在 Docker Compose 中更改 postgres 容器服务器端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37775702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:15:15  来源:igfitidea点击:

Changing a postgres containers server port in Docker Compose

postgresqldockerdocker-compose

提问by GreenGodot

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.

我正在尝试使用 Docker compose 在远程服务器上部署第二个数据库容器。这个 postgresql 服务器在端口 5433 上运行,而不是第一个 postgresql 容器使用的 5432。

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:

我的 docker compose 文件是:

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?

我觉得问题一定是服务器实例上的 postgresql.conf 文件将端口设置为 5432 导致我的应用程序尝试连接到它时出现错误。是否有一种简单的方法可以使用撰写文件中的命令更改端口,而不是使用卷来替换文件?

I am using the official postgresql container for this job.

我正在使用官方的 postgresql 容器来完成这项工作。

回答by Robert Moskal

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

我假设 postgres 正在容器中的端口 5432 上运行,并且您想在 5433 上的主机上公开它。

Use this in the ports strophe:

在端口 strophe 中使用它:

ports:
    -"5433:5432"

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

这将在主机上的端口 5433 上公开服务器。在这种情况下,您可以摆脱现有的公开 strophe。

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.

如果您只想将服务公开给撰写文件(而不是本地主机)中声明的其他服务,只需使用公开 strophe 并将其指向已经在内部公开的端口 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.

请记住, EXPOSE 指令实际上并没有做任何事情(它更像是对您的提示)。端口 5432 将向组合文件中声明的其他服务公开,无论是否使用该指令。

回答by user3751385

Some people may wish to actually change the port Postgres is running on, rather than remapping the exposed port to the host using the port directive. To do so, use command: -p 5433

有些人可能希望实际更改 Postgres 运行的端口,而不是使用 port 指令将暴露的端口重新映射到主机。为此,请使用command: -p 5433

In the example used for the question:

在用于问题的示例中:

db:
  image: postgres:latest
  environment:
    POSTGRES_PASSWORD: route_admin
    POSTGRES_USER: route_admin
  expose:
    - "5433" # Publishes 5433 to other containers but NOT to host machine
  ports:
    - "5433:5433"
  volumes:
    - ./backups:/home/backups
  command: -p 5433

Note that only the host will respect the port directive. Other containers will not.

请注意,只有主机会遵守端口指令。其他容器不会。