7

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 ?

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

Man, you are rock! I have been searching for my problem for 2 hours.
3

Docker Compose has a newer feature called secrets. You can bind the credentials like this:

services:
  secret-service:
      build:
        context: secret-service
      environment:
        - GOOGLE_APPLICATION_CREDENTIALS=/run/secrets/gcp-credentials
      secrets:
        - gcp-credentials

secrets:
  gcp-credentials:
    file: ./gcp-credentials.json

Reference: https://docs.docker.com/compose/compose-file/#secrets

1 Comment

On Ubuntu, you may use this: file: $HOME/.config/gcloud/application_default_credentials.json under secrets:gcp-credentials

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.