--abort-on-container-exit is not working after exited the terminal

I use docker-compose up --build --abort-on-container-exit to start the application, --abort-on-container-exit works while I’m hanging and monitoring the log(do nothing after running the command), however, it failed after I exited the terminal, and check the logs using docker-compose logs, only the error container exit, other containers keep running.

Example:

My project structure:

  • first-test.js
  • second-test.js
  • docker-compose.yml
  • first-test.Dockerfile
  • second-test.Dockerfile
  • package.json

first-test.js:

async function foo1() {
  for (let i = 0; i < 1000; i++) {
    console.log(i);
    if (i === 20) {
      throw "Error";
    }
    await new Promise(resolve => setTimeout(resolve, 1000));
}
}

foo1();

second-test.js

async function foo2() {
  for (let i = 0; i < 1000; i++) {
    console.log(i);
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
}

foo2();

first-test.Dockerfile:

FROM node:21
WORKDIR /usr/src/app
COPY . ./
RUN npm install
CMD [ "node", "first-test.js" ]

second-test.Dockerfile:

FROM node:21
WORKDIR /usr/src/app
COPY . ./
RUN npm install
CMD [ "node", "second-test.js" ]

docker-compose.yml:

version: "3.8"
services:
  first-test:
    build:
      context: ./
      dockerfile: ./first-test.Dockerfile

  second-test:
    build:
      context: ./
      dockerfile: ./second-test.Dockerfile

Both containers aborted after first-test.js threw the error if I just started the application by docker-compose up --build --abort-on-container-exit, however, if I closed the terminal before the error was thrown and checked the container logs by docker-compose logs, I see the second-test.js keep running and first-test.js is not. Why second-test.js is running even I added abort-on-container-exit in the docker-compose --up command?