Ahmed SEDDIK
Ahmed SEDDIK

Reputation: 181

Where i put my index.php when using docker nginx image

I can't find where i put my file index.php so that ngnix can interpreter my file. All containers are working and when i put localhost:8080 nginx works Here is my docker-compose.yml

web:
  image: nginx
  volumes:
   - ./templates:/etc/nginx/templates
  ports:
   - "8080:8080"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=8080

php:
       image: php:7.0-fpm
       expose:
           - 9000
       volumes_from:
           - app
       links:
           - elastic

app:
       image: php:7.0-fpm
       volumes:
           - .:/src

elastic:
       image: elasticsearch:2.3
       volumes:
         - ./elasticsearch/data:/usr/share/elasticsearch/data
         - ./elasticsearch/logs:/usr/share/elasticsearch/logs
       expose:
         - "9200"
       ports:
         - "9200:9200"

Can anyone help me please

Upvotes: 1

Views: 2328

Answers (1)

Adiii
Adiii

Reputation: 59936

You need to mount same volumes for both PHP and Nginx docker image.

version: '3'
services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./app:/app
      - ./nginx-config/:/etc/nginx/conf.d/
    ports:
      - 80:80
    depends_on:
      - php
  php:
    image: php:7.3-fpm-alpine
    volumes:
     - ./app:/app

in the above compose file, code is placed under app folder in the host.

Tree

├── app
│   ├── helloworld.php
│   └── index.php
├── docker-compose.yml
└── nginx-config
    └── default.conf

You Nginx config should use docker service network to connect with php-fpm container.

server {
    index index.php index.html;
    server_name php-docker.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /app/;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Or you can try the working example from Github.

git clone https://github.com/Adiii717/dockerize-nginx-php.git
cd dockerize-nginx-php;
docker-compose up

Now open the browser

http://localhost/helloworld.php

Upvotes: 1

Related Questions