Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: February 6, 2024
In this tutorial, we’ll see how to rebuild a container independently from the others with docker-compose.
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.
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.
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
In this tutorial, we’ve seen how to rebuild a container with docker-compose.
As always, the code is available over on GitHub.