[英]Dockerized Django REST Application and PostgreSQL DB
Is there any way that I do not need to change my settings.py file every time i decide to run my application ? 有什么方法不需要每次决定运行应用程序时都更改我的settings.py文件吗? What I mean is this: 我的意思是:
-Every time I want to run dockerized application with docker-compose my settings.py file looks like this: -每次我想使用docker运行dockerized应用程序时,请编写我的settings.py文件,如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'agent_technologies_db',
'USER': 'stefan_radonjic',
'PASSWORD': 'cepajecar995',
'HOST': 'db',
'PORT': '',
}
}
-And every time i want to run my application on my local machine my settings.py looks like this: -每次我想在本地计算机上运行应用程序时,settings.py都将如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'agent_technologies_db',
'USER': 'stefan_radonjic',
'PASSWORD': 'cepajecar995',
'HOST': 'localhost',
'PORT': '',
}
}
Is there any way around this? 有没有办法解决?
Here is what my docker-compose.yml file looks like: 这是我的docker-compose.yml文件的样子:
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER=stefan_radonjic
- POSTGRES_PASSWORD=cepajecar995
- POSTGRES_DB=agent_technologies_db
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/agent-technologies
ports:
- "8000:8000"
links:
- db
depends_on:
- db
Easiest way: use two separate settings files. 最简单的方法:使用两个单独的设置文件。
When running on local machine, use settings.py
. 在本地计算机上运行时,请使用settings.py
。
When running in Docker, use settings-docker.py
(or whatever the name is), and bind mount this file into the container as settings.py
. 在Docker中运行时,请使用settings-docker.py
(或任何名称),并将此文件作为settings.py
绑定到容器中。 This way, your application inside the container will see only the settings.py
file as expected. 这样,您的容器内的应用程序将只看到预期的settings.py
文件。
Simply modify your docker-compose.yml file like this (make sure to use the correct path): 只需像这样修改您的docker-compose.yml文件(确保使用正确的路径):
volumes:
- .:/agent-technologies
- settings-docker.py:/app/settings.py
I have fixed the issue by following the solution provided on the following link: 我已通过遵循以下链接上提供的解决方案解决了该问题:
How to properly runserver on different settings for Django? 如何在Django的不同设置上正确运行服务器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.