I made a Dockerfile, but when I run it and enter the container, the php8.0-fpm service is not running.
How do I make it run at build time? Note that I run the command service php8.0-fpm start
in the Dockerfile and even then it is not running.
What should I do for the php8.0-fpm service to start along with the container?
Below is the Dockerfile I made:
FROM ubuntu:jammy
ENV DEBIAN_FRONTEND=noninteractive
# Instalação Apache e PHP
RUN apt-get update &&
apt-get install software-properties-common -y &&
add-apt-repository ppa:ondrej/php -y &&
apt-get update &&
apt-get install -y
apache2
libapache2-mod-php8.0
libapache2-mod-php
php8.0-fpm
libapache2-mod-fcgid
# Alteração sequência index
COPY /src/dir.conf /etc/apache2/mods-enabled
# Commitando a nova configuração
RUN service apache2 restart
RUN service php8.0-fpm restart
# Inserindo página info.php
COPY /src/info.php /var/www/html
# Alterando módulos de multiprocessamento
RUN service apache2 stop &&
a2dismod php8.0 &&
a2dismod php8.1 &&
a2dismod mpm_prefork &&
a2enmod mpm_event &&
a2enconf php8.0-fpm &&
a2enmod proxy &&
a2enmod proxy_fcgi &&
service apache2 restart &&
service php8.0-fpm start
# Entrypoint para o conteiner iniciar o Apache
ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]```
3
Answers
I managed to leave it in just one container, PHP has an extension called Supervisor and with it installed we were able to start two or more services inside the container.
The Dockerfile looked like this:
And I created two configuration files for Supervisor.
apache.conf
fpm.conf
With that the two services started and it is working perfectly!
A Docker container only runs a single process. It doesn’t "run services" per se. The image build doesn’t preserve any running processes either. When you start the container, the image’s
ENTRYPOINT
orCMD
is the only thing that will be running in the container.Avoiding the technical reasons, I’d broadly suggest that commands like
service
orsystemctl
just don’t work in Docker.RUN service php8.0-fpm restart
, for example, does nothing: PHP-FPM wasn’t running before this command, and after theRUN
command it won’t be running either.You’d typically restructure this into multiple separate containers, using a tool like Docker Compose to run them all together. Docker has a couple of official sample applications that demonstrate this. A Compose-based setup for this might look like
Your
Dockerfile
would beginFROM php:8.0-fpm
and contain only the PHP-related setup;Dockerfile.apache
would beginFROM httpd:2.4
and only copy in the static assets and Apache configuration. Your proxy setup would need to reference the other container by nameProxyPass "/" "fcgi://php:9000"
; see Networking in Compose in the Docker documentation for additional details.Note that, even in this case, something like
docker-compose exec apache service apache2 status
still wouldn’t show "a service is running", but if youdocker-compose exec apache ps -e
you’d see the HTTP daemon as process 1 within the container. This is normal.You need to run php fpm on startup. if you installed bash in your vm os, you can do it this way.
STOPSIGNAL SIGTERM
CMD ["/bin/bash", "-c", "php-fpm8 && include your apache here"]
Full guide: How to setup PHP 8, NGINX and PHP-FPM with docker