Dockerize Laravel App for Production

deploying laravel app using docker on production server#

As we have already created a development version of laravel using docker now we will create a production version. most of the things will be same so m not going to repeat all those things again. if you do not know how to create docker file for development read this blog post.

Laravel using docker for development

In the preview post we have used php:8.0-fpm for development version and for running it we have used laravels php artisan serve command which start server on port 8000.

running using php cli is not that performant instead we can use mod_php for apache or php-fpm using nginx.

Whenever there is web server attached then multiple request getting hangled by the web server it self, and web server can launch multple php process to handle multiple request.

That’s why it is necessary to use web server for your project in production. i am going to use php:8.0-apache docker image for deploying php.

for that we also have to set document root and enable some modules like a2enmod rewrite

Our docker file will look like this


FROM php:8.0-apache
WORKDIR /var/www/html
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN apt-get update && apt-get install -y  \
    libfreetype6-dev \
    libjpeg-dev \
    libpng-dev \
    libwebp-dev \
    libzip-dev \
    --no-install-recommends \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql -j$(nproc) gd zip 
RUN curl -sS | php -- --install-dir=/usr/local/bin --filename=composer

As you know in previous post we have created a post script which is for post commnads after all the services started then that will execute.


version: '3'
      context: ./
      # - ./src:/src
      - ./src:/var/www/html
    command: bash -c "sh"

      - "8000:80"

As you can see we have only changed the volume section from ./src to ./var/www/html

In your file add command to enable mode rewrite and to start the apache server

composer install
a2enmod rewrite
exec docker-php-entrypoint apache2-foreground

That’s it this post if you have any doubt you can comment.

comments powered by Disqus