【发布时间】:2018-02-18 19:57:24
【问题描述】:
我想在 Docker 容器中使用 Google Cloud SDK 和 Django 项目。假设我想使用
from google.cloud import vision
from google.cloud.vision import types
当我使用requirements.txt 文件安装google-cloud 时,出现如下所示的错误。在我没有容器的本地机器上,它可以正常工作。有谁知道我该如何解决这个问题?
django_1 | oauth2client.client.ApplicationDefaultCredentialsError:应用程序默认凭据不可用。他们是 如果在 Google Compute Engine 中运行,则可用。否则,该 必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。看 https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。
更新 我在 MacBook 上工作,我在 this website 上找到了这个:
docker run --rm -it -v ~/.config/gcloud:/.config/gcloud google/cloud-sdk
此时我收到如下所示的错误。有什么想法可以解决这个问题吗?
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
django_1 | response = get_response(request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
django_1 | response = self.process_exception_by_middleware(e, request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
django_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
django_1 | return view_func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
django_1 | return self.dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
django_1 | response = self.handle_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
django_1 | self.raise_uncaught_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
django_1 | response = handler(request, *args, **kwargs)
django_1 | File "/code/backend/views.py", line 23, in get
django_1 | client = vision.ImageAnnotatorClient()
django_1 | File "/usr/local/lib/python3.6/site-packages/google/cloud/gapic/vision/v1/image_annotator_client.py", line 147, in __init__
django_1 | ssl_credentials=ssl_credentials)
django_1 | File "/usr/local/lib/python3.6/site-packages/google/gax/grpc.py", line 106, in create_stub
django_1 | credentials = _grpc_google_auth.get_default_credentials(scopes)
django_1 | File "/usr/local/lib/python3.6/site-packages/google/gax/_grpc_google_auth.py", line 62, in get_default_credentials
django_1 | credentials, _ = google.auth.default(scopes=scopes)
django_1 | File "/usr/local/lib/python3.6/site-packages/google/auth/_default.py", line 282, in default
django_1 | raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
django_1 | google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
django_1 | explicitly create credential and re-run the application. For more
django_1 | information, please see
django_1 | https://developers.google.com/accounts/docs/application-default-credentials.
【问题讨论】:
-
试试这个
docker run -e "GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS" --rm -it -v ~/.config/gcloud:/.config/gcloud google/cloud-sdk -
@TarunLalwani 使用你的命令后有
root@4271b8a28802:/#并且仍然是同样的错误。 -
你能在你的mac上运行
env并发布所有google相关的环境变量吗?将任何需要私有的值替换为虚拟值。 -
@TarunLalwani 你是说我添加到更新问题中的信息吗?
标签: python django python-3.x docker google-cloud-platform