I have to create a Dockerfile which has to inherit FROM centos:7 by definition and install nginx and php and get it to show a PHP file.

My Dockerfile:

FROM centos:7
EXPOSE 80/tcp
RUN yum -y update
RUN yum -y install epel-release
RUN yum -y install nginx php php-fpm
RUN echo '<?php phpinfo();' > /var/www/demo/index.php

My problems:

  1. How can I make docker understand that nginy has to start when the container is created and started?
  2. How do I make nginx understand to use PHP?

For 1. all I know is that this is not possible with docker build.

For 2. I wanted to use tutorials but those are all wrong, because:

  • there is no /etc/nginx/sites-available directory
  • there is no file called snippets/fastcgi-php.conf at any place to include in the nginx.conf

I have never worked with nginx before, so I have no idea where to start. No tutorial I found works, because all those files and folders don't exist. All I get is a 403 error page.

  • 1
    You're only supposed to run one thing in a container, thus you should have two containers, one for nginx and one for php-fpm. Both of these already have many containers on Docker Hub; look at how they are constructed for examples. – Michael Hampton Jan 13 at 22:06
  • I know that this would be the better way. But in this case the Dockerfile HAS TO inherit from centos:7. – Philipp Jan 14 at 8:27
  • Again, you can use the existing container definitions as an example. See for example centos/nginx-116-centos7 and centos/php-73-centos7. Actually you probably should just stop what you're doing and use those directly. – Michael Hampton Jan 14 at 10:07

This the approach that I have you at some point, you have to install and to config the nginx on the Dockerfile and start it any time when you run the docker, set this on docker-entrypoint.sh


#Install and config Nginx
RUN {\
    apt-get install nginx -y\
    rm /etc/nginx/sites-enabled/default\

ADD YOUR_CONFIG_FILE /etc/nginx/sites-enabled/
RUN service nginx start

RUN PATH="$PATH:docker-entrypoint.sh"
COPY docker-entrypoint.sh /
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]



service nginx start

Hope it helps!

New contributor
Ciprian Ilut is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Docker doesn't allow me to start nginx because of "Failed to get D-Bus connection: Operation not permitted" even when the container is started with --privileged option. And how do I make nginx use PHP? – Philipp Jan 14 at 10:06
  • I just forget it now. Docker is not able to run PHP on nginx no matter what I tried. Even if nginx runs, PHP lets the container or even the whole machine crash. – Philipp Jan 14 at 13:02
  • What I use to do is create a bare docker container and install everything on it, try to make it work and after that I make the dockerfile, I don't know if that could help you to notice what is going wrong. – Ciprian Ilut Jan 14 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.