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:
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
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.
- Go to your project directory:
- Run the build:
This command:
- takes the files from the
./webdirectory, - reads the Dockerfile,
- builds the image with your latest changes.
- Check the image:Confirm the image was built:
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
contextin thebuildblock. 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
ARGin the Dockerfile andargsindocker-compose.yml. - Dependencies:
depends_oncontrols startup order, butdocker-compose uponly waits for a dependency to start, not to be fully ready. Use readiness-check scripts if your services have startup dependencies.
| Concept | Description |
| Build Context | Directory with the Dockerfile and necessary resources for building an image. |
| Cached Layers | Uses previous layers to expedite rebuilds if no changes are detected. |
| Single Container Build | Use docker-compose build <service> to target individual service builds. |
| Dependencies | depends_on manages service startup order; use readiness checks if needed. |
| Environment Variables | Use ARG and args to inject build-time variables in Dockerfile and Compose. |
Related reading
- Docker Compose Change env variables
- Docker (Compose) client connects to Kafka too early
- Docker compose create kafka topics
- Docker compose doesn't save data in volume
- Docker compose .env default with fallback
- Docker compose healthcheck use environment variables
- Docker Compose keep container running
- Docker Compose Mac Error Cannot start service zoo1 Mounts denied
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.