2

Read a lot of topics, but couldn't understand what's going on. All is working fine before I add ENTRYPOINT to my Dockerfile. Container stops immediately without demonizing php-fpm:

FROM php:5.6-fpm

// ..Some installation instructions

# Entrypoint script
COPY ./run.sh /run.sh
RUN chmod +x /run.sh

ENTRYPOINT ["/run.sh"]
CMD ["php-fpm"]

The content of run.sh:

# Install all dependencies
php -d allow_url_fopen=on /usr/local/bin/composer install

As I understand my entrypoint will be executed with run.sh and then exited. If I will remove it then default entrypoint will be starting nginx in background. What is the best solution to run shell scripts without redefining entrypoint? Or maybe I'm talking wrong things..

alvery
  • 1,863
  • 2
  • 15
  • 35

3 Answers3

3

ENTRYPOINT and CMD are combined to create the final COMMAND that is run when the container is started. In your case this gives:

["/run.sh", "php-fpm"]

which means that php-fpm acts as an argument to the /run.sh script. That's obviously not what you want.

You can fix this by starting php-fpm inside your script AND making sure that it runs as PID1 using exec. Running the main process as Process ID 1 assures that it will receive SIGKILL and SIGTERM interrupts (Ctrl-C for instance) and exit gracefully if possible.

# Install all dependencies
php -d allow_url_fopen=on /usr/local/bin/composer install
exec php-fpm

Your CMD should then be empty (or removed, as specifying an ENTRYPOINT also resets the CMD):

CMD []

Then in your container you can specify arguments to php-fpm via the command. Eg:

docker run -d my_php_fpm_image --help
AymDev
  • 6,626
  • 4
  • 29
  • 52
Bernard
  • 16,149
  • 12
  • 63
  • 66
2

The problem is this:

starting nginx in background

You need a process running in the foreground. If there is none, the container will exit. I think you should keep nginx running in the foreground.

To do this, use:

php-fpm -F -R

From php-fpm help:

-F, --nodaemonize force to stay in foreground, and ignore daemonize option from config file
-R, --allow-to-run-as-root Allow pool to run as root (disabled by default)

AymDev
  • 6,626
  • 4
  • 29
  • 52
dnephin
  • 25,944
  • 9
  • 55
  • 45
  • I need a solution that will autodeploy git and composer in my destination folder. So i took my php-fpm container and put the script in dockerfile. All stuff was deployed in my folder but php-fpm doesn't starts because i put my composer install in ENTRYPOINT and CMD command doesn't starts at all – alvery Oct 15 '16 at 19:46
  • I think you might be confused by what CMD is supposed to do. See http://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile. The container only runs a single process. – dnephin Oct 16 '16 at 16:40
0

Found the way to put composer in a separate container. So i will not touch my php-fpm at all, as the best practice is one process per container. My app container that contains all project files (composer.json, .git etc.) will provide Dockerfile:

FROM composer/composer:php5

# Set application directory
WORKDIR /var/www/html

ENTRYPOINT /usr/local/bin/composer install
CMD ["true"]

After initiating docker-compose up -d this will bring all the dependencies from composer.json into mapped directory.

alvery
  • 1,863
  • 2
  • 15
  • 35