在docker compose yml文件中设置环境变量时找不到文件错误

问题描述 投票:0回答:1

我在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。

python docker docker-compose environment-variables google-cloud-speech
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.