Docker Compose + Postgres:公开端口 [英] Docker Compose + Postgres: Expose port
问题描述
我目前正在尝试将Docker用于新的Django / Postgres项目。我在Mac上工作,通常使用Postico快速连接到我的数据库。
I am currently trying to use Docker for my new Django/Postgres project. I am working on a Mac and usually use Postico to quickly connect to my database.
我以前是这样连接的:
我使用了Docker官方文档来设置docker-compose。我现在遇到的问题是我无法通过Postico连接到postgres数据库。在我看来,问题出在端口未暴露。
I used the official Docker documentation to setup docker-compose. I now have the issue, that I can't connect via Postico to the postgres db. It seems to me that the problem comes from the ports not being exposed.
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
推荐答案
主机端口,将其添加到Compose文件中的 db
服务中:
Just map the port to the host machine, add this to the db
service in your Compose file:
ports:
- "5432:5432"
也请确保在撰写文件中像这样设置postgres密码变量
Also make sure to set the postgres password variable in the compose file like this
environment:
POSTGRES_PASSWORD: example
默认用户是postgres,您可以使用 POSTGRES_USER
变量进行更改。
The default user is postgres, you can change it with the POSTGRES_USER
variable.
您可以在此处通过所有选项阅读有关图像的用法: https://hub.docker.com/_/postgres/
You can read about the usage of the image with all options here: https://hub.docker.com/_/postgres/
这篇关于Docker Compose + Postgres:公开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!