如何使用 docker-compose 使用特定错误代码退出 docker 容器?
How to make a docker container exit with a specific error-code with docker-compose?
我可以用 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
到 return 的结果可以在我的 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"]
这样的入口点时,它不会在 shell 中执行。 Docker 正在您的 $PATH
中寻找名为 exit
的命令,当然不存在这样的命令。
您需要 运行 您的命令在 shell 中,因为 exit
是一个 shell 命令:
entrypoint: ["/bin/sh", "-c", "exit 42"]
我可以用 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
到 return 的结果可以在我的 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"]
这样的入口点时,它不会在 shell 中执行。 Docker 正在您的 $PATH
中寻找名为 exit
的命令,当然不存在这样的命令。
您需要 运行 您的命令在 shell 中,因为 exit
是一个 shell 命令:
entrypoint: ["/bin/sh", "-c", "exit 42"]