Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Entrypoint's override involve CMD specification?

Tags:

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.

like image 288
Sim Sca Avatar asked Nov 14 '18 10:11

Sim Sca


1 Answers

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.

like image 187
BMitch Avatar answered Nov 17 '22 00:11

BMitch