docker - 如何在 Dockerfile 中为基础镜像指定运行参数?

标签 docker dockerfile

我在 Dockerfile 中定义一个图像,它有另一个图像作为其父图像:

FROM parent_org/parent:1.0.0
...

父图像的文档提到了一个参数(special-arg),可以在运行容器实例时传递:

docker run parent_org/parent:1.0.0 --special-arg

如何在我的 Dockerfile 中启用 special-arg

最佳答案

TL;DR:您可以使用 CMD通过做这样的事情来指令:

FROM parent_org/parent:1.0.0
CMD ["--special-arg"]

但是请注意,将额外的标志传递给 docker run如下所示将覆盖 --special-arg (因为 CMD 旨在指定默认 参数):

docker build -t child_org/child .
docker run child_org/child  # would imply --special-arg

docker run child_org/child --other-arg  # "--other-arg" replaces "--special-arg"

如果这不是您想要的,您应该重新定义 ENTRYPOINT如下所示。

CMD 和 ENTRYPOINT 指令

更多地了解 CMD以及 ENTRYPOINT ,你可以看看table参与这个其他 SO 答案:CMD doesn't run after ENTRYPOINT in Dockerfile .

在您的情况下,您可以重新定义 ENTRYPOINT在你的child图像(如果需要,默认 CMD )通过调整 child_org/child/Dockerfile w.r.t. parent 中定义了什么Dockerfile.

Assuming the parent_org/parent/Dockerfile looks like this:

FROM debian:stable  # for example

WORKDIR /usr/src/foo
COPY entrypoint.sh .
RUN chmod a+x entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]
CMD ["--default-arg"]

You could write a child_org/child/Dockerfile like this:

FROM parent_org/parent:1.0.0
RUN […]

# Redefine the ENTRYPOINT so the --special-arg flag is always passed
ENTRYPOINT ["./entrypoint.sh", "--special-arg"]

# If need be, redefine the list of default arguments,
# as setting ENTRYPOINT resets CMD to an empty value:
CMD ["--default-arg"]

关于docker - 如何在 Dockerfile 中为基础镜像指定运行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634908/

相关文章:

linux - Docker 运行错误 : "Thin Pool has free data blocks which is less than minimum required"

docker - docker stdin 和 tty 标志如何在容器的封面下工作?

django - Docker:将新的 postgres 容器重新连接到现有的数据容器

docker - Docker容器即将退出

docker - docker-compose drupal不接受我的数据库名称,并在下面抛出错误

postgresql - ASP.Net Core Docker容器找不到Postgres数据库

docker - 调用 tcp 127.0.0.1 :8000: connect: connection refused golang docker containers

bash - 在构建期间为 Docker 添加别名

docker - 在 Docker 中更改目录命令?

node.js - 使用 Yarn 依赖项为 Node 应用程序构建 Docker 镜像