Search code examples
phpdockernginxdocker-composetraefik

Multiple Nginx + PHP container


I have this configuration : - 1 Nginx (dev) docker + 1 Php (dev) docker - 1 Nginx (prod) docker + 1 Php (pro) docker

I use traefik and docker-compose. The problem is that I can't connect the dev php docker to the dev nginx docker... I have the "File not found." Error. It means that nginx can't find the fastcgi config.

Here is my docker-compose :

version: '3'
networks:
  proxy:
    external: true
  internal:
    external: false
services:
  mysql:
    image: 'mysql:5.7'
    environment:
      MYSQL_ROOT_PASSWORD: null
    ports:
      - "3306:3306"
    networks:
      - internal
    labels:
      - traefik.enable=false
  portainer:
    image: portainer/portainer
    networks:
      - internal
      - proxy
    labels:
      - "traefik.frontend.rule=Host:portainer.xxx.xyz"
      - "traefik.port=9000"
      - "traefik.backend=portainer"
      - traefik.docker.network=proxy
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    restart: unless-stopped
  Nginx-Pro:
    image: nginx:alpine
    build:
      context: .
      dockerfile: Dockerfile_Nginx
    container_name: Nginx-Pro
    labels:
      - traefik.backend=Nginx-Pro
      - traefik.frontend.rule=Host:xxx.fr,www.xxx.fr, yyy.fr, www.yyy.fr
      - traefik.docker.network=proxy
      - traefik.frontend.passHostHeader=true
      - "traefik.port=80"
      - "traefik.frontend.entryPoints=http,https"
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
    volumes:
      - /var/www/nginx-pro/:/usr/share/nginx/
      - /etc/nginx/nginx-pro/:/etc/nginx/
    links:
      - php-pro
  Nginx-Dev:
    image: nginx:alpine
    build:
      context: .
      dockerfile: Dockerfile_Nginx
    container_name: Nginx-Dev
    labels:
      - traefik.backend=Nginx-Dev
      - traefik.frontend.rule=Host:trombi.xxx.xyz
      - traefik.docker.network=proxy
      - traefik.frontend.passHostHeader=true
      - "traefik.port=80"
      - "traefik.frontend.entryPoints=http,https"
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
    volumes:
      - /var/www/nginx-dev/:/usr/share/nginx/
      - /etc/nginx/nginx-dev/:/etc/nginx/
    links:
      - php-dev
  php-pro:
    container_name: php-pro
    image: php:7.2-fpm-alpine3.7
    build:
      context: .
      dockerfile: Dockerfile_PHP
    labels:
      - traefik.docker.network=proxy
      - "traefik.port=9000"
    networks:
      - internal
      - proxy
    volumes:
      - /var/www/nginx-pro/:/usr/share/nginx/
  php-dev:
    image: php:7.2-fpm-alpine3.7
    container_name: php-dev
    build:
      context: .
      dockerfile: Dockerfile_PHP
    labels:
      - traefik.docker.network=proxy
      - "traefik.port=9001"
    ports:
      - 9001:9000
    networks:
      - internal
      - proxy
    volumes:
      - /var/www/nginx-dev/:/usr/share/nginx/

Here is my php conf for the site :

server {
   listen       80;
   server_name  trombi.xxx.xyz www.trombi.xxx.xyz;

   #charset koi8-r;
   #access_log  /var/log/nginx/log/host.access.log  main;


    location / {
       root   /usr/share/nginx/trombi.xxx.xyz;
       index  index.html index.htm index.php;
       try_files $uri $uri/ /index.php$is_args$args;
     }

   #error_page  404              /404.html;

   # redirect server error pages to the static page /50x.html
   #
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /usr/share/nginx/html;
   }

   # proxy the PHP scripts to Apache listening on 127.0.0.1:80
   #
   #location ~ \.php$ {
   #    proxy_pass   http://127.0.0.1;
   #}

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~* \.PHP$ {
        fastcgi_index   index.php;
        fastcgi_pass    php:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    /usr/share/nginx/trombi.xxx.xyz/$fastcgi_script_name;
    }
}

And this error in logs :

2018/05/17 06:59:47 [error] 8#8: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.19.0.5, server: trombi.xxx.xyz, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://172.20.0.4:9000", host: "trombi.xxx.xyz"
    172.19.0.5 - 
- [17/May/2018:06:59:47 +0000] "GET /info.php HTTP/1.1" 404 47 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.97"

Thank you for your help :) I can't find where it come from...


UDPATE : Thanks to @Constantin Galbenu Juste need to change in nginx conf:

fastcgi_pass    php:9000;

to :

fastcgi_pass    php-dev:9000;

Solution

  • In docker-compose the services are accessible by their name. Docker will resolve the IP address of the service automatically (you don't need to use IP addresses).

    In your case, the name of the PHP service is php-dev and not just php.

    The solution is to replace fastcgi_pass php:9000; with fastcgi_pass php-dev:9000;