Rebuild and Restart Single Docker Containers with Docker Compose

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:

  1. Rebuild a single Docker container.
  2. Restart the specific container without affecting others.
  3. 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.

Here’s the command to rebuild the web service:

ShellScript
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:

ShellScript
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:

YAML
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:

  1. Rebuild the web Service:
ShellScript
   docker compose up --no-deps --build web
  1. Restart the web Service:
ShellScript
   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.

Share this:

Leave a Reply