Docker 从头开始​​CMD错误

Docker from scratch CMD bug

我有以下 Dockerfile

FROM golang as builder
ARG CADDY_HASH=4b4e99bdb2e327d553a5f773f827f624181714af
WORKDIR /root/caddy
RUN wget -qO- github.com/caddyserver/caddy/archive/"$CADDY_HASH".tar.gz | tar zx --strip-components=1
RUN set -e; cd cmd/caddy && CGO_ENABLED=0 go build

FROM scratch

COPY --from=builder /root/caddy/cmd/caddy/caddy /

ARG PORT=8000
ENV PORT $PORT
EXPOSE $PORT

CMD /caddy file-server --browse --listen :$PORT

我使用此命令

构建并运行

DOCKER_BUILDKIT=0 docker build -t caddy-static-docker:latest . && docker run -e PORT=8000 -p 8000:8000 caddy-static-docker:latest

为什么这不起作用并且我收到此错误消息?

docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown.

使用 alpine 图像,这是 运行 shell 命令的轻量级图像,其中加载了 /bin/sh

SCRATCH 图像基本上是空的,/ 文件夹中没有任何内容,因此没有可执行文件来执行作为 CMD 的一部分给出的任何内容。

使用入口点而不是 CMD

ENTRYPOINT ["/caddy"]
CMD ["file-server", "--browse", "--listen", "8080"]

还要注意 json 语法(exec 形式),这导致子 shell 中的内容不是 运行。

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

来源:https://docs.docker.com/engine/reference/builder/#cmd

The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals. This means that the executable will not be the container’s PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop .

来源:https://docs.docker.com/engine/reference/builder/#entrypoint

您的 PORT 仍然会导致问题。考虑对其进行硬编码。