【问题标题】:When docker run, an error occurs. "ValueError: Unable to configure handler 'watchtower': You must specify a region."当 docker run 时,会发生错误。 “ValueError:无法配置处理程序'瞭望塔':您必须指定一个区域。”
【发布时间】:2017-05-31 08:46:18
【问题描述】:

首先,我使用服务器环境

  • 服务器:django + nginx + uwsgi
  • 云:docker + AWS ECS
  • 日志记录:AWS CloudWatch 日志服务 + watchtower 第三方应用程序

如果我使用python manage.py runserver 在本地运行服务器,则日志会很好地存储在 CloudWatch 日志中。但是,如果我使用 docker 和docker run --rm -it -p 8080: 80 image_name 命令构建我的项目,则会出现以下错误。

Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/config.py", line 558, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.5/logging/config.py", line 731, in configure_handler
    result = factory(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/watchtower/__init__.py", line 78, in __init__
    self.cwl_client = (boto3_session or boto3).client("logs")
  File "/usr/local/lib/python3.5/dist-packages/boto3/__init__.py", line 83, in client
    return _get_default_session().client(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/boto3/session.py", line 263, in client
    aws_session_token=aws_session_token, config=config)
  File "/usr/local/lib/python3.5/dist-packages/botocore/session.py", line 836, in create_client
    client_config=config, api_version=api_version)
  File "/usr/local/lib/python3.5/dist-packages/botocore/client.py", line 70, in create_client
    verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "/usr/local/lib/python3.5/dist-packages/botocore/client.py", line 224, in _get_client_args
    verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "/usr/local/lib/python3.5/dist-packages/botocore/args.py", line 45, in get_client_args
    endpoint_url, is_secure, scoped_config)
  File "/usr/local/lib/python3.5/dist-packages/botocore/args.py", line 103, in compute_client_args
    service_name, region_name, endpoint_url, is_secure)
  File "/usr/local/lib/python3.5/dist-packages/botocore/client.py", line 297, in resolve
    service_name, region_name)
  File "/usr/local/lib/python3.5/dist-packages/botocore/regions.py", line 122, in construct_endpoint
    partition, service_name, region_name)
  File "/usr/local/lib/python3.5/dist-packages/botocore/regions.py", line 135, in _endpoint_for_partition
    raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "django_app/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/log.py", line 75, in configure_logging
    logging_config_func(logging_settings)
  File "/usr/lib/python3.5/logging/config.py", line 795, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.5/logging/config.py", line 566, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'watchtower': You must specify a region.

错误信息指出了区域问题,我不知道如何解决。 django 日志设置如下所示。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'watchtower': {
            'level': 'DEBUG',
            'class': 'watchtower.CloudWatchLogHandler',
            'formatter': 'verbose',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['watchtower', 'console'],
            'level': 'INFO',
            'propagate': True,
        },
        'django.user': {
            'handlers': ['watchtower'],
            'level': DJANGO_LOG_LEVEL,
            'propagate': False,
        },
        'django.partner': {
            'handlers': ['watchtower'],
            'level': DJANGO_LOG_LEVEL,
            'propagate': False,
        }
    }
}

问题出在哪里?

【问题讨论】:

    标签: django amazon-web-services docker amazon-cloudwatchlogs


    【解决方案1】:

    您需要在容器内提供 aws 凭据。您可以使用卷动态安装它们:

    docker run -v $HOME/.aws:/root/.aws --rm -it -p 8080: 80 image_name
    

    您的凭据应该在本地$HOME/.aws,然后将它们挂载到运行您的应用程序的用户的主目录中(如果用户是其他用户,请将 /root 更改为另一个目录)

    【讨论】:

    • 谢谢。您可以使用您的命令在本地创建一个 docker 容器。
    • 嗨@Robert 实际上,我只想再问你一个问题。我将一个容器上传到 AWS ECS,此时无法执行“docker run”。所以我在 Dockerfile 中添加了“COPY .aws / /.aws”命令,我已验证此方法通过了凭据。 'docker exec' 查看容器内部。 .aws 目录通常复制到根路径。但它不会传递像 'docker run -v $ HOME / .aws: /root/.aws --rm -it -p 8080: 80 image_name' 这样的凭据。有没有其他办法?
    • 啊...我的第一条评论是错误的。 :( 你可以创建 => 我可以创建
    • 我在下面的网址中写了上面的问题。谢谢stackoverflow.com/questions/44299786/…
    • 好的,我去看看!
    【解决方案2】:

    您的容器内未配置 AWS 区域,并且可能也未配置凭证。看看documentationhere

    CLI 为上一节中配置的配置文件生成的文件如下所示:

    ~/.aws/凭据

    [默认] aws_access_key_id=AKIAIOSFODNN7EXAMPLE aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

    ~/.aws/config

    [默认] 地区=us-west-2 输出=json

    支持以下设置。

    aws_access_key_id – AWS 访问密钥。

    aws_secret_access_key – AWS 密钥。

    aws_session_token – AWS 会话令牌。仅当您使用临时安全凭证时才需要会话令牌。

    区域 – AWS 区域。

    output – 输出格式(json、文本或表格)

    【讨论】:

    • 但是,如果我把django日志改成默认,docker run没问题,推送到AWS ECS也成功了,所以服务器和nginx配合得很好。即使我在使用瞭望塔处理程序时遇到错误,这是否是凭据问题?本地 .aws 文件夹当然是用凭据设置的。
    • 谢谢。正如您所说,凭据问题是正确的。多亏了本地测试成功。 @Raf
    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2017-03-15
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多