docker
docker-compose
container
build
tutorial

Docker compose build a single service (one container only)

Codemia Knowledge Hub ·

To build only one container with Docker Compose, pass the service name to the build command, for example docker-compose build web. Compose then builds just that service's image from its build settings in docker-compose.yml and leaves the other services alone, which saves time when you have only changed code for one service.

Example docker-compose.yml with several services

Compose defines an application's services, networks and volumes in docker-compose.yml. The build key tells Compose to build an image before starting the container:

yaml
1version: '3.8'
2services:
3  web:
4    build:
5      context: ./web
6    ports:
7      - "5000:5000"
8
9  database:
10    image: postgres:latest
11    environment:
12      POSTGRES_USER: example
13      POSTGRES_PASSWORD: example

The web service is built from a Dockerfile in ./web, while database uses an existing image. docker-compose up builds any images that have a build section and pulls the rest as needed.

Building just one service

bash
docker-compose build web

This builds only the web service defined in docker-compose.yml. It is useful during development when you changed the source code of one service and only want to rebuild that container.

Step by step: rebuilding the web service

Say you changed the application code in the web directory.

  1. Go to your project directory:
bash
   cd path/to/project
  1. Run the build:
bash
   docker-compose build web

This command:

  • takes the files from the ./web directory,
  • reads the Dockerfile,
  • builds the image with your latest changes.
  1. Check the image:
    Confirm the image was built:
bash
   docker images

Look for the image tagged with the directory name or whatever tag you defined.

Build details that affect a single-service build

  • Build context: the directory Docker takes the files from to build the image, set as context in the build block. Docker looks for the Dockerfile inside this context unless told otherwise.
  • Layer cache: Docker checks whether an identical layer already exists and reuses it instead of rebuilding, which cuts build times dramatically.
  • Build-time variables: pass variables to the Dockerfile with ARG in the Dockerfile and args in docker-compose.yml.
  • Dependencies: depends_on controls startup order, but docker-compose up only waits for a dependency to start, not to be fully ready. Use readiness-check scripts if your services have startup dependencies.
ConceptDescription
Build ContextDirectory with the Dockerfile and necessary resources for building an image.
Cached LayersUses previous layers to expedite rebuilds if no changes are detected.
Single Container BuildUse docker-compose build <service> to target individual service builds.
Dependenciesdepends_on manages service startup order; use readiness checks if needed.
Environment VariablesUse ARG and args to inject build-time variables in Dockerfile and Compose.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design