Docker-compose退出代码应该为非零时似乎为零

我有两个Docker容器:

  1. b-db -包含我的数据库
  2. b组合-包含我的Web应用程序和在容器启动并运行后运行的测试。

我正在使用docker-compose.yml文件启动两个容器。

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:

我正在使用Jenkins启动容器并使用以下命令开始运行测试。我正在使用hereherehere的SO帖子概述的--exit-code-from

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

下面是我的Jenkinsfile的样子。

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

运行我的测试时,b组合似乎按预期退出,并带有非零错误代码,该错误代码显示在控制台上,如下所示。这会触发两个容器都关闭,这也是预期的行为。

  

b组合退出,代码2

     

停止b组合...

     

停止b-db ...

     

停止b-db ...完成在容器出口中终止...

为什么Jenkins仍然显示测试已通过(请参见下面的屏幕截图)?在docker-compose up --build --exit-code-from combined命令非零退出后,詹金斯不应该失败吗?

Docker-compose退出代码应该为非零时似乎为零

此外,当我在本地命令行中(而不是在Jenkins中)运行上述docker-compose命令后立即运行以下命令时,我得到的错误代码为0,这表明问题不在于Jenkins但是使用docker-compose却无法识别出我正在使用非零退出代码退出init.sh

$ echo $?
0

根据@LinPy的以下建议,我在计算机上和Jenkins中本地运行了以下命令。

docker-compose up -d --build db && docker-compose up --build combined || exit 2; echo $?

我收到的输出如下。最后一行是echo $?的输出,它显示脚本仍然以错误代码0退出。

b-combined | Mongoose disconnected
b-combined | TEST ENDED WITH EXIT CODE OF: 2
b-combined | EXITING SCRIPT WITH EXIT CODE OF: 2
b-combined exited with code 2
0

下面是运行上述命令后的詹金斯截图:

Docker-compose退出代码应该为非零时似乎为零

为帮助调试,以下是combineddocker-compose.yml服务的Dockerfile。

RUN npm install

COPY . .

EXPOSE 5000

RUN npm install -g history-server nodemon

RUN npm run build-test

EXPOSE 8080

COPY ./docker/combined/init.sh /scripts/init.sh

RUN ["chmod","+x","/scripts/init.sh"]

ENTRYPOINT [ "/scripts/init.sh" ]

下面是我的init.sh文件中的内容。

#!/bin/bash
# Start front end server
history-server dist -p 8080 &
front_pid=$!

# Start back end server that interacts with DB
nodemon -L server &
back_pid=$!

# Run tests
NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome

# Error code of the test
test_exit_code=$?

echo "TEST ENDED WITH EXIT CODE OF: $test_exit_code"

# End front and backend server
kill -9 $front_pid
kill -9 $back_pid

# Exit with the error code of the test
echo "EXITING SCRIPT WITH EXIT CODE OF: $test_exit_code"
exit "$test_exit_code"

以下是我的db服务的Dockerfile。它所做的就是将一些本地数据复制到Docker容器中,然后使用此数据初始化数据库。

FROM  mongo:3.6.14-xenial

COPY ./dump/ /tmp/dump/

COPY mongo_restore.sh /docker-entrypoint-initdb.d/

RUN chmod 777 /docker-entrypoint-initdb.d/mongo_restore.sh

下面是mongo_restore.sh中的内容。

#!/bin/bash
# Creates db using copied data
mongorestore /tmp/dump

从@LinPy更新解决方案之后,我尝试了以下步骤。

下面是我的 combined Dockerfile的外观:

RUN npm install

COPY . .

EXPOSE 5000

RUN npm install -g history-server nodemon

RUN npm run build-test

EXPOSE 8080

COPY ./docker/combined/init.sh /scripts/init.sh

RUN ["chmod","/scripts/init.sh"]

ENTRYPOINT [ "/scripts/init.sh" ]

# NEW LINE ADDED HERE
CMD ["sh","-c","exit $(cat /scripts/exit_code)"]

下面是我的 init.sh文件的外观。

#!/bin/bash
# Start front end server
history-server dist -p 8080 &
front_pid=$!

# Start back end server that interacts with DB
nodemon -L server &
back_pid=$!

# Run tests
NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome

# Error code of the test
test_exit_code=$?

echo "TEST ENDED WITH EXIT CODE OF: $test_exit_code"

# End front and backend server
kill -9 $front_pid
kill -9 $back_pid

# NEW LInes ADDED HERE
echo "$test_exit_code" > /scripts/exit_code
exec "$@"

# Exit with the error code of the test
echo "EXITING SCRIPT WITH EXIT CODE OF: $test_exit_code"
exit "$test_exit_code"

最后,我运行了以下命令:

docker-compose up -d --build db && docker-compose up --build combined || exit 2; echo $?

输出如下-最后一行(来自echo $?的输出)的退出代码为0。

b-combined | TEST ENDED WITH EXIT CODE OF: 2 ===========================
b-combined exited with code 2
0

解决方案:

我使用的是较早版本的docker-compose(低于v1.23.0)。正如您在docker-compose的release notes中所看到的那样,自v1.23.0起,--exit-code-from周围有一些错误修复。

qjf6835466qq 回答:Docker-compose退出代码应该为非零时似乎为零

我认为您的命令应该是:

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

通过这种方式,您将从combined获得退出代码

因为combined服务依赖于db,而该服务也将启动db服务。

我认为退出代码始终为0,因为--exit-code-from暗示着--abort-on-container-exit,而db将在退出时返回0

--abort-on-container-exit  Stops all containers if any container was
                           stopped. Incompatible with -d. ...
--exit-code-from SERVICE   Return the exit code of the selected service
                           container. Implies --abort-on-container-exit.

更新

尝试将其添加到脚本中,以代替最后一行:

#!/bin/bash
# Start front end server
history-server dist -p 8080 &
front_pid=$!

# Start back end server that interacts with DB
nodemon -L server &
back_pid=$!

# Run tests
NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome

# Error code of the test
test_exit_code=$?

echo "TEST ENDED WITH EXIT CODE OF: $test_exit_code"

# End front and backend server
kill -9 $front_pid
kill -9 $back_pid

# Exit with the error code of the test
echo "EXITING SCRIPT WITH EXIT CODE OF: $test_exit_code"
echo "$test_exit_code" > /scripts/exit_code
exec "$@"

然后在您的Dockerfile中添加一个CMD

CMD ["sh","-c","exit $(cat /scripts/exit_code)"]

运行

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

如评论中所述,我无法通过简单的撰写文件来重现您的问题。如果下面的示例仍然为您提供退出代码0,则安装docker-compose可能会出现问题。并且,如果工作正常,那么问题将出在您的容器实际上没有以正确的退出代码退出。您还应该运行docker container ls -a来查看退出的容器及其退出代码,并在已停止的容器上运行docker logs来验证输出。这是我的工作示例:

$ cat docker-compose.exit-code.yml
version: '3'

services:
  good:
    image: busybox
    command: /bin/sh -c "exit 0"

  bad:
    image: busybox
    command: /bin/sh -c "exit 42"

$ docker-compose -f docker-compose.exit-code.yml up --exit-code-from bad
Starting test_good_1_69c61ee0bdc6 ... done
Starting test_bad_1_fbe3194c1994  ... done
Attaching to test_bad_1_fbe3194c1994,test_good_1_69c61ee0bdc6
test_bad_1_fbe3194c1994 exited with code 42
Aborting on container exit...

$ echo $?
42

$ docker-compose -f docker-compose.exit-code.yml up --exit-code-from good
Starting test_good_1_69c61ee0bdc6 ... done
Starting test_bad_1_fbe3194c1994  ... done
Attaching to test_good_1_69c61ee0bdc6,test_bad_1_fbe3194c1994
test_good_1_69c61ee0bdc6 exited with code 0
Aborting on container exit...

$ echo $?
0
本文链接:https://www.f2er.com/3119906.html

大家都在问