【问题标题】:Getting exit code out of docker-compose up从 docker-compose up 中获取退出代码
【发布时间】:2018-04-18 12:34:57
【问题描述】:

我在从docker-compose up 中获取退出代码时遇到问题。
我有一个最简单的容器,它运行一个总是以 1 退出的脚本:

#!/usr/bin/env sh
exit 1

我的 Dockerfile:

FROM mhart/alpine-node:6
RUN mkdir /app
WORKDIR /app

还有我的 docker-compose.yml:

version: '2'

services:
  test_container:
    container_name: test_container
    build: .
    volumes:
      - ${PWD}/run.sh:/app/run.sh
    entrypoint: ["/app/run.sh"]

当我运行它时:

docker-compose -f docker-compose.yml up --force-recreate test_container

我可以在日志中看到:

Recreating test_container ... 
Recreating test_container ... done
Attaching to test_container
test_container exited with code 1

但是当我echo $? 时,我得到0
Docker 版本 17.09.0-ce,构建 afdb6d4。在 OSX 10.12.6 上运行。
难道我做错了什么?这是一个已知问题吗(我在那里找不到任何东西)?

【问题讨论】:

  • 我看不到返回1 的原因,因为docker-compose 已成功运行。
  • docker-compose run/exec 确实返回正在运行的容器的退出代码
  • 我相信这是因为您使用execrun 定义了一个特定的容器。但是使用docker compose up,您可能拥有一组容器。例如,如果您的案例中有另一个test_container_20 退出,那么您希望从echo $? 得到什么?
  • 为什么不尝试使用docker inspect 获取容器的退出代码?
  • 是的,我想我必须这样做

标签: docker docker-compose exit exit-code terminate


【解决方案1】:

一个选项 --exit-code-from SERVICE 可以与 docker-compose up 一起使用 :)

来自documentation

docker compose up

Options:
    --exit-code-from SERVICE   Return the exit code of the selected service container.
                               Implies --abort-on-container-exit.

    --abort-on-container-exit  Stops all containers if any container was stopped.
                               Incompatible with -d.

    -d                         Detached mode: Run containers in the background,
                               print new container names.
                               Incompatible with --abort-on-container-exit.

【讨论】:

  • 不错!我从文档中添加了一些信息,我认为应该提及...
  • "--exit-code-from SERVICE" 不太清楚,所以我很快测试了它。如果您有直接失败的服务,它将返回实际的退出代码,但如果没有,它会返回一个 SIGTERM (128)
猜你喜欢
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
相关资源
最近更新 更多