PHP-FPM wont start from a Dockerfile

Viewed 4067

Hello dear community,

I am trying to accomplish something very simple, I want to start a php-fpm service from a docker container using a dockerfile. My dockerfile content is posted here below:

   FROM debian
   RUN apt-get update && apt-get install php -y && apt-get install php7.3-fpm -y && service php7.3-fpm start

When I build this image from the dockerfile and run it as a container, the php-fpm service is not active.

I even tried it with using docker's "interactive mode" (-i arg) to ensure that the container was not exiting in the case that the service was running as a daemon.

I am confused because the command RUN service php7.3-fpm start from my dockerfile should have started the service.

To successfully start the service inside my container I actually have to manually log into it using the command docker exec -it #containerID bash and run the command service php7.3-fpm start myself, and then the service works and becomes active.

I don't understand why the php-fpm service is not starting automatically from my Dockerfile, any help would be very much appreciated. Thanks in advance!

1 Answers

To a first approximation, commands like service don't work in Docker at all.

A Docker container runs only a single foreground process. That's not usually an init system, or if it is, it's just enough to handle some chores like zombie process cleanup. Conversely, a Docker image only contains a filesystem image and some metadata on how to start that process, but it does not persist any running processes. So for example if you

RUN service php7.3-fpm start

it might record in some file that the service was supposed to have been started, but once the RUN command completes, the running process doesn't exist at all any more.

The easiest way to get a running PHP-FPM setup is to use the Docker Hub php image:

FROM php:7.3-fpm

This should do all of the required setup, including arranging for the FPM server to run as the main container command; you just need to COPY your application code in.

If you really want to run it yourself, you need to make it be the main command of your custom image

CMD ["php-fpm"]

as is done in php:7.3-fpm's Dockerfile.

Related