Parham Doustdar
Parham Doustdar

Reputation: 2039

How can I have nginx in one container and php-fpm in another?

I am trying to create two docker containers. One would contain nginx, and another would contain php-fpm. Here is my docker-compose.yml:

version: '2'
services:
  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
  fpm:
    build: ./php
    volumes:
      - ./php/code:/var/www/html/

NGINX

Here is my Dockerfile for the nginx container:

FROM nginx:latest RUN rm /etc/nginx/conf.d/default.conf COPY ./default.conf /etc/nginx/conf.d/

And, here is my default.conf:

server {
    listen  80;

    server_name localhost;
    root /var/www/html;

    error_log /var/log/nginx/localhost.error.log;
    access_log /var/log/nginx/localhost.access.log;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

This is all my nginx configuration.

PHP

Here is the Dockerfile in the ./php directory:

from php:fpm
COPY ./code/ /var/www/html/

Inside the ./code directory, i have a file named app.php that contains phpinfo().

The Problem

I run docker-compose up and when I try to open 192.168.99.100 (the IP of the docker machine in which docker engine is running), I get File not found. I have also tried 192.168.99.100/app.php, but it's the same.

What am I configuring wrong? I saw in an example on the Internet that the PHP files must live in the nginx container, but that doesn't make any sense since as far as I know, php-fpm is the process that must have access to those files.

Upvotes: 3

Views: 2680

Answers (2)

Parham Doustdar
Parham Doustdar

Reputation: 2039

After doing what has been suggested in the other answer and spending about six hours or more on this issue, I found that the reason my set up wasn't working was because docker-compose up does not rebuild your images, so the configuration in the container was an older version.

So, fixing this was as easy as docker-compose build and then docker-compose up.

Sorry for taking everyone's time.

Upvotes: 0

Andreas Koch
Andreas Koch

Reputation: 2037

The reason for your 404 error is that your Nginx container has no files in it.

You must link the same files you linked into the PHP-FPM container into the Nginx container:

version: '2'
services:
  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./php/code:/var/www/html/

  fpm:
    build: ./php
    volumes:
      - ./php/code:/var/www/html/

When the request reaches the web-server, the file must at least exist before the Nginx can pass the request along to the PHP-FPM container. You could even make the folder read-only for the Nginx container:

version: '2'
services:
  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./php/code:/var/www/html/:ro

  fpm:
    build: ./php
    volumes:
      - ./php/code:/var/www/html/

Upvotes: 3

Related Questions