Skip to content
Log In Sign Up info@muvcloud.com

The build Key

The short form points to a folder that contains a Dockerfile:

services:
  web:
    build: .

The long form sets each part:

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
      args:
        APP_ENV: production
      target: runtime
    image: registry.example.com/shop/web:1.4.0
KeyMeaning
contextFolder sent to the builder
dockerfilePath from the context
argsValues for ARG lines
targetStage of a multi-stage build

How to write the Dockerfile itself is covered in Dockerfile: how to write one.

Build Everything or One Service

docker compose build
docker compose build web
docker compose build web worker

Services that use only an image key and no build key are skipped; they are pulled, not built.

Useful Options

OptionWhat it does
--no-cacheIgnores cached layers
--pullFetches newer base images
--build-arg K=VOverrides one arg
-qHides build output

--pull matters for security updates. If your Dockerfile starts with FROM debian:13, Docker reuses the copy it already has; --pull checks the registry for a newer build of that tag first. --no-cache rebuilds every step, which helps when a step such as a package install has been cached for weeks.

build or up --build?

docker compose build web
docker compose up -d

docker compose up -d --build

The first pair builds, then recreates the containers whose image changed. The single line does both. Plain docker compose up -d builds only when it finds no image to use (with an image key it may pull one first), which is why code changes sometimes seem to have no effect: the old image is still there. More on the start command in docker compose up -d.

How the Result Is Named

Without an image key, Compose names the image after the project and the service, such as shop-web. With an image key, as in the long example above, the build gets that name and tag, so it can be pushed with docker compose push web and pulled on another server. Choosing tags is covered in docker tag.

Keep the Build Context Small

The context folder is what the builder can read. Without a .dockerignore file, a COPY . . line copies .git, local .env files, and dependency folders into the image. See .dockerignore.

The Build Cache

Each Dockerfile step is cached as a layer and reused while its inputs are unchanged. Copy dependency lists and install dependencies before you copy the rest of the code, so a code change does not repeat the install. The cache takes disk space over time:

docker system df
docker builder prune

Build arguments end up in the image's history, so never pass passwords or tokens as args. Compose can hand secrets to a build through a secrets entry under build instead.

Building on a Small Server

Installing packages and compiling code can take more memory than the running application. On a server with 1 or 2 GB of RAM, a build can be stopped by the OOM killer. Add a swap file, or build elsewhere: on your own computer or a build system, push to a private registry, and on the server run docker compose pull and docker compose up -d. Build for the server's CPU architecture when the two differ.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations