我们可以有一个没有 shell 的 docker 容器吗?

Can we have a docker container without a shell?

我们可以构建一个 docker 容器,但该容器中没有任何 shell 吗?是否可以创建没有 shell 的容器?

是的,你可以从头开始创建一个容器,它甚至不包含任何东西bash,它只会包含你在构建时复制的二进制文件,否则它将是空的。

FROM scratch
COPY hello /
CMD ["/hello"]

You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

While scratch appears in Docker’s repository on the hub, you can’t pull it, run it, or tag any image with the name scratch. Instead, you can refer to it in your Dockerfile. For example, to create a minimal container using scratch:

scratch-docker-image

使用这个作为基础图像,你可以创建你的自定义图像,例如你只节点运行时而不是更多,然后你尝试形成 scratch-node.

FROM node as builder

WORKDIR /app

COPY package.json package-lock.json index.js ./

RUN npm install --prod

FROM astefanutti/scratch-node

COPY --from=builder /app /

ENTRYPOINT ["node", "index.js"]