如何使用docker-compose将环境变量设置到docker容器中 [英] How to set environment variable into docker container using docker-compose

查看:827
本文介绍了如何使用docker-compose将环境变量设置到docker容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置凭据以使用Google Translate Api客户端,因此必须设置环境变量 GOOGLE_APPLICATION_CREDENTIALS ,该值是凭据文件的路径(来自Google Cloud)。

I want to set credentials to use Google Translate Api Client so I have to set environment variable GOOGLE_APPLICATION_CREDENTIALS that value is path to credential file (from Google Cloud).

使用过 docker build docker run 这很容易。

我已经被使用过 docker run
--env GOOGLE_APPLICATION_CREDENTIALS = / usr / src / app / CryptoTraderBot-901d31d199ce.json 和环境变量已设置。

I have been used docker run --env GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/CryptoTraderBot-901d31d199ce.json and environment variable has been set.

当我尝试在docker-compose中进行设置时,会遇到更困难的事情。我必须使用docker-compose,因为我需要几个容器,因此这是实现此目的的唯一方法。

More difficult things come when I tried to set it in docker-compose. I have to use docker-compose because I need few containers so it is only way to achieve this.

基于 Docker撰写环境变量文档我创建了如下所示的docker-compose.yml文件:

Based on Docker compose environment variables documentation I created my docker-compose.yml file that looks like this:

version: "3"
services:
redis:
  image: redis:4-alpine
crypto-bot:
  build: .
  depends_on:
    - redis
  environment:
    - GOOGLE_APPLICATION_CREDENTIALS = /usr/src/app/CryptoTraderBot-901d31d199ce.json

我也尝试过.json文件路径的多种组合,但是这些都没有被正常使用。

I also have been tried multiple combination of path to .json file but none of this has been worked properly.

您知道如何正确设置吗?

Have you got any idea how can I set it properly ?

推荐答案

在创建此问题时,我已经以一种有趣而又简单的方式解决了这个问题,但是我一直认为我发布了答案

While creating this question I have been resolve this problem in a funny and easy way but I have been thought that I post answer to help someone in the future with similiar problem.

您要做的就是删除下一个 = 签名,因此docker-compose.yml的最后两行应如下所示:

环境:
-GOOGLE_APPLICATION_CREDENTIALS = / usr / src / app / CryptoTraderBot- 901d31d199ce.json

All you have to do is remove " " (space) next = sign so two last lines of docker-compose.yml should looks like this: environment: - GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/CryptoTraderBot-901d31d199ce.json

这篇关于如何使用docker-compose将环境变量设置到docker容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆