docker - Docker-compose 退出代码应为非零时似乎为零
问题描述
我有两个 Docker 容器:
- b-db - 包含我的数据库
- b-combined - 包含我的 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 启动我的容器并使用以下命令开始运行测试。我正在使用此处、此处和此处--exit-code-from
的 SO 帖子中的概述。
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-combined 似乎按预期退出,并带有一个非零错误代码,该代码显示在控制台中,如下所示。这会触发两个容器关闭,这也是预期的行为。
b-combined 以代码 2 退出
停止b组合...
停止 b-db ...
停止 b-db ...完成 容器退出时中止...
为什么 Jenkins 仍然显示测试已通过(见下面的屏幕截图)?docker-compose up --build --exit-code-from combined
詹金斯不应该在命令的非零退出后失败吗?
此外,当我在本地(不在 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
下面是运行上述命令后 Jenkins 的截图:
为了帮助调试,下面combined
是docker-compose.yml
.
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", "+x", "/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(pre v1.23.0)。正如您在 docker-compose 的发行说明--exit-code-from
中看到的那样,自 v1.23.0 以来已经修复了几个错误。
解决方案
如评论中所述,我无法使用简单的撰写文件重现您的问题。如果以下示例仍然为您提供退出代码 0,则问题可能与您安装的docker-compose
. 如果它有效,那么问题将在于您的容器实际上没有使用正确的退出代码退出。您还应该运行 adocker 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
推荐阅读
- javascript - Alexa跳过插槽或以编程方式设置它?
- c# - 将 Visual Studio 2017 与 .Net Core SDK 3.0 一起使用
- php - php中的shell_exec在断开连接后仍然工作吗?
- python - 表格可视化未出现在 Tensorflow-PyCharm IDE 中
- awk - 在匹配模式之前打印行
- r - 如何使用 shinyFileChoose 创建响应式对象以加载 data.frame
- php - 在 MySQL 中将 latin1 转换为 utf8md4 的问题
- python - Numpy中的小数
- python - 在 Pyomo 中为约束提供上限和下限的问题
- c - C 库的公共头文件是否应该#include 其他带有双引号或尖括号的头文件?