Rafał
Rafał

Reputation: 602

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 ?

Upvotes: 7

Views: 9577

Answers (2)

Emre
Emre

Reputation: 1103

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

Upvotes: 3

Rafał
Rafał

Reputation: 602

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

Upvotes: 10

Related Questions