【问题标题】:Failed to run django app using docker containers无法使用 docker 容器运行 django 应用程序
【发布时间】:2020-12-30 04:36:54
【问题描述】:

我正在尝试在 localhost:8000 上运行 Django 应用程序。

我有一个创建 3 个容器的 docker-compose.yml 文件:

  • badgr-server_api
  • badgr-server_memcached
  • badgr-server_db_

当我运行 docker-compose up 时,它说启动 2 个容器并创建第 3 个 (badgr-server_api),我只看到 memchaced 和 db 正在运行,请参见屏幕截图。我已经尝试删除badgr-server_api 并再次运行它,我尝试删除图像。

我也再次运行docker-compose build,但我无法让它运行。希望有人能帮忙。

docker-compose up screenshot

运行docker后,我也运行了这个命令:

docker-compose exec api python /badgr_server/manage.py migrate

并得到这个结果:

Traceback (most recent call last):
  File "/badgr_server/manage.py", line 13, in <module>
   
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 212, in get_connection_params
    isolation_level = options.pop('isolation_level', 'read committed')
TypeError: pop() takes no arguments (2 given)

这是我的 docker-compose.yml:

# A dockerized badgr-server stack for development
version: '3.3'
services:

  # this container mirrors in the app code and runs the django dev server
  api:
    build:
      context: .
      dockerfile: .docker/Dockerfile.dev.api
    depends_on:
      - "db"
      - "memcached"
    command: /badgr_server/manage.py runserver 0.0.0.0:8000
    volumes:
      - ./apps:/badgr_server/apps
      - ./manage.py:/badgr_server/manage.py
      - ./.docker/etc/settings_local.dev.py:/badgr_server/apps/mainsite/settings_local.py
    networks:
      - badgr
    ports:
      - "8000:8000"

  # this container runs memcached
  memcached:
    image: 'bitnami/memcached:latest'
    expose:
      - "11211"
    networks:
      - badgr

  # this container runs mysql (database)
  db:
    image: mysql:5.6.39
    volumes:
      - badgr_server_dev_db:/var/lib/mysql:rw
      - ./.docker/etc/init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      - "MYSQL_PASSWORD=password"
      - "MYSQL_HOST=db"
      - "MYSQL_ROOT_PASSWORD=password"
    expose:
      - "3306"
    networks:
      - badgr

networks:
  badgr:
    driver: bridge

volumes:
  badgr_server_dev_db:
> import random import string from .settings import * from mainsite
> import TOP_DIR
> 
> DEBUG = False DEBUG_ERRORS = DEBUG DEBUG_STATIC = DEBUG DEBUG_MEDIA =
> DEBUG
> 
> TIME_ZONE = 'America/Los_Angeles' LANGUAGE_CODE = 'en-us'
> 
> 
> ##
> #
> # Database Configuration
> #
> ## DATABASES = {
>     'default': {
>         'ENGINE': 'django.db.backends.mysql',
>         'NAME': 'badgr',
>         'USER': 'root',
>         'PASSWORD': 'password',
>         'HOST': 'db',
>         'PORT': '',
>         'OPTIONS': {
>             "SET character_set_connection=utf8mb3, collation_connection=utf8_unicode_ci"
>             #            ,  # Uncomment when using MySQL to ensure consistency across servers
>         },
>     } }
> 
> 
> ###
> #
> # CACHE
> #
> ### CACHES = {
>      'default': {
>          'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
>          'LOCATION': 'memcached:11211',
>          'KEY_FUNCTION': 'mainsite.utils.filter_cache_key'
>      }  }
> 
> 
> 
> ###
> #
> # Email Configuration
> #
> ### DEFAULT_FROM_EMAIL = 'noreply@example.com'  # e.g. "noreply@example.com" EMAIL_BACKEND =
> 'django.core.mail.backends.console.EmailBackend'
> 
> 
> ###
> #
> # Celery Asynchronous Task Processing (Optional)
> #
> ### CELERY_RESULT_BACKEND = None
> # Run celery tasks in same thread as webserver (True means that asynchronous processing is OFF) CELERY_ALWAYS_EAGER = True
> 
> 
> ###
> #
> # Application Options Configuration
> #
> ### HTTP_ORIGIN = 'http://localhost:8000' ALLOWED_HOSTS = ['*'] STATIC_URL = HTTP_ORIGIN + '/static/'
> 
> CORS_ORIGIN_ALLOW_ALL = False CORS_ORIGIN_WHITELIST = (
>     'http://localhost:4200', )
> 
> # Optionally restrict issuer creation to accounts that have the 'issuer.add_issuer' permission BADGR_APPROVED_ISSUERS_ONLY = False
> 
> # Automatically send an email the first time that recipient identifier (email type) has been used on the system.
> GDPR_COMPLIANCE_NOTIFY_ON_FIRST_AWARD = True
> 
> SECRET_KEY = ''.join(random.choice(string.ascii_uppercase +
> string.digits) for _ in range(40)) UNSUBSCRIBE_KEY =
> ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in
> range(40)) UNSUBSCRIBE_SECRET_KEY = str(SECRET_KEY)
> 
> 
> ###
> #
> # Logging
> #
> ### LOGS_DIR = os.path.join(TOP_DIR, 'logs') if not os.path.exists(LOGS_DIR):
>     os.makedirs(LOGS_DIR) LOGGING = {
>     'version': 1,
>     'disable_existing_loggers': False,
>     'handlers': {
>         'mail_admins': {
>             'level': 'ERROR',
>             'filters': [],
>             'class': 'django.utils.log.AdminEmailHandler'
>         },
> 
>         # badgr events log to disk by default
>         'badgr_events': {
>             'level': 'INFO',
>             'formatter': 'json',
>             'class': 'logging.FileHandler',
>             'filename': os.path.join(LOGS_DIR, 'badgr_events.log')
>         }
>     },
>     'loggers': {
>         'django.request': {
>             'handlers': ['mail_admins'],
>             'level': 'ERROR',
>             'propagate': True,
>         },
> 
>         # Badgr.Events emits all badge related activity
>         'Badgr.Events': {
>             'handlers': ['badgr_events'],
>             'level': 'INFO',
>             'propagate': False,
> 
>         }
> 
>     },
>     'formatters': {
>         'default': {
>             'format': '%(asctime)s %(levelname)s %(module)s %(message)s'
>         },
>         'json': {
>             '()': 'mainsite.formatters.JsonFormatter',
>             'format': '%(asctime)s',
>             'datefmt': '%Y-%m-%dT%H:%M:%S%z',
>         }
>     }, }

【问题讨论】:

  • 您可以使用您的设置文件,尤其是数据库部分吗?
  • 这可能是因为你的 DATABASE 设置是一个列表,而它应该是一个字典。
  • 屏幕截图唯一显示的是 api 服务器没有产生输出。它可能正在运行。
  • 谢谢@Taek 我已经添加了设置文件,我将查看数据库设置,我认为这是一个字典。如您所知,我是新手,但感谢您的帮助,如果您在设置文件中看到任何内容,请告诉我。
  • @NickODell 在第 4 行显示“完成”,我认为这意味着容器正在运行没有任何问题,但它没有像其他 2 个容器那样显示任何进一步的信息。感谢您抽出宝贵时间留下您的反馈。

标签: python django docker docker-compose containers


【解决方案1】:

您的DATABASES.default.OPTIONS 应该是字典而不是集合,如果您不习惯的话,很难看出两者之间的区别:

In [5]: type({'key': 'value'})
Out[5]: dict

In [6]: type({'key and no value'})
Out[6]: set

这就是您收到 pop() takes no arguments (2 given) 错误消息的原因,因为这就是 set.pop 的工作原理:https://docs.python.org/3.7/library/stdtypes.html#frozenset.pop

您应该能够通过使用格式正确的 dict 来使其工作而无需注释选项,例如:

DATABASES = {
    "default": {
        ...
        "OPTIONS": {
            "character_set_connection": "utf8mb3",
            "collation_connection": "utf8_unicode_ci",
        },
    }
}

我不确定确切的语法,尤其是值 "utf8mb3""utf8_unicode_ci" 我认为应该是枚举。我没有 mysql db 可以试用,所以你必须自己找到它。

【讨论】:

  • 非常感谢,你说得对,应该是dict :)
猜你喜欢
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2020-12-19
  • 2017-08-03
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多