This guide explains how to set up Google Cloud service account authentication for your Flutter Web Docker container. Service accounts provide persistent, non-expiring authentication that's ideal for containers and CI/CD environments.
- No token expiration: Unlike user credentials, service accounts don't expire
- No interactive prompts: Perfect for automated environments
- Project isolation: Each container can use its own service account
- Works for both Google Cloud and Firebase: One authentication method for all services
- Secure: Keys can be rotated and permissions limited
- Go to Google Cloud Console
- Select your project
- Navigate to IAM & Admin → Service Accounts
- Click Create Service Account
- Fill in the details:
- Name:
flutter-dev-container(or your preferred name) - ID: Will auto-generate
- Description: "Service account for Flutter development container"
- Name:
- Click Create and Continue
Grant the necessary roles based on your needs:
Firebase Admin- Full Firebase accessStorage Admin- For Firebase StorageCloud Datastore User- For Firestore
Project Editor- Broad permissions (use cautiously)Cloud Build Editor- For CI/CD pipelinesApp Engine Admin- For deployments
Click Continue after selecting roles.
- On the service account page, click Done
- Find your service account in the list and click on it
- Go to the Keys tab
- Click Add Key → Create new key
- Choose JSON format
- Click Create
- The key file will download automatically
-
Create auth directory in your project:
mkdir -p ./auth
-
Move the key file:
mv ~/Downloads/your-project-xxxxx.json ./auth/service-account-key.json -
Update .gitignore (IMPORTANT):
echo "auth/" >> .gitignore
-
Configure .env file:
# These are the defaults - no need to change if using standard setup USE_HOST_GCLOUD_AUTH=false GOOGLE_APPLICATION_CREDENTIALS=/home/developer/.gcloud/service-account-key.json # Optional: Set project ID (will be auto-detected from key) GOOGLE_CLOUD_PROJECT=your-project-id
./start.shThe container will automatically:
- Detect the service account key
- Authenticate with Google Cloud
- Extract the project ID from the key
- Configure Firebase with the same credentials
- Set up Application Default Credentials
Once the container starts, verify authentication:
docker exec -it flutter-web-dev bash -c "gcloud auth list"You should see your service account email listed as ACTIVE.
- Check that
./auth/service-account-key.jsonexists - Ensure the file is valid JSON
- Verify volume mount in docker-compose.yml
- The JSON file may be corrupted
- Download a fresh key from GCP Console
- Check file permissions
- The service account key contains the project ID
- If you need a different project, create a service account in that project
- Or explicitly set
GOOGLE_CLOUD_PROJECTin .env
- Never commit keys: Always add
auth/to .gitignore - Limit permissions: Only grant necessary roles
- Rotate keys: Regularly create new keys and delete old ones
- Use separate accounts: Different service accounts for dev/staging/prod
- Monitor usage: Check service account activity in GCP Console
For production Kubernetes environments, consider Workload Identity instead of service account keys. This provides keyless authentication but requires more setup.
If you encounter issues:
- Check
~/.auth-statusinside the container for debugging info - Review container logs:
docker logs flutter-web-dev - Ensure your service account has the necessary permissions
- Verify the key file is valid JSON