简体   繁体   English

Dockerized Django REST Framework / Postgresql-不允许的主机

[英]Dockerized Django REST Framework / Postgresql - Disallowed host

When I try to execute the following command: 当我尝试执行以下命令时:

sudo docker-compose up

I get the following error inside my terminal: 我在终端中收到以下错误:

在此处输入图片说明

Here is what my docker file looks like: 这是我的docker文件的样子:

FROM python:3.6

ENV PYTHONUNBUFFERED 1
RUN mkdir /agent-technologies
WORKDIR /agent-technologies
COPY . /agent-technologies 
RUN pip install -r requirements.txt

EXPOSE 8000

Here is what my docker-compose.yml 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

And finally here are settings of PostgresSQL DB I have previously created : 最后是我之前创建的PostgresSQL DB的设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'agent_technologies_db',
        'USER': 'stefan_radonjic',
        'PASSWORD': 'cepajecar995',
        'HOST': 'db',
        'PORT': '',
    }
}

Your django app is trying to connect to 'localhost' database, but as it is inside its own container -- localhost is not right uri to reach database. 您的django应用正在尝试连接到“本地主机”数据库,但由于它位于其自己的容器内-本地主机不适合访问数据库。

Dockercompose will resolve your namequeries to database if you will refer to is by db container name. 如果您是通过db容器名称引用的,则Dockercompose会将您的名称查询解析为数据库。 In your case it is "db" 您的情况是“ db”

As text on picture reads -- you have to add "0.0.0.0" into your ALLOWED_HOSTS variable inside your settings module. 阅读图片上的文字时-您必须在设置模块内的ALLOWED_HOSTS变量中添加“ 0.0.0.0”。

Be sure that you've read django documentation carefully. 确保您已仔细阅读django文档。 It is pretty vigorous. 它非常有活力。

The error is quite self explanatory.. You didn't set your ip in your ALLOWED_HOSTS setting . 该错误很容易解释。.您未在ALLOWED_HOSTS设置中设置 IP。 Try adding these to your settings file: 尝试将这些添加到您的设置文件:

ALLOWED_HOSTS = ['*'] # wildcard, allows all

Or if you want to be explicit: 或者,如果您想露骨:

ALLOWED_HOSTS = ['0.0.0.0'] # explicit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dockerized Django REST应用程序和PostgreSQL DB - Dockerized Django REST Application and PostgreSQL DB Django 不允许的主机 - Disallowed Host at Django 在AWS上托管Dockerized Django项目 - Host Dockerized Django project on AWS CORS在dockerized Django REST + Angular项目中被阻止 - CORS blocked in dockerized Django REST + Angular project 来自另一台主机的 Django REST 框架 sql 原始查询 - Django rest framework sql raw query from another host 如何在 django rest 框架中的分页 URL 中更改下一个键中的主机? - How to change the host in next key in a paginated URL in django rest framework? django rest 框架与 postgresql 中的时区感知日期问题 - timezone aware dates issue in django rest framework with postgresql 如何使用相同的 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 在AWS中部署Django应用程序。 提高不允许的主机异常 - Deploying Django application in AWS. Raise Disallowed Host exception 在Google Cloud Platform上不允许使用Django,Kubernetes和负载均衡器的主机 - Disallowed host with Django, Kubernetes and a Load Balancer on Google Cloud Platform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM