如何使用docker compose为使用postgres的Django应用正确配置卷?
我有一个使用Postgresql的停靠Django应用程序。在我将卷添加到docker compose文件之前,所有内容都会运行find,以便为DB提供持久数据存储 docker-compose.yml:如何使用docker compose为使用postgres的Django应用正确配置卷?,django,postgresql,docker,docker-compose,Django,Postgresql,Docker,Docker Compose,我有一个使用Postgresql的停靠Django应用程序。在我将卷添加到docker compose文件之前,所有内容都会运行find,以便为DB提供持久数据存储 docker-compose.yml: version: "3" services: app: build: context: . ports: - "8000:8000" volumes: - ./app:/app command: >
version: "3"
services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=postgres
- DB_PASS=dbpassword
depends_on:
- db
db:
image: postgres:10-alpine
volumes:
- ./postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=dbpassword
ports:
- "5432:5432"
此处的错误消息:
django.db.utils.OperationalError: could not connect to server: Connection refused
app_1 | Is the server running on host "db" (172.26.0.2) and accepting
app_1 | TCP/IP connections on port 5432?
app_1 |
recipe-api_app_1 exited with code 1
db_1 | 2019-08-31 22:52:35.969 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2019-08-31 22:52:35.969 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2019-08-31 22:52:35.972 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2019-08-31 22:52:36.042 UTC [18] LOG: database system was shut down at 2019-08-31 22:41:04 UTC
db_1 | 2019-08-31 22:52:36.055 UTC [1] LOG: database system is ready to accept connections
一旦你为docker compose添加了卷,我就会一直收到这个错误,如果没有卷,一切都会正常运行
编辑:wait_for_db命令:
def handle(self, *args, **options):
"""Handle the command"""
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections['default']
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
看起来数据库正在正确启动,但应用程序在准备就绪之前需要连接;我会调试为什么你的
wait_for_db
不起作用。@DavidMaze我编辑了这个问题,添加了我的wait_for_db
命令,我不明白为什么它会在添加了卷的情况下起作用,但在添加卷的情况下却不起作用postgres@DavidMaze这是wait_for_db
脚本的问题,已修复,谢谢