gpt4 book ai didi

Docker:Entrypoint 的覆盖涉及 CMD 规范?

转载 作者:行者123 更新时间:2023-12-02 18:21:03 26 4
gpt4 key购买 nike

这是我纯粹好奇的问题:

我必须个性化一个 Docker 镜像,特别是这是我的 dockerfile 的摘录:

ARG DEFAULT_PHP_VERSION
FROM php:${DEFAULT_PHP_VERSION:+${DEFAULT_PHP_VERSION}-}fpm-alpine
# RUN some personal stuff
RUN rm -rf /var/www/html

# Set proper Entrypoint
COPY build/fs/usr/local/bin/my-entrypoint.sh /usr/local/bin/my-entrypoint.sh
RUN chmod +x /usr/local/bin/my-entrypoint.sh
ENTRYPOINT [ "my-entrypoint.sh" ]

WORKDIR /var/www

my-entrypoint.sh是:
#!/bin/sh

set -e

echo "Trying my-entrypoint with args: $@"

if [ ! -z "$XDEBUG_ENABLED" ] ; then
echo "Enabling XDEBUG"
docker-php-ext-enable xdebug
fi

# execute default entrypoint
echo "Execute Main:"
docker-php-entrypoint $@
echo "Main Done"

原图是 PHP-FPM-ALPINE有命令
CMD [ "php-fpm" ]

我的问题是当我运行这个图像时(好吧,我们正确运行容器,而不是图像,我知道)默认命令没有传递给 my-entrypoint.sh ,实际上输出是:
Trying my-entrypoint with args: 
Enabling XDEBUG
Execute Main:
Main Done

那就是 ENTRYPOINT 没有收到默认命令 php-fpm ,所以主进程自动停止。

但是如果我修改 dockerfile 添加 CMD在末尾:
ARG DEFAULT_PHP_VERSION
FROM php:${DEFAULT_PHP_VERSION:+${DEFAULT_PHP_VERSION}-}fpm-alpine
# RUN some personal stuff
RUN rm -rf /var/www/html

# Set proper Entrypoint
COPY build/fs/usr/local/bin/my-entrypoint.sh /usr/local/bin/my-entrypoint.sh
RUN chmod +x /usr/local/bin/my-entrypoint.sh
ENTRYPOINT [ "my-entrypoint.sh" ]
CMD ["php-fpm"]

WORKDIR /var/www

一切顺利(即 CMD 被传递到入口点):
Trying my-entrypoint with args: php-fpm
Enabling XDEBUG
Execute Main:

最后我的问题:

为什么我必须重新声明 CMD ["php-fpm"]如果我更改 ENTRYPOINT指示?

note that CMD ["php-fpm"] is the same in original IMAGE.

最佳答案

这是从先前图像继承值时的异常(exception)情况之一。如果父图像定义了 CMD ,并且您的图像定义了 ENTRYPOINT ,那么 CMD 的值被清零。在所有其他情况下,您应该看到 ENTRYPOINTCMD从父图像继承不变。有关此决定背后的逻辑,请参阅 issue 5147 .

关于Docker:Entrypoint 的覆盖涉及 CMD 规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53298532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com