
Build Once, Deploy Everywhere: Why Config Doesn't Belong in Your Build
- كاتب موثّق
محتويات المقال
The common misconception
--configuration this "provides a mechanism for configuring environment-specific data in your application." To add staging, the same page has you copy environment.ts to environment.staging.ts and add a staging build configuration. If the official way to set an API URL is a build flag, a build per environment looks like the design.
What actually happens
Build, release and run are separate stages
A second build is a second artifact
RUN step can hold different packages on different machines. Even with every input pinned, a per-environment build differs on purpose, because the settings are part of it. In our run, the staging and production images got different image IDs, so the image you tested is not the image you ship. A gets its confidence from promoting the same artifact through each stage.
Config is what varies between deploys
Values baked into an image travel with it
docker history printed the production URL twice, once from ENV and once from the ARG itself.
The fix: one image, config at startup
server.ts (below) on Docker 29.4.3 with node:24.22.2-alpine3.24. The wrong one bakes the URL in with a build argument:
# Dockerfile: ARG PAYMENTS_URL, then ENV PAYMENTS_URL=$PAYMENTS_URLdocker build --build-arg PAYMENTS_URL=https://sandbox.payments.example \ -t shop-api:1.4.0-staging .docker build --build-arg PAYMENTS_URL=https://api.payments.example \ -t shop-api:1.4.0-prod .docker image ls --format '{{.Tag}} {{.ID}}' shop-api# 1.4.0-prod e6cf6bf4ae98# 1.4.0-staging 5911098ae226docker history --format '{{.CreatedBy}}' shop-api:1.4.0-prod | grep PAYMENTS# ENV PAYMENTS_URL=https://api.payments.example# ARG PAYMENTS_URL=https://api.payments.exampledocker build -t shop-api:1.4.0 .docker run -d --name shop-staging \ -e PAYMENTS_URL=https://sandbox.payments.example shop-api:1.4.0docker run -d --name shop-prod \ -e PAYMENTS_URL=https://api.payments.example shop-api:1.4.0docker logs shop-prod# shop-api 1.4.0, payments at https://api.payments.exampledocker inspect --format '{{.Name}} {{.Image}}' shop-staging shop-prod# /shop-staging sha256:c6339bd7a8c12e3a83c260e9b2357033791d8896232c273dab406b7eebc1c7f6# /shop-prod sha256:c6339bd7a8c12e3a83c260e9b2357033791d8896232c273dab406b7eebc1c7f6import { createServer } from "node:http";// Per-deploy config comes from the environment at startupconst paymentsUrl = process.env.PAYMENTS_URL;if (!paymentsUrl) { throw new Error("PAYMENTS_URL is not set");}createServer((_req, res) => { res.end("ok");}).listen(8080, () => { console.log(`shop-api 1.4.0, payments at ${paymentsUrl}`);});PAYMENTS_URL exits right away with Error: PAYMENTS_URL is not set, which is far better than failing on the first payment.
How to apply it in your project
List what varies between deploys Go through your build scripts, Dockerfiles and pipeline for per-environment values: URLs, hostnames, credentials, feature switches. Each one is config. Move it out of the build Replace build arguments, compile-time defines and file replacements with values read at startup, and fail fast when one is missing. Promote one artifact Build once per commit, tag it with the version or commit SHA, record its digest, and deploy that digest to staging and then production. The latest-tag article showed why the digest matters. Give secrets to the container, not the image Pass credentials at run time from your platform's secret store, never as build arguments, and check docker historyon your current images for anything that leaked.
Key takeaways
Build once; config joins at release, so one artifact can run in every environment. A per-environment build means production runs an image that was never tested. Config is whatever varies between deploys; read it at startup and fail fast when it's missing. Build arguments and ENVvalues stay in the image, so never use them for secrets.Promote the same digest from staging to production, and roll back by redeploying the previous one.
التعليقات (0)