Quick revision
The short answer
Build the container image once and promote that exact image from dev to test to production. Everything that differs between environments, such as database URLs, feature switches and passwords, is supplied from outside at run time: environment variables, Kubernetes ConfigMaps and Secrets, or mounted files that Spring Boot imports with spring.config.import or spring.config.additional-location. Secrets and environment-specific profiles never go into the image.
What to remember
In this answer, we saw why a container image should be built once and promoted unchanged, and how Spring Boot gets the differences between environments from outside. Environment variables override packaged defaults through relaxed binding, ConfigMaps and Secrets supply those variables or mounted files, and spring.config.import or spring.config.additional-location load the files. The image keeps only safe defaults, and environment profiles with secrets stay out of it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Build the container image once and promote that exact image from dev to test to production. Everything that differs between environments, such as database URLs, feature switches and passwords, is supplied from outside at run time: environment variables, Kubernetes ConfigMaps and Secrets, or mounted files that Spring Boot imports with spring.config.import or spring.config.additional-location. Secrets and environment-specific profiles never go into the image.
Why this question is asked
Almost every team that moves a Spring Boot service into containers meets this question in its first week: the image works on a laptop, and now it has to run against three different databases with three different sets of credentials. Interviewers ask it because the answer shows whether we understand why an image should be immutable, and whether we know the tools Spring Boot gives us to read configuration from the environment. In this answer, we will recap what an image is and why we build it only once, then look at how Spring Boot reads external configuration, how to feed it values with environment variables, ConfigMaps and Secrets, how to import mounted files, and finally which mistakes to avoid.
1A Quick Recap: Build Once, Run Everywhere
A container image is a frozen package of our application: the JDK, the jar and its dependencies, all stacked in read-only layers. Every container started from that image is identical, and the image is identified by a digest, a hash of its contents such as sha256:4f2a and so on.
The idea behind building once is simple. What we test must be what we ship. If we build a separate image for production, even from the same commit, the bytes can differ: a base image may have been updated in between, or a dependency resolved to a newer patch version. Then the image we tested in staging is not the image running in production, and any bug that the difference causes was never tested. So the pipeline builds one image, tags it with the commit, and promotes the same digest from environment to environment.
That leaves one question: if the image is the same everywhere, where does the difference between dev and production come from? The answer is that the environment supplies it when the container starts.
2How Spring Boot Reads Configuration From Outside
Spring Boot builds its Environment, the set of all configuration values the application can see, from many property sources in a fixed order. Values from later sources override earlier ones. The ones that matter here are, from weaker to stronger: application.properties inside the jar, profile-specific files inside the jar, files outside the jar, operating system environment variables, Java system properties and command-line arguments.
Environment variables are the easiest bridge into a container. Spring Boot uses relaxed binding, which means it maps the upper-case, underscore-separated form of a name to the property it stands for. SPRING_DATASOURCE_URL becomes spring.datasource.url, and SERVER_PORT becomes server.port. So we do not need any special code to read them.
- Command-line argumentsstrongest: rarely used in containers
- Environment variablesfrom the Deployment, ConfigMaps and Secrets
- Imported filesmounted ConfigMap or Secret files via
spring.config.import - application.yaml in the jardefaults that are the same everywhere
4 layers, from the top down. Step through them one at a time.
Later and stronger sources override what the image ships with.
Notice that the file in the image holds only defaults that are safe to publish. The real values arrive at start-up and win because environment variables rank above files packaged in the jar.
3Supplying Values With ConfigMaps and Secrets
On Kubernetes, a ConfigMap is an object that holds plain configuration as key-value pairs, and a Secret is the same idea for sensitive values, kept apart so that access to it can be restricted and it can be encrypted at rest. Each environment has its own namespace or cluster with its own ConfigMap and Secret, while the Deployment points to the same image digest everywhere.
We can pass them to the container in two ways: as environment variables, or as files mounted into the container's file system. Environment variables suit a handful of simple values. Files suit larger configuration and secrets, because a file is not visible in the process listing and is not copied into crash reports that dump the environment.
The image line is the only part that must stay identical. Everything under envFrom, env and volumes is owned by the environment.
4Importing Mounted Files With spring.config.import
When configuration arrives as a mounted file, we tell Spring Boot where to find it. The property spring.config.import names extra locations to load, and the prefix optional: tells Boot not to fail if the file is missing, which keeps the same image runnable on a laptop.
For Kubernetes there is also a configtree: import. A config tree is a folder in which each file name is a property name and the file content is its value, which is exactly how Kubernetes mounts a Secret. Boot reads every file in the folder as one property.
spring.config.additional-location does a similar job from the outside: it adds locations to the ones Boot already searches, so we can set it as an environment variable in the Deployment. Values in those extra files override the defaults in the jar. The difference is mostly about who decides. spring.config.import is written into the application's own configuration, while additional-location is set by whoever deploys it.
5What Not to Bake Into the Image
The tempting shortcut is to put application-prod.yaml, with the production database password inside, into the jar and start the container with SPRING_PROFILES_ACTIVE=prod. It works, but it has three costs. Anyone who can pull the image can read the password, because image layers are just archives. Changing the password now needs a new build, which breaks build once. And the image now knows which environments exist, so adding a new one means changing code.
Profiles themselves are fine. A profile, a named group of settings that Boot activates on request, is a good way to switch behaviour, such as turning on a mock payment gateway in dev. What we avoid is profiles that carry environment-specific values and, above all, secrets. A good test is to ask whether the image could be published to a public registry without leaking anything. If the answer is no, something belongs outside it.
| Value | Example | Where it lives |
|---|---|---|
| Defaults shared by all | server.shutdown, pool size | application.yaml in the image |
| Environment endpoints | database URL, broker address | ConfigMap as env vars or file |
| Secrets | passwords, API keys | Secret mounted as files or a secrets manager |
| Behaviour switches | mock gateway in dev | a profile activated by an env var |
6Follow-up questions
Should we use environment variables or mounted files for secrets?
Both work, and many teams use environment variables because they are simple. Files are slightly safer: environment variables can leak through process listings, debug endpoints that print the environment, and child processes that inherit them. Mounted Secret files can also be updated without restarting the pod, although Spring Boot will only pick up the new value on restart unless we add a refresh mechanism. For high-value secrets, a secrets manager such as Vault or a cloud provider's service, read at start-up, adds rotation and auditing.
How do we keep the image runnable on a developer's laptop?
We give every property a safe local default in application.yaml and mark every import as optional:, so a missing mounted file is not an error. Developers can then run the same image with docker run and a few -e flags, or use a local profile that points at Docker Compose services. The production values never need to exist on the laptop.
What is the risk of using a different image tag per environment, such as orders:prod?
A mutable tag can be moved to point at a different image, so two pods started an hour apart may run different code. Deploying by digest, or by an immutable tag tied to the commit, guarantees that what staging approved is exactly what production runs.
7Conclusion
In this answer, we saw why a container image should be built once and promoted unchanged, and how Spring Boot gets the differences between environments from outside. Environment variables override packaged defaults through relaxed binding, ConfigMaps and Secrets supply those variables or mounted files, and spring.config.import or spring.config.additional-location load the files. The image keeps only safe defaults, and environment profiles with secrets stay out of it.
Open with
I build the image once in CI, tag it with the commit, and promote the same digest through dev, test and production, so what we tested is exactly what we run.
Then, if they want more
Everything that differs comes from the environment at start-up. Spring Boot already ranks environment variables above the application.yaml inside the jar, and relaxed binding means SPRING_DATASOURCE_URL simply becomes spring.datasource.url, so on Kubernetes I put plain settings in a ConfigMap and passwords in a Secret, and the Deployment injects them.
For larger configuration I mount the ConfigMap as a file and load it with spring.config.import, using optional:file: for a YAML file and optional:configtree: for a mounted Secret, where each file name is a property. If I do not want to change the application's own config, spring.config.additional-location set as an environment variable does the same from the outside.
What I avoid is an application-prod.yaml with real credentials baked into the jar: anyone who can pull the image can read it, rotating a password would need a rebuild, and the image would have to know every environment. The image should be safe to publish; its defaults work on a laptop, and the platform supplies the rest.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the best way to give the same container image different configuration in dev...": https://scrivio-interview-library.netlify.app/spring-boot/packaging-and-deployment/86ee28790e/