Docker Compose get bad gateway with php app served by NGINX

  docker, docker-compose, nginx, php

I am trying to serve a PHP API with NGINX using docker-compose, but I am getting a "502 Bad Gateway" error on deployment.
I only have this error log in nginx container

2021/05/03 20:33:13 [error] 8#8: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: api.arche.com, request: "GET / HTTP/1.1", upstream: "fastcgi://172.21.0.3:9000", host: "api.arche.com:8080"

It seems that php-fpm si successfully running in PHP container, the API and the db are connected and it seems that nginx configuration is ok

Here is the docker-compose.yml file:

version: "3.8"

services:
  api:
    env_file:
      - .env
    build:
      args:
        user: ${APP_USER}
        uid: ${APP_UID}
      context: ./back/
    image: arche_api
    ports:
      - 9000:9000
    expose:
      - 9000
      - 3306
    container_name: arche_api
    restart: unless-stopped
    tty: true
    working_dir: /var/www/back/
    volumes:
      - ./back/:/var/www/back/
      - ./config/database:/var/lib/mysql
    depends_on:
      - db
    networks:
      - arche

  db:
    env_file:
      - .env
    image: mysql:5.7
    container_name: arche_db
    restart: unless-stopped
    ports:
      - 3306:3306
    expose:
      - 3306
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'false'
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USER}
      SERVICES_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./config/database:/docker-entrypoint-initdb.d
    networks:
      - arche

  server:
    image: nginx:1.13-alpine
    container_name: arche_server
    restart: unless-stopped
    volumes:
      - ./back/:/var/www/back
      - ./config/server/:/etc/nginx/conf.d/
    ports:
      - 8080:80
    networks:
      - arche
    depends_on:
      - api
      - db
    links:
      - api
      - db

volumes:
  db_data: {}

networks:
  arche:
    driver: bridge

the PHP API Dockerfile:

FROM debian:stable-slim

ARG user
ARG uid

RUN rm -f /etc/localtime 
 && ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime

WORKDIR /var/www/arche

RUN apt update 
 && apt -y upgrade 
 && apt -y install vim git curl wget zip apt-transport-https lsb-release ca-certificates netcat

RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
RUN apt update 
 && apt -y install php8.0-common php8.0-fpm php8.0-bz2 php8.0-curl php8.0-intl php8.0-mysql php8.0-readline php8.0-xml php8.0-mbstring

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN mv composer.phar /bin/composer
RUN php -r "unlink('composer-setup.php');"

RUN sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 40M/g' /etc/php/8.0/fpm/php.ini
RUN sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 40M/g' /etc/php/8.0/cli/php.ini
RUN sed -i 's/post_max_size = 8M/post_max_size = 64M/g' /etc/php/8.0/fpm/php.ini
RUN sed -i 's/post_max_size = 8M/post_max_size = 64M/g' /etc/php/8.0/cli/php.ini

RUN useradd -G www-data,root -u ${uid} -d /home/${user} ${user}
RUN mkdir -p /home/${user} && chown -R ${user}:${user} /home/${user}

RUN mkdir -p /var/run
RUN /etc/init.d/php8.0-fpm start

EXPOSE 9000
CMD ["php-fpm8.0", "-F", "-R"]

and finnaly the NGINX conf file:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name api.arche.com;

    root /var/www/back/public/;
    index index.php index.html;

    access_log /var/log/nginx/api-access.log;
    error_log  /var/log/nginx/api-error.log error;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

I can’t figure what is wrong with my docker configuration
thank you for all your futur answers

EDIT :
Here are the api container logs:

docker logs arche_api
[03-May-2021 23:48:31] NOTICE: fpm is running, pid 1
[03-May-2021 23:48:31] NOTICE: ready to handle connections
[03-May-2021 23:48:31] NOTICE: systemd monitor interval set to 10000ms

Source: Docker Questions

LEAVE A COMMENT