Dockerized Django应用程序拒绝连接

时间:2018-09-25 01:11:49

标签: python django docker docker-compose dockerfile

当我输入0.0.0.0:8000甚至我的docker-machine ip时,我在Docker上运行的Django应用程序都不允许我连接到它。

错误显示如下:

  

Safari无法打开页面“ 0.0.0.0:8000”,因为服务器意外断开了连接。服务器繁忙时,有时会发生这种情况。等待几分钟,然后重试。

另外,当我尝试docker-machine ssh default时,我输入wget -qO- 127.0.0.1:8000来查看服务器是否允许我从docker vm内部进行连接,我会收到此错误响应

  

docker @ default:〜$ wget -qO- 127.0.0.1:8000   wget:无法连接到远程主机(127.0.0.1):连接被拒绝

Dockerfile

FROM python:3.6-alpine

# Copying requirements files for python modules
ADD requirements.txt /requirements.txt

# Installing build deps for running images
RUN apk add --no-cache --virtual .build-deps \
  build-base postgresql-dev libffi-dev \
  && pip install -r requirements.txt \
  && find /usr/local \
      \( -type d -a -name test -o -name tests \) \
      -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
      -exec rm -rf '{}' + \
  && runDeps="$( \
    scanelf --needed --nobanner --recursive /usr/local \
            | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
            | sort -u \
            | xargs -r apk info --installed \
            | sort -u \
)" \
&& apk add --virtual .rundeps $runDeps \
&& apk del .build-deps

# Copying application code for the docker container to persist
RUN mkdir /src
COPY src/ /src
WORKDIR /src

# uWSGI port opening
EXPOSE 8000

# DJANGO enviroment settings
RUN DATABASE_URL=none /usr/local/bin/python manage.py collectstatic --noinput

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
382f6202153d        dronerestapi_app    "python manage.py ru…"   4 minutes ago       Up 4 minutes        0.0.0.0:8000->8000/tcp      dronerestapi_app_1
db6e0145a5da        postgres:9.6        "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:5432->5432/tcp   dronerestapi_db_1

docker-compose.yml

version: '2'

services:
db:
    environment:
        POSTGRES_DB: drone_db
        POSTGRES_USER: app_user
        POSTGRES_PASSWORD: changeme
    image: postgres:9.6
    ports:
        - 5432:5432
    volumes:
        - ./postgres-data:/var/lib/postgresql/data

app:
    environment:
        DATABASE_URL: postgres://app_user:changeme@drone_db
    build:
        context: .
        dockerfile: ./Dockerfile
    volumes:
        - ./src/:/src
    ports:
        - 8000:8000
    command: python manage.py runserver 0.0.0.0:8000
    depends_on:
        - db

如您所见,我公开了所有端口,所以我认为唯一出错的是Django Settings error下面的设置

settings.py

"""
Django settings for core project.

Generated by 'django-admin startproject' using Django 1.11.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'fqa*89n0thw=bjr1-)&)l%@k5--17mhndy@!98u0(!@1hynzox'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS =  '*'


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'drones',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'core.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
       },
    },
]

WSGI_APPLICATION = 'core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'drone_db',
    'USER': 'app_user',
    'PASSWORD': 'changeme',
    'HOST': 'db',
    'PORT': '5432',
    }
}


 # Password validation
 # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-   validators

AUTH_PASSWORD_VALIDATORS = [
    {
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

我不知道该怎么做,我尝试做的一切只是导致我无法连接到Django服务器,我可以通过GUI工具很好地连接到Postgres服务器。

0 个答案:

没有答案