502 bad gateway error

Hi,
I am new to Docker and created an image using pre-baked images nginx:alpine and php:fpm-alpine. These are two containers and connected them via internal network bridge. Here is my docker-compose.yml file

version: "3"
services:
  nginx:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports: 
      - "8080:80"
    networks:
      - internal
    volumes:
      - ./data/:/var/www/html/
      - ./logs/nginx:/var/log/nginx/
  php:
    image: php:fpm-alpine
    networks:
      - internal
    volumes:
      - ./data/:/var/www/html/
      - ./logs/php.log:/var/log/fpm-php.www.log
networks:
  internal:
    driver: bridge

Now, I composed up this yml file and can see containers up and running. I saved and loaded the php container image and then tried to access the app. I get 502 bad gateway error.
this is default.conf file where I am configuring the port

server {
    listen 0.0.0.0:80;
    root /var/www/html;
    location / {
        index index.php index.html;
    }
    location ~ \.php$ {
        include fastcgi_params; 
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

docker-compose.yml file for the loaded image.

version: "3"
services:
  nginx:
    image: php_with_nginx_nginx:latest
    ports: 
      - "8080:80"
    networks:
      - internal
    volumes:
      - ./data/:/var/www/html/
      - ./logs/nginx:/var/log/nginx/
  php:
    image: phpnginxload:latest
    networks:
      - internal
    volumes:
      - ./data/:/var/www/html/
      - ./logs/php.log:/var/log/fpm-php.www.log
    ports:
      - "9000:9000"
networks:
  internal:
    driver: bridge

When checked logs, there is no php service in the container

7020:~/php_with_nginx$ docker exec -it php_with_nginx_php_1 sh
/ # netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
tcp        0      0 127.0.0.11:38961        0.0.0.0:*               LISTEN      
udp        0      0 127.0.0.11:33901        0.0.0.0:*                    


there is no port 9000 here...

/usr/sbin # php-fpm -F
sh: php-fpm: not found
/usr/sbin # php -v
sh: php: not found
/usr/sbin # 

any pointers would be highly appreciated.

If you don’t specify a network for your service a new bridging network is created automatically with your containers attached to it. So no real need for the lines regarding network within your docker-compose.yml. This is not the reason for the error but why make things more complicated than neccessary

Why do you save the images and use them for another docker-compose.yml? If you make changes to the pre-baked images that should survive a container-restart it is better to create you own image using a Dockerfile based on the pre-baked image and use the resulting image. Or if you don’t need any changes to the pre-baked images that you can stay with your initial docker-compose.yml.

To get information what is started when a constainer is started you can run docker container inspect <containername|containerid> and search for the Path and Args-attribute or directly use docker container inspect --format="{{.Path}} - {{.Args}}" <containername|containerid>.
Or directly check the image using docker image inspect <imagename|imageid> and search for Config → Cmd or directly use docker image inspect --format="{{.Config.Cmd}}" <imagename|imageid>

Maybe its easier with a single container, there is a simple php:apache image version.

php:<version>-apache
This image contains Debian’s Apache httpd in conjunction with PHP (as mod_php) and uses mpm_prefork by default.

https://hub.docker.com/_/php

1 Like