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

查看:64
本文介绍了重用继承的图像的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中,看不到当前 CMD ENTRYPOINT 的值。如果您控制上游基本映像并在其中包含此代码,则允许下游组件进行更改,那么拥有 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

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天全站免登陆