python - 在docker compose yml文件中设置环境变量时出现文件未找到错误

标签 python docker docker-compose environment-variables google-cloud-speech

我正在 docker-compose.yml 文件中设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。这是我的 docker-compose.yml 文件的结构:

version: "2"

services:

  server:
    container_name: server

    environment:
        - GOOGLE_APPLICATION_CREDENTIALS=/dir1/dir2/file1.json

我的 JSON 文件位于上述目录中。从我的 python 代码(也作为 Docker 容器的一部分运行)中,我使用 Google Speech API,如下所示:

import io
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

#take speech wav file and turn it into text. return text
def transcribe_file(speech_file):
    """Transcribe the given audio file."""
    client = speech.SpeechClient()

当我执行上面的操作时,它会抛出错误 google.auth.exceptions.DefaultCredentialsError: File/dir1/dir2/file1.json was not found.

我无法理解为什么当 JSON 存在于正确的目录中时却找不到它。

最佳答案

确保将 /dir1/dir2/file1.json 添加到 Docker 镜像或将其安装为卷。仅将文件放在主机上的该位置是不够的。

关于python - 在docker compose yml文件中设置环境变量时出现文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52195378/

相关文章:

sql - 正在运行的应用程序使用的Docker数据库在哪里

spring-boot - zipkin2.reporter. 在 docker 中使用 Zipkin 和 kafka 时找不到 Sender bean

python - 每次需要使用虚拟环境时都需要激活它吗?

java - 持续集成 - 仅部署更改的组件

python - pyparsing - 解析 xml 注释

docker - Ping直到Docker启动或超时

linux - 如何通过 docker cp 复制目录?

django - Docker中的django和send_mail导致SMTPServerDisconnected

python - Tensorboard 标量绘图,横轴为纪元号

python /集/字典/初始化