Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!