How to add local GOOGLE_APPLICATION_CREDENTIALS to docker on docker run command?

I’m trying to run a docker container with an application that uses some GCP services.

I have created and downloaded a service account json key, and I can run the application outside docker when I run

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/project-credentials-123.json"

before running the application.

However, when I run the application inside docker, the GCP client libraries can’t find the default access keys.

The docker run command I’m using looks like:

docker run -p 8501:8501 -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/project-credentials-123.json -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/project-credentials-123.json:ro 571a38aaaf15 streamlit run src/app/app.py

where 571a38aaaf15 is the docker image id.

Note that I copied my project-credentials-123.json to /tmp/keys/ to better follow https://cloud.google.com/run/docs/testing/local


Note: similar to Set GOOGLE_APPLICATION_CREDENTIALS in Docker, but I can’t comment there due to < 50 reputation.

Source: StackOverflow