在 Docker Compose 中更改 postgres 容器服务器端口 [英] Changing a postgres containers server port in Docker Compose

查看:61
本文介绍了在 Docker Compose 中更改 postgres 容器服务器端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的 docker compose 文件是:

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

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

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 容器来完成这项工作.

I am using the official postgresql container for this job.

推荐答案

我假设 postgres 在容器的 5432 端口上运行,而您想在 5433 上的主机上公开它.

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

这个端口:

ports:
    - "5433:5432"

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

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

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

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.

请记住,EXPOSE 指令实际上并没有做任何事情(它更像是对您的提示).端口 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.

这篇关于在 Docker Compose 中更改 postgres 容器服务器端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆