1. Overview

In this tutorial, we’ll see how to rebuild a container independently from the others with docker-compose.

2. Presentation of the Problem

Let’s define a docker-compose.yml configuration file with two containers: One will refer to the latest ubuntu image and the other one to the latest alpine image. We’ll add pseudo-terminals for each with “tty: true” to prevent the containers from exiting directly on launch:

version: "3.9"
services:
  ubuntu:
    image: "ubuntu:latest"
    tty: true
  alpine:
    image: "alpine:latest"
    tty: true

Let’s now build the containers and start them. We’ll use the docker-compose up command with the -d option to let them run in the background:

$ docker-compose up -d

Container {folder-name}-alpine-1  Creating
Container {folder-name}-ubuntu-1  Creating
Container {folder-name}-ubuntu-1  Created
Container {folder-name}-alpine-1  Created
Container {folder-name}-ubuntu-1  Starting
Container {folder-name}-alpine-1  Starting
Container {folder-name}-alpine-1  Started
Container {folder-name}-ubuntu-1  Started

We can quickly check that our containers are running as expected:

$ docker-compose ps
NAME                         COMMAND             SERVICE             STATUS              PORTS
{folder-name}-alpine-1   "/bin/sh"           alpine              running
{folder-name}-ubuntu-1   "bash"              ubuntu              running

We’ll now see how we can rebuild and restart the ubuntu container without impacting the alpine container.

3. Rebuild and Restart a Container Independently

Adding the name of the container to the docker-compose up command will do the trick. We’ll add the build option to build the image before starting the container. We’ll also add the force-recreate flag because we haven’t changed the image:

$ docker-compose up -d --force-recreate --build ubuntu
Container {folder-name}-ubuntu-1  Recreate
Container {folder-name}-ubuntu-1  Recreated
Container {folder-name}-ubuntu-1  Starting
Container {folder-name}-ubuntu-1  Started

As we can see, the ubuntu container was rebuilt and restarted without any impact on the alpine container.

4. If the Container Depends on Another Container

Let’s now slightly update our docker-compose.yml file to make the ubuntu container depend on the alpine one:

version: "3.9"
services:
  ubuntu:
    image: "ubuntu:latest"
    tty: true
    depends_on:
      - "alpine"
  alpine:
    image: "alpine:latest"
    tty: true

We’ll stop the previous containers and rebuild them from scratch with the new configuration:

$ docker-compose stop
Container {folder-name}-alpine-1  Stopping
Container {folder-name}-ubuntu-1  Stopping
Container {folder-name}-ubuntu-1  Stopped
Container {folder-name}-alpine-1  Stopped

$ docker-compose up -d
Container {folder-name}-alpine-1  Created
Container {folder-name}-ubuntu-1  Recreate
Container {folder-name}-ubuntu-1  Recreated
Container {folder-name}-alpine-1  Starting
Container {folder-name}-alpine-1  Started
Container {folder-name}-ubuntu-1  Starting
Container {folder-name}-ubuntu-1  Started

In this case, we need to add the no-deps option to explicitly tell docker-compose not to restart linked containers:

$ docker-compose up -d --force-recreate --build --no-deps ubuntu
Container {folder-name}-ubuntu-1  Recreate
Container {folder-name}-ubuntu-1  Recreated
Container {folder-name}-ubuntu-1  Starting
Container {folder-name}-ubuntu-1  Started

5. Conclusion

In this tutorial, we’ve seen how to rebuild a container with docker-compose.

As always, the code is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.