Docker-compose exit code appears to be zero to Jenkins when it should be non-zero

I have two Docker containers:

  1. b-db – contains my database
  2. b-combined – contains my web application and tests that run once the container is up and running.

I’m using a docker-compose.yml file to start both containers.

version: '3'
services:
    db:
        build:
            context: .
            dockerfile: ./docker/db/Dockerfile
        container_name: b-db
        restart: unless-stopped
        volumes:     
            - dbdata:/data/db
        ports:
            - "27017:27017"
        networks:
            - app-network

    combined:
        build:
            context: .
            dockerfile: ./docker/combined/Dockerfile
        container_name: b-combined
        restart: unless-stopped
        env_file: .env
        ports:
            - "5000:5000"
            - "8080:8080"
        networks:
            - app-network
        depends_on:
            - db

networks:
    app-network:
        driver: bridge

volumes:
    dbdata:
    node_modules:

I’m using Jenkins to launch my containers and start running tests using the following command. I’m using --exit-code-from as outlined by SO posts from here, here and here.

docker-compose up --build --exit-code-from combined

Below is what my Jenkinsfile looks like.

pipeline {
    agent any
    environment {
        CI = 'true'
    }
    stages {
        stage('Test') {
            steps {
                sh 'docker-compose up --build --exit-code-from combined'
            }
        }
    }
}

When my tests run it appears that b-combined exits as expected with a non-zero error code, which is displayed to the console as shown below. This triggers both containers to shutdown, which is also expected behaviour.

b-combined exited with code 2

Stopping b-combined …

Stopping b-db …

Stopping b-db … done Aborting on container exit…

Why is it that Jenkins still displays the tests has having passed (see below screenshot)? Shouldn’t Jenkins have failed following a non-zero exit of the docker-compose up --build --exit-code-from combined command?

enter image description here

Furthermore, when I run the following immediately after I run the above docker-compose command in my command line I get an error code of 0, which is not expected.

$ echo $?
0

Source: StackOverflow