Search code examples
reactjslaraveldockerdocker-composeport

ERR_EMPTY_RESPONSE - Laravel/Nginx with Docker


I am new to Docker and I have some difficulties understanding what I'm doing wrong. I tried to create one docker environment to host a React App (Frontend) and a Laravel App (Backend).

React is working like a charm, but Laravel won't work. When I try to access the domain:Port via the Browser I get the error "ERR_EMPTY_RESPONSE - localhost didn't sen any data".

I'd be really glad if someone could help me. I will attach my docker-compose.yml, nginx config and Dockerfile for Laravel.

Thanks in the meantime

docker-compose.yaml

version: '3.7'
services:

    frontend:
      build:
        context: ./app-frontend 
      ports:
        - "80:80"
      environment:
        - watchpack_polling=true
      volumes:
        - ./app-frontend:/app
      networks:
        - app-network

    nginx:
      image: nginx:alpine
      ports:
        - "8000:80"
      volumes:
        - ./app-backend:/var/www
        - ./nginx/conf.d/:/etc/nginx/conf.d/
      depends_on:
        - backend
        - appdb
      networks:
        - app-network

    backend:
      build:
        context: ./app-backend/
        dockerfile: Dockerfile
      restart: unless-stopped
      environment:
        MYSQL_DATABASE: ${DB_DATABASE}
        MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
        MYSQL_PASSWORD: ${DB_PASSWORD}
        MYSQL_USER: ${DB_USERNAME}
      working_dir: /var/www
      volumes:
        - ./app-backend:/var/www
      ports:
        - "9000:9000"
      networks:
        - app-network
        - db

    appdb:
      image: mysql:5.7
      restart: unless-stopped
      environment:
        MYSQL_DATABASE: ${DB_DATABASE}
        MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
        MYSQL_PASSWORD: ${DB_PASSWORD}
        MYSQL_USER: ${DB_USERNAME}
      volumes:
        - ./initdb:/docker-entrypoint-initdb.d
      ports:
        - 3306:3306
      networks:
        - app-network
        - db

    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      restart: unless-stopped
      environment:
        PMA_HOST: appdb
        MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      depends_on:
        - appdb
      ports:
        - 8443:80
      networks:
        - db

networks:
  app-network:
    driver: bridge
  db:

nginx config:

server {
    listen 8000;
    server_name yourdomain.com;  # Change to your domain or localhost

    root /var/www/public;  # Path to your Laravel public directory

    index index.php;

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

    location ~ \.php$ {
        fastcgi_pass backend:9000;  # PHP-FPM server address and port
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Dockerfile:

########################################################################
### Composer

FROM composer:2.6.2 AS composer

COPY composer.json /app
COPY composer.lock /app

RUN composer install        \
    --ignore-platform-reqs  \
    --no-ansi               \
    --no-autoloader         \
    --no-interaction        \
    --no-scripts

COPY . /app/
RUN composer dump-autoload --optimize --classmap-authoritative

### Composer
########################################################################
### NodeJS

FROM node:20.6.1-alpine3.18 AS node

WORKDIR /app

COPY package.json           /app
COPY package-lock.json      /app
COPY webpack.mix.js         /app
COPY /resources             /app/resources

RUN npm install && npm run dev

### NodeJS
########################################################################
### PHP

FROM php:8.1-fpm-alpine3.18

RUN apk update && apk add --no-cache \
    libpng-dev                       \
    freetype-dev                     \
    oniguruma-dev                    \
    libxml2-dev

RUN rm -rf /var/lib/apt/lists/* && rm -rf /var/cache/apk/*
RUN docker-php-ext-configure gd --enable-gd --with-freetype
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

COPY . /var/www
COPY --from=composer /app/vendor                /var/www/html/vendor
COPY --from=node     /app/public                /var/www/html/public/
COPY --from=node     /app/mix-manifest.json     /var/www/html/mix-manifest.json


RUN addgroup -g 1000 -S www && \
    adduser -u 1000 -S www -G www-data

COPY --chown=www:www-data . /var/www

WORKDIR /var/www

### PHP
########################################################################

I tried playing around with the ports and different images, but nothing was successful. http://localhost:8000 always shows the same error. I also checked if the ports where blocked by another system or firewall, but that wasn't the case.

EDIT: This is what docker compose ps returns:

ims_mk-appdb-1 | mysql:5.7 | "docker-entrypoint.sh mysqld" | appdb | 55 seconds ago | Up 38 seconds | 0.0.0.0:3306->3306/tcp, 33060/tcp

ims_mk-backend-1 | ims_mk-backend | "docker-php-entrypoint php-fpm" | backend | 55 seconds ago | Up 36 seconds | 8000/tcp, 0.0.0.0:9000->9000/tcp

ims_mk-frontend-1 | ims_mk-frontend | "/bin/sh -c 'npm start'" | frontend | 55 seconds ago | Up 43 seconds | 0.0.0.0:80->80/tcp

ims_mk-nginx-1 | nginx:alpine | "/docker-entrypoint.sh nginx -g 'daemon off;'" | nginx | 51 seconds ago | Up 26 seconds | 80/tcp, 0.0.0.0:8000->8000/tcp

ims_mk-phpmyadmin-1 | phpmyadmin/phpmyadmin | "/docker-entrypoint.sh apache2-foreground" | phpmyadmin | 51 seconds ago | Up 26 seconds | 0.0.0.0:8443->80/tcp


Solution

  • I solved the issue by creating a fresh docker installation. I don't really know what caused the issue. Anyways if someone is interested, here is my new configuration.

    docker-compose.yaml

    version: '3.8'
    
    services:
    
        # Frontend App
        frontend:
          build:
            context: ./frontend 
          ports:
            - "80:80"
          environment:
            - watchpack_polling=true
          volumes:
            - ./frontend:/app
    
        # Web Server Service
        nginx:
          image: nginx:alpine
          container_name: nginx
          ports:
            - "8080:80"
          volumes:
            - ./backend:/var/www
            - ./nginx/conf.d/:/etc/nginx/conf.d/
          depends_on:
            - backend_app
            - db
    
        db:
          image : mysql
          container_name : mysql
          volumes:
            - ./mysql/data:/var/lib/mysql
          ports:
            - "3306:3306"
          environment:
            MYSQL_DATABASE: backend
            MYSQL_ROOT_PASSWORD: password
    
        # Application Service
        backend_app:
          container_name: backend
          build: 
            context: ./php
            dockerfile: Dockerfile
          volumes:
            - ./backend:/var/www
          ports:
            - "9000:9000"
          working_dir: /var/www
    
        #phpMyAdmin Service
        phpmyadmin:
          image: phpmyadmin/phpmyadmin
          container_name: phpmyadmin
          ports:
            - "3400:80"
          depends_on:
            - db

    dockerfile:

    FROM php:8.2.11-fpm
    
    # Install composer
    RUN echo "\e[1;33mInstall COMPOSER\e[0m"
    RUN cd /tmp \
        && curl -sS https://getcomposer.org/installer | php \
        && mv composer.phar /usr/local/bin/composer
    
    RUN docker-php-ext-install pdo pdo_mysql
    
    RUN apt-get update
    
    # Install useful tools
    RUN apt-get -y install apt-utils nano wget dialog vim
    
    # Install important libraries
    RUN echo "\e[1;33mInstall important libraries\e[0m"
    RUN apt-get -y install --fix-missing \
        apt-utils \
        build-essential \
        git \
        curl \
        libcurl4 \
        libcurl4-openssl-dev \
        zlib1g-dev \
        libzip-dev \
        zip \
        libbz2-dev \
        locales \
        libmcrypt-dev \
        libicu-dev \
        libonig-dev \
        libxml2-dev

    nginx config:

    server {
      listen 80;
      index index.php index.htm index.html;
      error_log /var/log/nginx/error.log;
      access_log /var/log/nginx/access.log;
      server_name localhost;
      root /var/www/public;
    
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
    
      location /index.php {
          try_files $uri = 404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass backend_app:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
    
      }
    }