Rob*_*sma 9 python django docker docker-compose
我是 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
Run Code Online (Sandbox Code Playgroud)
这是在我的 Django settings.py 中:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'postgres',
'PORT': '5432',
}
}
Run Code Online (Sandbox Code Playgroud)
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?
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索了很多,我已经验证了 postgres 正在运行,在端口 5432 上,我可以使用 psql 命令连接到它。
我搞不清楚了。我的错误是什么?
编辑:它似乎没有使用我的 settings.py 文件或其他东西,因为它询问服务器是否在本地主机上运行,而设置应该寻找 postgres。
Jas*_*nov 16
当遇到此问题的人请检查您的settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'db'
}
}
Run Code Online (Sandbox Code Playgroud)
您的数据库名称HOST:'db'和docker-compose文件数据库名称应该相同。如果您想从数据库重命名,请确保更改docker-compose文件并setting.py:
db:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data/
Run Code Online (Sandbox Code Playgroud)
检查你的manage.py,应该有一行
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
Run Code Online (Sandbox Code Playgroud)
如果没有这样的行,请将其放入
设置你的DJANGO_SETTINGS_MODULE相对于PYTHONPATH.
UPD我克隆了你的存储库并通过更改 docker-compose.yml 中的命令启动了 Web 服务
- command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000
+ command: python manage.py runserver 0.0.0.0:8000
Run Code Online (Sandbox Code Playgroud)
我确信DJANGO_SETTINGS_MODULE是正确的。
| 归档时间: |
|
| 查看次数: |
8644 次 |
| 最近记录: |