0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker Compose Build: Simple Guide

Use docker compose build to build or rebuild Docker images defined in your docker-compose.yml file. This command reads the build context and Dockerfile settings for each service and creates the images locally.
📐

Syntax

The basic syntax of the docker compose build command is:

  • docker compose build [options] [SERVICE...]

Here:

  • docker compose build builds all services defined in the docker-compose.yml file.
  • [SERVICE...] is an optional list of specific services to build.
  • [options] are flags to control the build process, like --no-cache to avoid using cache.
bash
docker compose build [options] [SERVICE...]
💻

Example

This example shows how to build images for services defined in a docker-compose.yml file. It demonstrates building all services and then building a specific service.

yaml
version: '3.8'
services:
  web:
    build: ./web
    image: mywebapp:latest
  db:
    build: ./db
    image: mydb:latest

# Commands to run:
docker compose build

docker compose build web
Output
Building web Step 1/5 : FROM python:3.10-slim ---> abcdef123456 Step 2/5 : COPY . /app ---> Using cache Step 3/5 : RUN pip install -r requirements.txt ---> Using cache Step 4/5 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged mywebapp:latest Building db Step 1/3 : FROM postgres:14 ---> 123456abcdef Step 2/3 : COPY init.sql /docker-entrypoint-initdb.d/ ---> Using cache Step 3/3 : CMD ["postgres"] ---> Using cache Successfully built 123456abcdef Successfully tagged mydb:latest # When building only web service: Building web Step 1/5 : FROM python:3.10-slim ---> abcdef123456 Step 2/5 : COPY . /app ---> Using cache Step 3/5 : RUN pip install -r requirements.txt ---> Using cache Step 4/5 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged mywebapp:latest
⚠️

Common Pitfalls

Common mistakes when using docker compose build include:

  • Not specifying the correct build context path in docker-compose.yml, causing build failures.
  • Forgetting to rebuild images after changing Dockerfiles or source code, leading to outdated containers.
  • Using the latest tag without versioning, which can cause confusion about which image is running.
  • Not using --no-cache when you want a fresh build, so changes might not be applied.

Example of wrong and right usage:

bash
# Wrong: build without updating Dockerfile or context
$ docker compose build

# Right: force rebuild without cache
$ docker compose build --no-cache
📊

Quick Reference

Here is a quick cheat sheet for docker compose build options:

OptionDescription
--no-cacheBuild images without using cache
--pullAlways attempt to pull a newer version of the base image
--parallelBuild images in parallel (default behavior)
SERVICESpecify one or more services to build instead of all

Key Takeaways

Use docker compose build to build images defined in your compose file.
Specify service names to build only those images when needed.
Use --no-cache to force a fresh build without cached layers.
Always update your Dockerfile or context before building to see changes.
Avoid using ambiguous tags like latest for better image management.