30

I want to set GOOGLE_APPLICATION_CREDENTIALS inside my docker container using my key.json file but I don't want to copy this file into my container.

katakuri
  • 327
  • 1
  • 3
  • 14
  • What do you want to use? There are many methods. One is to use a Docker Volume: https://docs.docker.com/storage/volumes/ Another is Docker Secrets: https://blog.docker.com/2017/02/docker-secrets-management/ – John Hanley Jul 22 '19 at 00:46
  • I want to set credentials while using docker run – katakuri Jul 22 '19 at 04:05
  • Did you read the links that I provided? The first one shows you how to use Docker run and attach a directory on your host so that you can read the file inside the container. – John Hanley Jul 22 '19 at 04:08

5 Answers5

27
 docker run \
   -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/[FILE_NAME].json \
   -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/[FILE_NAME].json:ro \
   gcr.io/[PROJECT_ID]/[IMAGE]

Ref: https://cloud.google.com/run/docs/testing/local

northtree
  • 8,569
  • 11
  • 61
  • 80
1

For anyone who is using a key file in the current working directory:

docker run \
  -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/[FILE_NAME].json \
  -v $(pwd):/tmp/keys/[FILE_NAME].json:ro \
  gcr.io/[PROJECT_ID]/[IMAGE]
micmia
  • 1,371
  • 1
  • 14
  • 29
0

I used something like norththree in https://stackoverflow.com/a/59727132/223960

But I did not want to have to put in the FILE_NAME all the time:

   docker run \
     -e GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS \
     -v $GOOGLE_APPLICATION_CREDENTIALS:$GOOGLE_APPLICATION_CREDENTIALS:ro \
     gcr.io/[PROJECT_ID]/[IMAGE]

This mounts the credential file at the same path in the docker container as it is outside. The only advantage of this is that you don't have to figure out FILE_NAME so you can put this in your README and your coworkers can just copy and paste into their shell.

Brian C.
  • 6,455
  • 3
  • 32
  • 42
0

I had the same problem. If you want to do it in Windows then:

Firstly add a system variable GOOGLE_APPLICATION_CREDENTIALS in windows: enter image description here

Make sure a path to your json file exists.

Reboot cmd if it was running. Exec echo %GOOGLE_APPLICATION_CREDENTIALS% command in cmd to be sure you did it correctly.

Then you should exec a command which runs a docker container with creating a json file in a container and setting an environment variable which points to this json file. -v flag creates this json file, syntax is path_to_your_json_file(here we put a system variable we set earlier):path_to_file_in_a_container_we_want_to_create:rights(ro means read only). -e flag creates a variable in a container which points to json file we set using -v flag (a value of this flag is the second part of a v flag value).

So the final command may look like this:

docker run -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/application_default_credentials.json -v %GOOGLE_APPLICATION_CREDENTIALS%:/tmp/keys/application_default_credentials.json:ro [other flags] image_name:image_tag
Andromeda
  • 1,205
  • 1
  • 14
  • 21
KaffLime
  • 1
  • 1
-1

If you want to set credentials using docker run, you can supply the file with the --env-file flag, or supply the arguement with --env

The tricky part is, since it's in json, you would have to cat the json file, grep the variable you're looking for, replace the colon with =, and remove the brackets. That's a lot of work.

Instead, why don't you just create file, inside the file use key = value notation, and supply the file with:

docker run --env-file my_config_file ....
alex067
  • 3,159
  • 1
  • 11
  • 17