我是Docker的新手,我试图使用docker-compose和docker-machine将我的Django rest API放入包含Nginx,Gunicorn和Postgres的容器中。以下教程:https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/Docker Django无法连接到服务器:连接被拒绝
我的大部分代码与教程(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。
感谢您的回复,但线路在那里!一切工作在当地。 –
@您的更新,它是正确的。我提到的回购为我提供了:)。我已经设法使它工作,但我不知道如何。 –