docker compose up --build Explained (With Examples)

Key Takeaways
- ✓--build builds images from their Dockerfiles before starting containers.
- ✓Plain up reuses existing images, so baked-in code changes don't appear.
- ✓Builds still use the cache; use docker compose build --no-cache for a clean build.
- ✓Recreated containers keep their volumes; data inside the container is lost.
Table of Contents
- •docker compose up --build Explained (With Examples)
- •What docker compose up Does
- •What --build Adds
- •When You Need --build (and When You Don’t)
- •An Example
- •Related Flags That Get Confused
- •Forcing a Completely Fresh Build
- •A Safe Deploy Sequence on a Server
- •Troubleshooting
- •Frequently Asked Questions
- •Skip the Build Server
docker compose up --build Explained (With Examples)
docker compose up --build builds the images for your services from their Dockerfiles, then creates and starts the containers. It’s the command you run after changing your code or Dockerfile, so the running containers use the new image instead of the one Compose built last time. This guide explains exactly what it does, when you need it, and the related flags (--no-cache, --force-recreate, -d) that people mix up.
Quick answer: use docker compose up --build -d after changing application code or a Dockerfile. Plain docker compose up reuses an existing image and won’t pick up your code changes unless the code is mounted as a volume.
What docker compose up Does
For each service in compose.yaml, docker compose up:
Creates the networks and volumes if they don’t exist.
Uses the service’s image. If the service has a
build:section and no image exists yet, Compose builds it once.Creates and starts the containers. If a service’s configuration or image has changed since the last run, Compose stops and recreates that container, keeping its mounted volumes.
The key detail is step 2: once an image exists, plain up doesn’t rebuild it, even if you changed the source code.
What --build Adds
Docker’s reference describes --build as “Build images before starting containers”. With it, Compose runs a build for every service that has a build: section, then starts the containers. Because the image is new, Compose recreates those containers so they run the new code.
docker compose up --build # build, then run in the foreground
docker compose up --build -d # build, then run in the background
docker compose up --build web # build and start only the "web" service (and its dependencies)Builds still use Docker’s layer cache, so only the steps after your change rerun. Copy package.json or requirements.txt and install dependencies before copying the rest of the code, and a code change won’t reinstall every dependency.
When You Need --build (and When You Don’t)
| You changed… | Command |
|---|---|
| Application code baked into the image | docker compose up --build -d |
| The Dockerfile or build arguments | docker compose up --build -d |
Only compose.yaml settings (ports, environment, volumes) | docker compose up -d (Compose recreates changed services) |
| Code mounted as a volume (development) | Nothing; restart the app or use its reload mode |
| A pre-built image tag from a registry | docker compose pull then docker compose up -d |
An Example
services:
web:
build: .
ports:
- "127.0.0.1:3000:3000"
env_file: .env
depends_on:
- db
db:
image: postgres:17
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:After editing your app, docker compose up --build -d rebuilds web and recreates its container. The db service uses a ready-made image, so it isn’t built, and its data stays in the dbdata volume.
Related Flags That Get Confused
| Flag | What it does |
|---|---|
--build | Build images before starting containers |
--no-build | Don’t build an image, even if one is missing |
--force-recreate | Recreate containers even if their configuration and image haven’t changed |
--no-deps | Don’t start linked services |
--pull always | Pull the latest base images before running |
-d, --detach | Run containers in the background |
--remove-orphans | Remove containers for services no longer in the Compose file |
--wait | Wait for services to be running or healthy (implies detached mode) |
Descriptions from Docker’s docker compose up reference, checked 26 October 2026.
Forcing a Completely Fresh Build
--build uses the build cache. If a cached step is stale (for example, a RUN apt-get update layer that never reruns), build without the cache, then start:
docker compose build --no-cache
docker compose up -dAdd --pull to the build to fetch newer base images too: docker compose build --no-cache --pull.
A Safe Deploy Sequence on a Server
git pull
docker compose build
docker compose up -d --remove-orphans
docker image prune -fBuilding first, then starting, keeps the old containers serving traffic while the new image builds. docker image prune clears old dangling images so the disk doesn’t fill up. To check what happened, read the logs with docker compose logs -f web; our Docker logs guide covers more.
Troubleshooting
Changes don’t appear: you ran
upwithout--build, or a cached layer copied old files. Check your.dockerignoreand rebuild with--no-cache.“docker: unknown command: docker compose”: the Compose plugin isn’t installed. See installing Docker on Ubuntu.
Builds are slow: order the Dockerfile so dependency installs come before copying source code, and exclude
node_modules,.gitand build output with.dockerignore.Database data disappeared: it wasn’t on a named volume. Recreating a container deletes anything stored only inside it.
Frequently Asked Questions
What is the difference between docker compose up and docker compose up --build?
Plain up reuses existing images; up --build builds images from their Dockerfiles first, so code changes baked into the image take effect.
Does docker compose up --build rebuild everything from scratch?
No. It uses Docker’s build cache, so only changed layers rebuild. Use docker compose build --no-cache for a clean build.
Does --build delete my data?
No. Recreated containers keep their mounted volumes. Data stored only inside a container’s filesystem is lost when it’s recreated, which is why databases need volumes.
Is docker-compose up --build the same command?
Yes, in the older standalone docker-compose (with a hyphen). Compose v2 is used as docker compose (with a space).
Should I use --build in production?
It works for single-server deploys. Many teams build images in CI, push them to a registry, and run docker compose pull and up -d on the server instead.
Skip the Build Server
On RackUp Cloud Apps every Git push builds and deploys your Dockerfile for you, with a private database, from $2.44 a month. Prefer your own Compose stack? The Developer VPS gives you root access, 8 vCPU and 12 GB RAM for $35 a month. See plans.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
DevOps & TutorialsDocker Port Mapping Explained: -p, EXPOSE and Compose ports
How Docker port mapping works: the -p syntax, EXPOSE vs publishing, Compose ports, service names on the network, and the UFW security detail most guides skip.
DevOps & TutorialsDocker Engine vs Docker Desktop: Which One Do You Need?
Docker Engine is the open-source runtime for Linux; Docker Desktop is a desktop app with a VM, GUI and its own licence. Which to use on servers and laptops.
DevOps & TutorialsERR_CONNECTION_REFUSED: What It Means and How to Fix It
ERR_CONNECTION_REFUSED means your browser reached the address and the connection was rejected. Fixes for visitors, for localhost and for server owners.