4

I was looking to externalize application configuration for containerized applications on Google Cloud Run. I know there are environment variables available for cloud run application and I want to have something as Config Server for Cloud Run.

Is there any out of the box support available on GCP?

Michal
  • 15,429
  • 10
  • 73
  • 104
Ady
  • 584
  • 8
  • 16

2 Answers2

2

When setting up your Cloud Run deployment, you can simply inject environment variables into your service:

enter image description here

Because Spring Boot comes with application.properties mechanism, you can easily override those values exactly from the environment variables. Do keep in mind, that the syntax is slightly different:

application.properties

  • spring.profiles.active=dev

environment variables

  • SPRING_PROFILES_ACTIVE=dev

Injected env variables will take precedence over the ones defined in your application.properties file.

Michal
  • 15,429
  • 10
  • 73
  • 104
  • How about container arguments? Do you know how to pass them? – Ady Feb 13 '20 at 10:34
  • @Ady technically that should be one and the same thing. Think of it as setting an env variable in your command line. It's there for anything running to use. So if your container environment is expecting some variable, you can pass it the same way. – Michal Feb 13 '20 at 11:12
  • @Michal, we added SPRING_PROFILES_ACTIVE=dev to Cloud Run, and, we added spring.profiles.active=dev in application.properties, but got the error "Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. ". Are we missing something in Dockerfile, we have ENTRYPOINT as follows "ENTRYPOINT exec java -jar /app.jar". Thank you – Zi Sang Sep 28 '20 at 07:13
  • Does this work for any variable which you would set in application.properties? Just need to capitalise and replace full stop with underscore? – Chris A Apr 15 '23 at 00:20
  • @ChrisA yes, as far as I know. If it's something custom, you need to implement serialization in your Spring app, but existing values can be overriden like this. – Michal Apr 17 '23 at 10:51
  • Thanks Michal. Any idea how you would handle it if your Spring variable already had an underscore in it, e.g. `api.client_secret`? How would this translate on cloud run? – Chris A Apr 17 '23 at 11:53
  • This is the answer you're looking for - https://stackoverflow.com/a/40612891/1196908 – Michal Apr 20 '23 at 13:48
1

There are two solutions to it :

  1. If your docker file is "ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/****.jar"]" then use "-Dspring.profiles.active=dev" in the container arguments on the cloud run.

  2. In case your docker file has "CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/***.jar"]" You can do it by setting Environment Variable as SPRING_PROFILES_ACTIVE and value as dev in "Variables & Secrets" tab on cloud run Container Configurationenter image description here

Abhay Phougat
  • 280
  • 2
  • 6