我可以使用以下示例啟動 Docker 容器docker-compose并檢查其退出代碼:
# 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
有沒有辦法讓這個容器退出并帶有特定的錯誤代碼?
我希望 my 的結果docker inspect回傳一個可在 my 中指定的非零值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.
uj5u.com熱心網友回復:
更仔細地查看您的錯誤訊息:
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 中執行。$PATHDocker 正在您的named中尋找命令exit,當然不存在這樣的命令。
您需要在 shell 中運行命令,因為exit是 shell 命令:
entrypoint: ["/bin/sh", "-c", "exit 42"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/434666.html
