我是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。
感謝您的回覆,但線路在那裏!一切工作在當地。 –
@您的更新,它是正確的。我提到的回購爲我提供了:)。我已經設法使它工作,但我不知道如何。 –