django_debug_toolbar和Docker

所以我让Docker和Django在本地工作,首先通过Dockerfile构build一个映像,然后使用Fig获取postgres映像,将其链接到基本映像,然后运行本地服务器。 一切正常,除了django_debug_toolbar。 出于某种原因,它不会显示出来。 在internal_ips中也有dockerhost ip。 任何人都可以帮助我吗? Docker通过boot2docker在mac上运行。

谢谢!

我的设置:

init .py

import os if 'DEBUG' not in os.environ or not os.environ['DEBUG']: from .local import * else: pass 

base.py

 """ common and global settings """ from sys import path from os import environ from os.path import abspath, basename, dirname, join, normpath from django.core.exceptions import ImproperlyConfigured import dj_database_url def get_env_variable(var_name): try: return environ[var_name] except KeyError: error_msg = "Set the environment variable" % var_name raise ImproperlyConfigured(error_msg) # Paths DJANGO_ROOT = dirname(dirname(abspath(__file__))) SITE_ROOT = dirname(DJANGO_ROOT) SITE_NAME = basename(DJANGO_ROOT) # End Paths # URLs MEDIA_ROOT = normpath(join(SITE_ROOT, 'media')) MEDIA_URL = "/media/" STATIC_ROOT = normpath(join(SITE_ROOT, 'assets')) STATIC_URL = "/static/" STATICFILES_DIRS = ( normpath(join(SITE_ROOT, 'static')), ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) ROOT_URLCONF = '%s.urls' % SITE_NAME path.insert(0, join(DJANGO_ROOT, 'apps')) # add apps folder to system path # End URLs # Database # example: postgres://joris:luna@localhost/bitbybit DATABASES = {'default': dj_database_url.config( default='postgres://postgres@db:5432/postgres')} # End Database # Templates TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.static', 'django.core.context_processors.tz', 'django.contrib.messages.context_processors.messages', 'django.core.context_processors.request', ) TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) TEMPLATE_DIRS = ( normpath(join(SITE_ROOT, 'templates')), ) # End Templates # SECURITY WARNING: keep the secret key used in production secret! # make it unique and store it as an environment variable SECRET_KEY = r"d%g7_h6cz=xbhs*5-i+e$c7mns*s)^_+#^8n@^-2dno@uie-z9" # Application DJANGO_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ) LOCAL_APPS = ( 'home', ) INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware', ) WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME # End Application # Internationalization LANGAUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # End Internationalization 

Local.py

 from .base import * # Debug config DEBUG = True TEMPLATE_DEBUG = DEBUG # End Debug config # Hosts ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] # End Hosts # Django Debug Toolbar config INSTALLED_APPS += ( 'debug_toolbar', ) INTERNAL_IPS = ('127.0.0.1', 'localhost') # End Django Debug Toolbar config 

允许我显示django工具栏的IP地址是与我的docker容器关联的getway的IP地址。 为了获得gatway的IP,我运行这个命令

 docker inspect my_container_name | grep -e '"Gateway"' # "Gateway": "172.18.0.1", 

总的来说,我的设置看起来像这样

 INSTALLED_APPS = ( 'debug_toolbar', ) INTERNAL_IPS = ['172.18.0.1'] 

解决了。 检查请求标头中的REMOTE_ADDR的值,并将其添加到INTERNAL_IPS。

使用接受的答案在https://unix.stackexchange.com/questions/87468/is-there-an-easy-way-to-programmatically-extract-ip-address我能得到这个工作,通过传递地址作为环境variables,将主机的Docker桥连接到docker run命令:

 -e "DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')" 

使用该设置, settings.py的以下两行检测到它,并允许主机看到工具栏:

 if "DOCKER_HOST" in os.environ: INTERNAL_IPS = [os.environ["DOCKER_HOST"]] 

如果你想以编程方式做这个,而不是复制/粘贴你的容器IP,我build议你喜欢Django的cookiecutter人。 在您的本地设置文件中:

 INTERNAL_IPS = ['127.0.0.1', ] import socket import os # tricks to have debug toolbar when developing with docker ip = socket.gethostbyname(socket.gethostname()) INTERNAL_IPS += [ip[:-1] + '1'] 

作为参考,这是django-cookiecutter local.py设置文件的链接。

这是一种不需要将地址注入容器的方法。 我不确定如果你的容器连接到多个networking,如果docker改变了他们分配地址的方式(如果这看起来不太可能),它将会如何工作。

 import subprocess route = subprocess.Popen(('ip', 'route'), stdout=subprocess.PIPE) network = subprocess.check_output( ('grep', '-Po', 'src \K[\d.]+\.'), stdin=route.stdout).decode().rstrip() route.wait() network_gateway = network + '1' INTERNAL_IPS = [network_gateway]