0

I'm just learning Docker and have a problem with running nginx together with php. This is my Dockerfile

FROM nginx
RUN apt-get update -y
RUN apt-get install php7.4-fpm -y
ADD start.sh /
RUN chmod +x /start.sh
CMD ["/start.sh"]

start.sh content:

#!/bin/bash
service php7.4-fpm start
nginx -g 'daemon off;'

If I omit the last line CMD ["/start.sh"], accessing files from the host machine works but php files aren't processed because php7.4-fpm is not running. But when I add this line nginx stops serving any files. Through I can confirm that nginx and php are running inside the container with docker exec nginx-custom service nginx status and docker exec nginx-custom service php7.4-fpm status. The nginx error log is empty.

This is the CMD of the original nginx image, which I thought is the only thing that gets overwritten? I guess I have some basic problems in understanding how Docker works at this point.

CMD ["nginx", "-g", "daemon off;"]
  • You'd almost always run these in two separate containers. For a couple of reasons, commands like `service` don't work well in Docker. – David Maze Aug 10 '22 at 17:31
  • You should really avoid running multiple main processes in a single docker container, it is against the Docker philosophy. So ideally would be you ran two different containers, one for Nginx and one for PHP-fpm and then link them together through network. But if you really insist on using one, you should start from a Debian/Alpiune and install both Nginx and php-fpm and then run supervisord. Another possibility is to use apache which does not need another process to use PHP functionality. – matic1123 Aug 10 '22 at 17:32
  • I even found a good and very similar issue to yours on SO reference (https://stackoverflow.com/questions/46332919/combining-php-fpm-with-nginx-in-one-dockerfile) – matic1123 Aug 10 '22 at 17:33

0 Answers0