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.api and backend/Dockerfile.worker each have at least two FROM instructions. The first uses an image whose name starts with golang:, and the final stage uses an image whose name starts with gcr.io/distroless/static.
  • Each Dockerfile has a RUN instruction containing CGO_ENABLED=0, go build, and the correct package path: ./cmd/api for the API and ./cmd/worker for the worker.
  • Each runtime stage has an explicit USER instruction whose value is not root, 0, 0:0, or root:root.
  • Each runtime stage has a COPY --from= instruction and an exec-form ENTRYPOINT beginning with [. Runtime stages contain no RUN instructions.
  • In both Dockerfiles, RUN go mod download appears before COPY . ., so application source changes do not invalidate the dependency-download layer.
  • Neither Dockerfile declares ENV or ARG values containing PASSWORD, SECRET, TOKEN, API_KEY, or DSN, regardless of case.
  • backend/.dockerignore excludes .env and .git, either directly or with trailing slashes.
  • backend/.env.example defines MYSQL_ROOT_PASSWORD, JWT_SECRET, and API_DOMAIN, and every variable referenced through ${VAR} in backend/compose.prod.yml. The root .gitignore ignores .env using .env, /.env, backend/.env, or **/.env, and does not contain a wildcard such as .env* that would also ignore .env.example.
  • backend/compose.prod.yml defines all six services: mysql, redis, redpanda, api, worker, and caddy. Their images start with mysql:8.4, redis:7, redpandadata/redpanda, and caddy:2 for the infrastructure services. The API and worker images start with ghcr.io/yashdagar/zomato-api: and ghcr.io/yashdagar/zomato-worker: respectively. Both application services have build contexts of . or ./ and use Dockerfile.api or Dockerfile.worker.
  • All six services declare a restart policy of unless-stopped or always.
  • 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.test list. Both api and worker depend on mysql with condition: service_healthy.
  • Every environment key containing PASSWORD or SECRET in any service has a value containing ${.

Constraints

  • Do not modify the provided tests.
  • Keep .env and .git out of the Docker build context.
  • Do not put credentials in Dockerfile ARG or ENV declarations.

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:

bash
set -e
cd backend
make test W=7B1
./scripts/check-w7-b1.sh
cp .env.example .env
docker compose -f compose.prod.yml config -q

The 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.

Finished Exercise 7.2?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. API Dockerfile builds the API binary in a separate build stage.
  2. Worker Dockerfile builds the worker binary in a separate build stage.
  3. Runtime stages use distroless images and non-root users.
  4. .dockerignore and .env.example protect the build context and credentials.
  5. Compose defines all six services, persistent volumes, and MySQL readiness.
  6. All W7B1 tests and Docker verification commands pass.

Commands

terminal
cd backend && make test W=7B1
terminal
cd backend && ./scripts/check-w7-b1.sh
terminal
cd backend && cp .env.example .env && docker compose -f compose.prod.yml config -q

Tests

  • TestW7B1_DockerfilesAreMultiStageWithGoBuilderAndDistrolessRuntime in backend/internal/deploy/w7b1_test.go
  • TestW7B1_BinariesAreBuiltStaticallyFromTheRightPackage in backend/internal/deploy/w7b1_test.go
  • TestW7B1_RuntimeStageRunsAsNonRoot in backend/internal/deploy/w7b1_test.go
  • TestW7B1_RuntimeStageCopiesTheBinaryAndRunsNoShellCommands in backend/internal/deploy/w7b1_test.go
  • TestW7B1_ModuleDownloadIsCachedBeforeTheSourceIsCopied in backend/internal/deploy/w7b1_test.go
  • TestW7B1_NoSecretsAreBakedIntoTheImages in backend/internal/deploy/w7b1_test.go
  • TestW7B1_DockerignoreKeepsSecretsAndHistoryOutOfTheBuildContext in backend/internal/deploy/w7b1_test.go
  • TestW7B1_EnvExampleListsEveryVariableComposeNeedsAndRealEnvIsIgnored in backend/internal/deploy/w7b1_test.go
  • TestW7B1_ComposeDefinesEveryProductionService in backend/internal/deploy/w7b1_test.go
  • TestW7B1_ServicesRestartUnlessStopped in backend/internal/deploy/w7b1_test.go
  • TestW7B1_StatefulServicesUseNamedVolumes in backend/internal/deploy/w7b1_test.go
  • TestW7B1_ApiAndWorkerWaitForAHealthyMysql in backend/internal/deploy/w7b1_test.go
  • TestW7B1_PasswordsAndSecretsAreInterpolatedNeverLiteral in backend/internal/deploy/w7b1_test.go