The build: key tells Compose how to construct an image for a service. The simplest form is just a path to a directory containing a Dockerfile, but a richer form lets you specify the context separately from the dockerfile, so you can keep the Dockerfile named differently per environment. To pass build-time variables, use build.args with a map of ARG names to values, and reference them in the Dockerfile as ARG NAME; remember that ARG values are only available during the build, not at runtime, which is exactly what you want for things like a NODE_VERSION baked into a base image. To build for multiple CPU architectures in one go, list platforms under build.platforms, which requires BuildKit and buildx. To use a specific stage of a multi-stage Dockerfile, set build.target to the stage name.
To rebuild images after changing a Dockerfile, run docker compose build or docker compose up --build. To rebuild only one service, pass the service name: docker compose build web. To force a fresh pull of base images before starting, run docker compose pull && docker compose up -d. To skip rebuilding and reuse the existing image, use --no-build, and to skip pulling entirely use --no-pull. To force-recreate containers without rebuilding the image, use docker compose up -d --force-recreate, optionally scoped to a single service. The docker compose watch command in Compose v2 provides live development by watching source paths and either syncing changed files into the running container or triggering a full rebuild, configured via develop: or x-develop: blocks on each service.
Resource controls live under the deploy.resources key in Compose, though they were originally designed for Swarm. Under limits, you can set cpus as a fractional number like '1.5' and memory with a unit like 512M; the kernel will kill the container if it exceeds the memory limit, which is how OOM kills manifest. Under reservations, you can set guaranteed minimums that act as soft limits. Setting realistic memory limits is the single most effective way to avoid mysterious OOM kills, which you can detect by checking the OOMKilled field in docker inspect. To scale a service to multiple instances, the local-development-friendly command is docker compose up -d --scale worker=3, while in Swarm-mode Compose you would declare replicas under deploy.replicas. To request a GPU, list capabilities: [gpu] under deploy.resources.reservations.devices, which requires the NVIDIA runtime to be installed on the host.