I have created two custom images, one for Nginx
, the other for Php-fpm
based on alpine
, created their containers, but for some reason Nginx
gives me 502 Bad Gateway
when trying to serve a php file, and the following error :
/var/log/nginx/error.log
:
2024/04/16 15:28:29 [error] 25#25: *16 connect() failed (111: Connection refused) while connecting to upstream, client: 172.30.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.30.0.3:9000", host: "localhost:8000"
but when I work with php:fpm-alpine
an image from Docker Hub, it works perfectly, you will find below a docker compose that contains both the Docker Hub image, and my custom image, then replace in the Nginx default.conf
file fastcgi_pass my-php-fpm:9000;
to fastcgi_pass php-fpm:9000;
in order to test both of them.
don't forget to run nginx -s reload
or restart the nginx container after modifying the config file.
I don't know from where is the problem, all of the containers share the same volume and the same network.
Nginx Dockerfile :
FROM alpine:3.19
RUN apk upgrade && apk update
RUN apk add nginx && apk add openssl
RUN mkdir /var/www/html
COPY ./default.conf /etc/nginx/http.d/
CMD nginx -g "daemon off;"
EXPOSE 80
default.conf :
server {
listen 80;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
try_files $uri =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
my-php-Fpm Dockerfile :
FROM alpine:3.19
RUN apk upgrade && apk update
RUN apk add curl php83 php83-phar php83-fpm
RUN mkdir -p /var/www/html/
CMD php-fpm83 -F
EXPOSE 9000
docker-compose.yml :
version: "3.8"
services:
nginx:
image: my-nginx
build:
context: ./nginx/
container_name: nginx
ports:
- 8000:80
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
php-fpm:
image: php:fpm-alpine
container_name: php-fpm
ports:
- 9000
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
my-php-fpm:
image: my-php-fpm
build:
context: ./my-php-fpm/
container_name: my-php-fpm
ports:
- 9000
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
networks:
net:
index.php :
<?php echo phpinfo();?>
Directory Structure:
test
├── docker-compose.yml
├── index.php
├── my-php-fpm
│ └── Dockerfile
└── nginx
├── Dockerfile
└── default.conf
Thanks to this StackOverflow answer that I found, I could get my custom php-fpm
container to work as expected
since I am using php-fpm82
I found its config within this path
/etc/php82/php-fpm.d/www.conf
I had to search for
listen = 127.0.0.1:9000
and change it to
listen = 0.0.0.0:9000
restarted the container and the php page index.php
got displayed as it should, but the thing that I don't understand is why the DockerHub image php:fpm-alpine
works well with my Nginx
container, even with listen = 127.0.0.1:9000
set in its configuration.
ANY IDEAS?
Thanks for @β.εηοιτ.βε and his clarification, you can find more informations within the comment section;