Laravel docker container not serving website

  docker, dockerfile, kubernetes, laravel

I am trying to build a docker image for kubernetes cluster using Dockerfile for a Laravel website. I have not much experience with docker, so I searched internet and found a Dockerfile with docker-compose where I can run the website. But I want to use only Dockerfile and keep the Website only in a single container, no database or other container and no docker-compose.yaml file. Can you suggest me what should I need to do to make a single docker container that only serves the website? I want to use nginx as the web server.

Here is my Dockerfile

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y 
    git 
    curl 
    libpng-dev 
    libonig-dev 
    libxml2-dev 
    zip 
    unzip 
    libzip-dev 
    nginx

COPY nginx.conf /etc/nginx/nginx.conf

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd xml zip

RUN pecl install -o -f redis 
    &&  rm -rf /tmp/pear 
    &&  docker-php-ext-enable redis

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -d /home/hridoy hridoy
RUN mkdir -p /home/hridoy/.composer && 
    chown -R hridoy:hridoy /home/hridoy

# Set working directory
WORKDIR /var/www

ADD . .

RUN composer install --no-interaction

USER hridoy

I am using a custom nginx config file and here is the file

server {
    listen 80;
    root /var/www/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

When I run docker build command, it builds the image successfully and I can also run the image using docker run -p 8080:80 app
But when I go to localhost:8080, I cannot access the website. The browser gives me the error-

This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

Source: Laravel

Leave a Reply