【发布时间】:2022-10-12 22:08:12
【问题描述】:
我有一个docker-compose,它有一个django 和一个nginx 作为反向代理
码头工人-compose.yml:
version: "3"
services:
app:
restart: always
command: ./startup.sh
image: region-docker.pkg.dev/project_id/repo/image:tag
container_name: backend
expose:
- "8000"
volumes:
- static_volume:/code/static
hostname: app
nginx:
restart: always
image: region-docker.pkg.dev/project_id/repo/image:tag
volumes:
- static_volume:/code/static
ports:
- "80:80"
depends_on:
- app
volumes:
static_volume:
settings.py中的数据库连接变量配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'Instance': 'project_id:region:instance_name',
'NAME': 'database_name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'database_internal_IP',
'PORT': '5432',
}
}
每当我在虚拟机 CLI 中运行 docker-compose up 时,nginx 运行完美,但 django 服务器出现此错误
django.db.utils.OperationalError: could not connect to server: Connection timed out
backend | Is the server running on host "instance-private-IP" and accepting
backend | TCP/IP connections on port 5432?
笔记:当我运行psql -h instance-private-IP -U username 时,连接已成功建立
笔记:当我在本地 pc 上运行完全相同的容器时,使用相同的配置,只有公共 IP 而不是私有 IP,容器运行得很好
笔记:附加到 VM 的服务帐户可以访问已启用的云 SQL
【问题讨论】:
标签: django postgresql docker google-cloud-platform docker-compose