Production container stack
You build the production container stack for the Go API and worker, with distroless images and Compose services for MySQL, Redis, Redpanda, and Caddy. Both application services wait for MySQL's healthcheck, and Compose receives credentials through environment interpolation.
Ticket
Build the production container stack for the Go API and worker. Package each binary in its own multi-stage Dockerfile, then define the MySQL, Redis, Redpanda, API, worker, and Caddy services in compose.prod.yml.
The deployment needs small application images that do not contain build tools or credentials. MySQL and Redpanda must retain their data when containers are recreated, and the API and worker must wait for MySQL to become healthy. The next module uses this stack to deploy the application on a server behind HTTPS.
Acceptance criteria
backend/Dockerfile.apiandbackend/Dockerfile.workereach have at least twoFROMinstructions. The first uses an image whose name starts withgolang:, and the final stage uses an image whose name starts withgcr.io/distroless/static.- Each Dockerfile has a
RUNinstruction containingCGO_ENABLED=0,go build, and the correct package path:./cmd/apifor the API and./cmd/workerfor the worker. - Each runtime stage has an explicit
USERinstruction whose value is notroot,0,0:0, orroot:root. - Each runtime stage has a
COPY --from=instruction and an exec-formENTRYPOINTbeginning with[. Runtime stages contain noRUNinstructions. - In both Dockerfiles,
RUN go mod downloadappears beforeCOPY . ., so application source changes do not invalidate the dependency-download layer. - Neither Dockerfile declares
ENVorARGvalues containingPASSWORD,SECRET,TOKEN,API_KEY, orDSN, regardless of case. backend/.dockerignoreexcludes.envand.git, either directly or with trailing slashes.backend/.env.exampledefinesMYSQL_ROOT_PASSWORD,JWT_SECRET, andAPI_DOMAIN, and every variable referenced through${VAR}inbackend/compose.prod.yml. The root.gitignoreignores.envusing.env,/.env,backend/.env, or**/.env, and does not contain a wildcard such as.env*that would also ignore.env.example.backend/compose.prod.ymldefines all six services:mysql,redis,redpanda,api,worker, andcaddy. Their images start withmysql:8.4,redis:7,redpandadata/redpanda, andcaddy:2for the infrastructure services. The API and worker images start withghcr.io/yashdagar/zomato-api:andghcr.io/yashdagar/zomato-worker:respectively. Both application services have build contexts of.or./and useDockerfile.apiorDockerfile.worker.- All six services declare a restart policy of
unless-stoppedoralways. - MySQL uses a named volume declared at the top level of the compose file and mounted at
/var/lib/mysql. Redpanda uses a top-level declared named volume mounted at/var/lib/redpanda/data. - MySQL has a non-empty
healthcheck.testlist. Bothapiandworkerdepend onmysqlwithcondition: service_healthy. - Every environment key containing
PASSWORDorSECRETin any service has a value containing${.
Constraints
- Do not modify the provided tests.
- Keep
.envand.gitout of the Docker build context. - Do not put credentials in Dockerfile
ARGorENVdeclarations.
Design direction
Keep backend/Dockerfile.api and backend/Dockerfile.worker separate. Give them the same build-stage structure, but compile different packages and produce different executables. Copy go.mod and go.sum before the application source, then download dependencies. This makes the dependency layer reusable when you change Go code.
The Docker build context for both application services should be the backend directory. Remember that COPY paths are relative to the build context, not the repository root. Keep .dockerignore beside the Dockerfiles so it controls which files enter that context.
Use backend/compose.prod.yml to describe the production services and their connections. Consider the difference between a restart policy and dependency readiness: restarting a failed container does not mean MySQL is ready. A MySQL healthcheck paired with service_healthy handles startup ordering. Named volumes preserve state independently of container lifetimes.
Use .env.example as a tracked template, not as a secrets store. Compose can interpolate values from the local .env file, while the real file stays untracked. Keep the image and build configuration for each application service so the same compose file can build local images and identify the images used for deployment. Any layout that meets the contract passes.
How to check your work
Run these commands from the repository root:
set -e
cd backend
make test W=7B1
./scripts/check-w7-b1.sh
cp .env.example .env
docker compose -f compose.prod.yml config -qThe test command checks the deployment contract. The Docker helper builds both images and checks that their containers run as non-root users, contain no shell, and produce images smaller than 60 MB. The final command validates Compose interpolation and configuration syntax using the example environment file.
Read the first failing assertion carefully. It identifies the file and the part of the deployment contract that needs attention. Fix that failure before investigating later errors.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- API Dockerfile builds the API binary in a separate build stage.
- Worker Dockerfile builds the worker binary in a separate build stage.
- Runtime stages use distroless images and non-root users.
.dockerignoreand.env.exampleprotect the build context and credentials.- Compose defines all six services, persistent volumes, and MySQL readiness.
- All W7B1 tests and Docker verification commands pass.
Commands
cd backend && make test W=7B1cd backend && ./scripts/check-w7-b1.shcd backend && cp .env.example .env && docker compose -f compose.prod.yml config -qTests
TestW7B1_DockerfilesAreMultiStageWithGoBuilderAndDistrolessRuntimeinbackend/internal/deploy/w7b1_test.goTestW7B1_BinariesAreBuiltStaticallyFromTheRightPackageinbackend/internal/deploy/w7b1_test.goTestW7B1_RuntimeStageRunsAsNonRootinbackend/internal/deploy/w7b1_test.goTestW7B1_RuntimeStageCopiesTheBinaryAndRunsNoShellCommandsinbackend/internal/deploy/w7b1_test.goTestW7B1_ModuleDownloadIsCachedBeforeTheSourceIsCopiedinbackend/internal/deploy/w7b1_test.goTestW7B1_NoSecretsAreBakedIntoTheImagesinbackend/internal/deploy/w7b1_test.goTestW7B1_DockerignoreKeepsSecretsAndHistoryOutOfTheBuildContextinbackend/internal/deploy/w7b1_test.goTestW7B1_EnvExampleListsEveryVariableComposeNeedsAndRealEnvIsIgnoredinbackend/internal/deploy/w7b1_test.goTestW7B1_ComposeDefinesEveryProductionServiceinbackend/internal/deploy/w7b1_test.goTestW7B1_ServicesRestartUnlessStoppedinbackend/internal/deploy/w7b1_test.goTestW7B1_StatefulServicesUseNamedVolumesinbackend/internal/deploy/w7b1_test.goTestW7B1_ApiAndWorkerWaitForAHealthyMysqlinbackend/internal/deploy/w7b1_test.goTestW7B1_PasswordsAndSecretsAreInterpolatedNeverLiteralinbackend/internal/deploy/w7b1_test.go