Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variable into docker container using docker-compose

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).

When I have been used docker build and docker run it was pretty easy.

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

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.

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

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 ?

like image 449
Rafał Avatar asked Jul 20 '26 16:07

Rafał


1 Answers

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.

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

like image 123
Rafał Avatar answered Jul 23 '26 05:07

Rafał



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!