我对Docker还很陌生,正在尝试使用docker-compose和docker-machine将我的Django REST API与Nginx、Gunicorn和Postgres放入容器中。我遵循了这个教程:https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/
我的大部分代码与教程相同(https://github.com/realpython/dockerizing-django),只有一些名称变化。
以下是我的docker-compose.yml
文件(为了调试目的,我将gunicorn命令改为runserver)
web:
restart: always
build: ./web
expose:
- "8000"
links:
- postgres:postgres
- redis:redis
volumes:
- /usr/src/app
- /usr/src/app/static
env_file: .env
environment:
DEBUG: 'true'
command: /usr/local/bin/python manage.py runserver
nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- web
links:
- web:web
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data/
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
volumes:
- redisdata:/data
这是我的Django的settings.py文件中的内容:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'postgres',
'PORT': '5432',
}
}
Nginx和Postgres(以及Redis)已经启动并运行,但我的Django服务器无法启动,并显示以下错误:web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 | could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 5432?
我已经谷歌了很多并且验证了postgres正在运行,端口为5432,我可以使用psql命令连接到它。
我很迷茫,我的错误在哪里?
编辑:看起来它没有使用我的settings.py文件或其他东西,因为它正在询问服务器是否在本地主机上运行,而设置应该正在寻找postgres。