Docker: Entrypoint's override involve CMD specification?
Asked Answered
O

1

10

That's a question for my pure curiousity:

I have to personalize a Docker Image, in particular this is an extract of my 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

and my-entrypoint.sh is:

#!/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"

the original image is PHP-FPM-ALPINE that has the command

CMD [ "php-fpm" ]

My problem is that when i run this image (ok, we properly run containers, not images, I know) the default command is not passed to my-entrypoint.sh, in fact the output is:

Trying my-entrypoint with args: 
Enabling XDEBUG
Execute Main:
Main Done

That is the ENTRYPOINT doesn't receive the default command php-fpm, and so the main process automatically stops.

BUT if I modify the dockerfile adding the CMD at the end:

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

and all goes right (that is the CMD is passed to entrypoint):

Trying my-entrypoint with args: php-fpm
Enabling XDEBUG
Execute Main:

Finally my question:

why I have to redeclare the CMD ["php-fpm"] if I change the ENTRYPOINT directive?

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

Ossian answered 14/11, 2018 at 10:54 Comment(2)
Without having CMD [ "php-fpm" ] in your docker file can you docker exec -it containerName sh for me and check what docker-php-entrypoint is, should be located somewhere like /usr/local/bin/docker-php-entrypoint. Presumably this is changing, compare the differences. Else, try outputting exactly what $@ is to see what's the difference is there.Lema
Possible duplicate: Is CMD in parent docker overriden by CMD/ENTRYPOINT in child docker image?Nickelplate
N
9

This is one of the exception cases when inheriting values from a previous image. If the parent image defines a CMD, and your image defines an ENTRYPOINT, then the value of CMD is nulled out. In all other scenarios, you should see ENTRYPOINT and CMD inherited from parent images unchanged. For the logic behind this decision, please see issue 5147.

Nickelplate answered 14/11, 2018 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.