如何用docker-compose让一个带有特定错误代码的docker容器退出? [英] How to make a docker container exit with a specific error-code with docker-compose?

查看:0
本文介绍了如何用docker-compose让一个带有特定错误代码的docker容器退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用docker-compose启动一个Docker容器,并用这个例子检查它的退出代码:

# Dockerfile
FROM alpine:3.15 as base
# docker-compose.yml
version: '3.6'
services:
  dummy:
    build:
      context: .
    entrypoint: ["sleep", "42"]
    image: "tmp:tmp"
$ docker-compose up --force-recreate
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Recreating docker-compose_dummy_1 ... done
Attaching to docker-compose_dummy_1
docker-compose_dummy_1 exited with code 0
$ docker inspect docker-compose_dummy_1 --format='{{.State.ExitCode}}' # 42 seconds later
0

是否可以使用特定错误代码退出此容器?
我希望docker inspect的结果返回可在docker-compose.yml中指定的非零值。

我天真地认为将入口点从sleep更改为exit应该有效:

# docker-compose.yml
version: '3.6'
services:
  dummy:
    build:
      context: .
    entrypoint: ["exit", "42"]
    image: "tmp:tmp"

...但它没有:

$ docker-compose up --force-recreate
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Recreating docker-compose_dummy_1 ... error

ERROR: for docker-compose_dummy_1  Cannot start service dummy: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "exit": executable file not found in $PATH": unknown

ERROR: for dummy  Cannot start service dummy: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "exit": executable file not found in $PATH": unknown
ERROR: Encountered errors while bringing up the project.

推荐答案

更仔细地查看错误消息:

ERROR: for docker-compose_dummy_1  Cannot start service dummy: OCI
runtime create failed: container_linux.go:345: starting container
process caused "exec: "exit": executable file not found in $PATH":
unknown

当您指定像["exit", "42"]这样的入口点时,它不会在外壳中执行。Docker正在您的$PATH中查找名为exit的命令,当然不存在此类命令。

您需要在外壳中运行命令,因为exit是外壳命令:

    entrypoint: ["/bin/sh", "-c", "exit 42"]

这篇关于如何用docker-compose让一个带有特定错误代码的docker容器退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆