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"]```
CodePudding user response:
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
or CMD
is the only thing that will be running in the container.
Avoiding the technical reasons, I'd broadly suggest that commands like service
or systemctl
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 the RUN
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
# docker-compose.yaml
version: '3.8'
services:
php:
build: . # using default Dockerfile
apache:
build:
context: .
dockerfile: Dockerfile.apache
ports:
- '8000:80'
Your Dockerfile
would begin FROM php:8.0-fpm
and contain only the PHP-related setup; Dockerfile.apache
would begin FROM httpd:2.4
and only copy in the static assets and Apache configuration. Your proxy setup would need to reference the other container by name ProxyPass "/" "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 you docker-compose exec apache ps -e
you'd see the HTTP daemon as process 1 within the container. This is normal.
CodePudding user response:
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:
FROM httpd:2.4-alpine
RUN apk update && \
apk add \
php \
php-fpm \
php-zip \
composer \
supervisor
COPY . /usr/local/apache2/htdocs
COPY httpd.conf /usr/local/apache2/conf/httpd.conf
COPY supervisor /etc/supervisor
WORKDIR /usr/local/apache2/htdocs
CMD ["supervisord","-n", "-c", "/etc/supervisor/supervisord.conf"]
And I created two configuration files for Supervisor.
apache.conf
[program:apache]
command=httpd -DFOREGROUND
autostart=true
autorestart=true
priority=10
startretries=1
startsecs=1
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
fpm.conf
[program:php-fpm]
command = php-fpm8 --nodaemonize
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
With that the two services started and it is working perfectly!