Recommended GCE service account authentication inside Docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If a Docker container runs on a Google Compute Engine VM, the recommended authentication method is usually not copying a JSON key into the image. The better approach is to attach a service account to the VM and let the application use Application Default Credentials from the metadata server.
Why Metadata-Based Authentication Is Preferred
A service-account key file is a long-lived credential. If you bake it into an image or mount it carelessly, you create extra rotation, leakage, and distribution problems. On GCE, the platform already exposes short-lived access tokens for the VM's attached service account through the metadata server.
Google client libraries know how to use that automatically through Application Default Credentials, often called ADC.
Recommended Setup on GCE
The secure pattern is:
- Create or choose a dedicated service account.
- Grant it only the roles the workload needs.
- Attach that service account to the GCE VM.
- Run the container without embedding key files.
- Use Google client libraries normally inside the container.
From inside the container, the code usually does not need any special path configuration if it can reach the metadata server.
Example with Python
The Python client library can obtain credentials automatically.
When this code runs inside a container on a properly configured GCE VM, storage.Client() can resolve credentials from ADC and talk to Google Cloud Storage without a JSON key file.
What Must Be True for This to Work
The VM needs a service account attached, and that service account needs the right IAM roles. The VM also needs API access scopes that do not block the required services, especially on older instance configurations.
Inside the container, outbound access to the metadata server must be available. In most ordinary Docker-on-GCE setups, that works automatically because the container shares the host network path to the metadata endpoint.
Avoid Putting Keys in the Image
A Dockerfile like this is the pattern you should avoid:
It works technically, but it turns the image into a secret distribution vehicle. Anyone with the image may gain access to that credential unless you wrap the whole process in very careful controls.
If you must use a key outside GCE or in local development, mount it at runtime instead of baking it into the image.
Local Development Versus Production
It is normal for local development to use a different authentication flow than production. For example, on a laptop you might set GOOGLE_APPLICATION_CREDENTIALS to a local file or use gcloud auth application-default login.
In production on GCE, prefer the attached service account and metadata-backed ADC. The application code can stay the same while the environment determines how credentials are resolved.
Debugging Credential Resolution
If authentication fails inside the container, check the environment in this order:
- Does the VM have the expected service account attached?
- Does that service account have the required IAM roles?
- Can the container reach the metadata server?
- Is some incorrect
GOOGLE_APPLICATION_CREDENTIALSvalue overriding ADC?
A broken environment variable is a common cause of confusion because it can force the library to use a missing or invalid key file instead of the metadata-based identity.
Common Pitfalls
The most common mistake is storing a service-account key in the image for convenience. That creates an avoidable secret-management problem.
Another issue is granting the VM's service account broad project-wide roles when the container needs only one narrow permission set. Use least privilege.
Developers also sometimes forget that IAM roles and legacy API scopes both matter on some GCE setups. Correct IAM alone is not always enough if scopes are restricted.
Summary
- On GCE, prefer the VM's attached service account and Application Default Credentials.
- Do not bake JSON service-account keys into the Docker image.
- Let Google client libraries fetch short-lived credentials from the metadata server.
- Give the attached service account only the roles the workload actually needs.
- Use key files only for environments where metadata-based credentials are unavailable.
Related reading
- Redeploy spring-boot application in docker container?
- Redirecting command output in docker
- Reloading code in a dockerized node.js app with docker-compose
- Remove Docker images from Nexus Repository Manager OSS 3.0.1-01
- Recommended way to manage credentials with multiple AWS accounts?
- Recursive Fetch All Items In DynamoDB Query using Node JS
- Refreshing OAuth token using Retrofit without modifying all calls
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.