简体   繁体   English

Dockerized Django REST应用程序和PostgreSQL DB

[英]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.

相关问题 Dockerized Django REST Framework / Postgresql-不允许的主机 - Dockerized Django REST Framework / Postgresql - Disallowed host 尝试使用PostgreSQL DB部署Dockerized Django应用程序时,Kubernetes Pod对象在无限循环中崩溃 - Kubernetes Pod Object crashing in infinite loop when trying to deploy Dockerized Django application with PostgreSQL DB 为Dockerized Django应用播种MySQL数据库 - Seeding a MySQL DB for a Dockerized Django App CORS在dockerized Django REST + Angular项目中被阻止 - CORS blocked in dockerized Django REST + Angular project 使用 PyCharm 调试 Dockerized Django 应用程序 - Debugging Dockerized Django Application Using PyCharm 如何使用相同的 DATABASE_HOST 从 dockerized django 和非 dockerized django 将 django 应用程序连接到 dockerized postgres db - How to connect a django app to a dockerized postgres db, both from a dockerized django and non-dockerized django using same DATABASE_HOST 如何使用 PostgreSQL DB 在 Django Rest Framework 中关联两个没有外键的 Django 模型 - How to relate two Django models without Foreign Key in Django Rest Framework using PostgreSQL DB 正在发生同一行的插入,以及如果DB Django-rest-framework,PostgreSQL中已经存在iti,如何拒绝插入 - Insertions of same row is happening and how to reject an insertion if iti is already present in DB Django-rest-framework,PostgreSQL 运行 Dockerized Django 应用程序时 Gunicorn 'Worker 无法启动' - Gunicorn 'Worker failed to boot' when running Dockerized Django application 如何为 Dockerized Django 应用程序选择正确的 Amazon-EC2 AMI? - How to choose the correct Amazon-EC2 AMI for a Dockerized Django application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM