Docker for Laravel multi tenancy application -Error 502

47 views Asked by At

I have configured a Docker setup consisting of multiple services: PHP, Nginx, MySQL, and Redis. My intention is to run a Laravel application using this configuration. However, when I execute docker-compose up, I encounter a "Bad Gateway" error. Here's an overview of my setup:

Dockerfile for PHP Service:

# Use the official PHP image as the base image with PHP 8.2
FROM php:8.2-fpm

# Set the working directory in the container
WORKDIR /var/www/html

# Copy composer.lock and composer.json to install dependencies
COPY composer.lock composer.json /var/www/html/

# Install Composer (PHP dependency manager)
RUN apt-get update && apt-get install -y \
    git \
    zip \
    unzip \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    zlib1g-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy the Laravel application files to the working directory in the container
COPY . /var/www/html

# Install dependencies
RUN composer install


# Expose port 9000 and start php-fpm server
EXPOSE 9001
CMD ["php-fpm"]

Docker Compose File (docker-compose.yml):

version: '3'

services:
    nginx:
        container_name: nginx_container
        image: nginx:latest
        ports:
            - "80:80"
        volumes:
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php
        networks:
            - app-network

    php:
        container_name: php_container
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - .:/var/www/html
        ports:
            - "9001:9000"
        depends_on:
            - mysql
            - redis
        networks:
            - app-network

    mysql:
        container_name: mysql_container
        image: mysql:5.7
        platform: linux/amd64
        restart: always
        environment:
            MYSQL_DATABASE: zmenu
            MYSQL_USER: user
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: root_password
        volumes:
            - mysql_data:/var/lib/mysql
        networks:
            - app-network

    redis:
        container_name: redis_container
        image: redis:latest
        ports:
            - "6379:6379"
        networks:
            - app-network

networks:
    app-network:
        driver: bridge
        external: true

volumes:
    mysql_data:

Nginx Configuration (nginx/default.conf):

server {
    listen 80 default_server;
    server_name ~^(?<subdomain>.+)\.zmenu\.test$;

    location / {
        proxy_pass http://php:9001;
        proxy_set_header Host $subdomain.zmenu.test;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Despite setting up the services and Nginx configuration, running docker-compose up results in a "Bad Gateway" error. I'm unsure why this error is occurring. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated.

0

There are 0 answers