I'm learning docker compose and trying to run the project provided from some Docker course. There is the project to link nginx:latest and php:8.2-fpm containers to display message from index.php. But I cannot reproduce the results. It's supposed to display "Hello from PHP!" message, when I go to localhost, but I've got this message instead:
Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx.
As I understand nginx container is ok, and the problem is with linking nginx and php.
Explain how to solve this issue, please.
Project structure:
project/ ├── docker-compose.yml ├── nginx/ │ ├── default.conf │ ├── html/ │ └── Dockerfile ├── php/ │ ├── index.php │ └── Dockerfile
Here is docker-compose.yml:
version: '3.8'
services:
nginx:
build: ./nginx
image: nginx:latest
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx/html:/var/www/html
depends_on:
- php
networks:
- app_network
php:
build: ./php
image: php:8.2-fpm
container_name: php
volumes:
- ./php:/var/www/html
networks:
- app_network
networks:
app_network:
driver: bridge
nginx/default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
nginx/Dockerfile:
FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/
COPY ./html /usr/share/nginx/html
Docker Desktop logs:
NOTICE: fpm is running, pid 1 NOTICE: ready to handle connections
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2025/05/25 08:03:14 [notice] 1#1: using the "epoll" event method 2025/05/25 08:03:14 [notice] 1#1: nginx/1.27.5 2025/05/25 08:03:14 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 2025/05/25 08:03:14 [notice] 1#1: OS: Linux 5.15.167.4-microsoft-standard-WSL2 2025/05/25 08:03:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2025/05/25 08:03:14 [notice] 1#1: start worker processes 2025/05/25 08:03:14 [notice] 1#1: start worker process 29 2025/05/25 08:03:14 [notice] 1#1: start worker process 30 2025/05/25 08:03:14 [notice] 1#1: start worker process 31 2025/05/25 08:03:14 [notice] 1#1: start worker process 32 2025/05/25 08:03:14 [notice] 1#1: start worker process 33 2025/05/25 08:03:14 [notice] 1#1: start worker process 34 2025/05/25 08:03:14 [notice] 1#1: start worker process 35 2025/05/25 08:03:14 [notice] 1#1: start worker process 36 2025/05/25 08:03:14 [notice] 1#1: start worker process 37
Sorry about such amount of text.
It worked! I'm not sure if this is the case, but apparanely port 80 which nginx was supposed to listen was busy.
I checked sudo lsof -i :80
and got list of services:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 167 root 5u IPv4 27834 0t0 TCP *:http (LISTEN)
nginx 167 root 6u IPv6 27835 0t0 TCP *:http (LISTEN)
nginx 168 www-data 5u IPv4 27834 0t0 TCP *:http (LISTEN)
...
After stopping them my message from index.php was displayed at http://localhost.
Summarized project structure:
project/
├── docker-compose.yml
├── nginx/
│ └── default.conf
├── php/
│ └── index.php
Delete Dockerfiles - containers are based on original (not custom) repository images.
Delete nginx/html dir - it seems to be unused in this primitive task.
Summarized files content:
docker-compose.yml:
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./php:/var/www/html
depends_on:
- php
networks:
- app_network
php:
image: php:8.2-fpm
container_name: php
volumes:
- ./php:/var/www/html
networks:
- app_network
networks:
app_network:
driver: bridge
nginx/default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Thanks Lukasz Zagroba and David Maze for useful links and info!