While working on a complex project with multiple services defined in a docker-compose.yml
file, you might often find yourself needing to rebuild and restart just one container without disrupting the others. Docker Compose makes this task straightforward, allowing you to keep your workflow smooth and uninterrupted.
Thank me by sharing on Twitter 🙏
In this post, I’ll share a step-by-step guide on how to rebuild and restart a single Docker container using Docker Compose. This technique will save you time and ensure your development process remains efficient and productive.
Introduction
Docker Compose is a powerful tool for defining and managing multi-container Docker applications. However, when changes are made to a single service, rebuilding and restarting all containers can be unnecessary and time-consuming. Fortunately, Docker Compose provides options to target specific services for rebuilding and restarting.
This guide will cover how to:
- Rebuild a single Docker container.
- Restart the specific container without affecting others.
- Use Docker Compose options to streamline this process.
Rebuild a Single Docker Container
First, let’s discuss how to rebuild a single Docker container. Suppose you have a service named web
that requires rebuilding after some code changes. Docker Compose provides the --build
option to rebuild services.
Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems
$40.63 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)The Game Boy Encyclopedia: Every Game Released for the Nintendo Game Boy and Game Boy Color
$27.40 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Nexus: A Brief History of Information Networks from the Stone Age to AI
$21.71 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Here’s the command to rebuild the web
service:
docker compose up --no-deps --build web
In this command:
--no-deps
: Ensures that Docker Compose doesn’t start any linked services.--build
: Rebuilds the specified service, in this case,web
.
Restart the Specific Container Without Affecting Others
After rebuilding the container, it’s crucial to restart only the specific service to see the changes in action. Docker Compose allows you to do this efficiently, ensuring that other services remain running.
Use the following command to restart the web
service:
docker compose up --no-deps web
This command:
- Restarts only the
web
service without stopping and starting other services. - Ensures minimal disruption to your development environment.
Practical Example
To illustrate, let’s say you’ve made changes to your TypeScript code within the web
service. After updating your Dockerfile or any related configurations, you need to rebuild and restart the web
container.
Here’s a sample docker-compose.yml
file:
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
After making changes to your web
service code, follow these steps:
- Rebuild the
web
Service:
docker compose up --no-deps --build web
- Restart the
web
Service:
docker compose up --no-deps web
These commands ensure that only the web
service is rebuilt and restarted, leaving the db
service running unaffected.
Conclusion
By using Docker Compose’s --no-deps
and --build
options, you can efficiently rebuild and restart individual containers without disrupting your entire multi-service setup. This approach saves time, maintains productivity, and ensures a smoother development workflow.
Whether you’re working on a complex application or a simple project, mastering these Docker Compose commands will enhance your development process, allowing you to focus more on coding and less on container management. Embrace this technique to keep your projects running seamlessly and efficiently.