重用继承图像的 CMD 或 ENTRYPOINT [英] Reuse inherited image's CMD or ENTRYPOINT

查看:25
本文介绍了重用继承图像的 CMD 或 ENTRYPOINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在容器启动/重启/附加时包含我自己的 shell 脚本 CMD,而不删除继承图像使用的 CMD?

How can I include my own shell script CMD on container start/restart/attach, without removing the CMD used by an inherited image?

我正在使用它,它确实可以很好地执行我的脚本,但似乎覆盖了 PHP CMD:

I am using this, which does execute my script fine, but appears to overwrite the PHP CMD:

FROM php

COPY start.sh /usr/local/bin

CMD ["/usr/local/bin/start.sh"]

我应该做些什么不同的事情?我正在避免复制/粘贴父图像的 ENTRYPOINT 或 CMD,这可能不是一个好方法.

What should I do differently? I am avoiding the prospect of copy/pasting the ENTRYPOINT or CMD of the parent image, and maybe that's not a good approach.

推荐答案

正如评论中提到的,没有内置的解决方案.从 Dockerfile 中,您看不到当前 CMDENTRYPOINT 的值.如果您控制上游基础映像并在其中包含此代码,那么拥有 run-parts 解决方案会很好,从而允许下游组件进行更改.但是 docker 有一个固有的问题会导致问题,容器应该只运行一个需要在前台运行的命令.因此,如果上游映像启动,它将继续运行,而不会让您的后续步骤有机会运行,因此您需要确定运行命令的顺序以确保单个命令最终运行而不会退出.

As mentioned in the comments, there's no built-in solution to this. From the Dockerfile, you can't see the value of the current CMD or ENTRYPOINT. Having a run-parts solution is nice if you control the upstream base image and include this code there, allowing downstream components to make their changes. But docker there's one inherent issue that will cause problems with this, containers should only run a single command that needs to run in the foreground. So if the upstream image kicks off, it would stay running without giving your later steps a chance to run, so you're left with complexities to determine the order to run commands to ensure that a single command does eventually run without exiting.

我个人的偏好是一个更简单且硬编码的选项,添加我自己的命令或入口点,并使我的命令的最后一步 exec 成为上游命令.您仍然需要手动识别要从上游 Dockerfile 调用的脚本名称.但现在在您的 start.sh 中,您将拥有:

My personal preference is a much simpler and hardcoded option, to add my own command or entrypoint, and make the last step of my command to exec the upstream command. You will still need to manually identify the script name to call from the upstream Dockerfile. But now in your start.sh, you would have:

#!/bin/sh

# run various pieces of initialization code here
# ...

# kick off the upstream command:
exec /upstream-entrypoint.sh "$@"

通过使用 exec 调用,您可以将 pid 1 传输到上游入口点,以便正确处理信号.尾随的 "$@" 传递任何命令行参数.如果您想在自己的 start.sh 脚本中处理和提取一些 args,您可以使用 set 来调整 $@ 的值.

By using an exec call, you transfer pid 1 to the upstream entrypoint so that signals get handled correctly. And the trailing "$@" passes through any command line arguments. You can use set to adjust the value of $@ if there are some args you want to process and extract in your own start.sh script.

这篇关于重用继承图像的 CMD 或 ENTRYPOINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆