我正在为我的Django项目设置位桶管道,以便能够运行一些自动测试。我正在使用默认的python Docker映像和postgres docker映像作为服务,但是Django无法连接到PostgreSQL服务器。 无论我尝试什么,我总是会收到错误消息:
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
我尝试在Django设置文件中设置带有或不带有端口5432的localhost / posgres映像名称。 还以为图像可能没有暴露端口。
我的bitbucket管道文件
# This is a sample build configuration for Python.
# Check our guides at https://confluence.atlassian.com/x/x4UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: python:3.7.3
definitions:
services:
postgres:
image: postgres
environment:
POSTGRES_DB : 'my_db'
POSTGRES_USER : 'my_user'
POSTGRES_PASSWORD: 'my_pwd'
expose:
- "5432"
ports:
- "5432:5432"
pipelines:
default:
- step:
caches:
- pip
script: # Modify the commands below to build your repository.
- pip install -r requirements.txt
# Run tests
- python manage.py test --keepdb
services:
- postgres
我的Django数据库设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_pwd',
'HOST': 'localhost',
'PORT': '',
},
'TEST': {
'NAME': 'my_db',
'CHARSET': 'UTF8',
'HOST': 'postgres',
'PORT': '5432'
},
}
我希望我的管道能够连接到运行posgresql的postgres映像。