Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active November 9, 2019 15:24
Show Gist options
  • Save afiqiqmal/0547fe5a23851cfa07181fc81c25ab65 to your computer and use it in GitHub Desktop.
Save afiqiqmal/0547fe5a23851cfa07181fc81c25ab65 to your computer and use it in GitHub Desktop.
Dockerfile for laravel application

Dockerfile for laravel

Just run this command
docker build -t laraveldev . --build-arg WHAT_ENVIRONMENT=local --build-arg PROJECT_FOLDER_NAME=my_laravel_docker

WHAT_ENVIRONMENT - indicate the folder of config
PROJECT_FOLDER_NAME - indicate the project folder

FROM webdevops/php-nginx:7.2
ARG WHAT_ENVIRONMENT=local
ARG PROJECT_FOLDER_NAME=laravel_app
LABEL maintainer=hafiq
LABEL email=hafiq@terato.com
#####################################
# Software Installation #
#####################################
# Install all the necessary libraries
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
# Install necessary softwares
RUN apt-get update \
&& apt-get -y -qq install \
autoconf automake \
libmcrypt-dev libmagickwand-dev libsodium-dev libhiredis-dev uuid-dev
# Install necessary softwares
RUN apt-get update \
&& apt-get install -y -qq \
build-essential cmake git systemd systemd-sysv vim wget yasm \
re2c file jpegoptim texinfo optipng pngquant gifsicle mercurial pkg-config
# Install necessary softwares
RUN apt-get update \
&& apt-get install -y -qq \
redis-server logrotate rsyslog ufw mysql-server iputils-ping nginx-extras
# Install needed extension
RUN yes | pecl install -s mcrypt-1.0.1
RUN yes | pecl install -s uuid
RUN yes | pecl install -s uopz \
&& echo "extension=uopz.so" > /usr/local/etc/php/conf.d/uopz.ini
# Configure xDebug
RUN yes | pecl install -s xdebug-2.7.2 \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini
# Install extra PHP Extensions
RUN docker-php-ext-install sodium
RUN docker-php-ext-enable xdebug opcache gd mcrypt imagick redis sodium uuid
# Clean up all the mess done by installing stuff
RUN apt-get autoremove --purge -y software-properties-common \
autoconf automake build-essential cmake mercurial texinfo \
&& apt-get clean \
&& apt-get autoclean \
&& echo -n > /var/lib/apt/extended_states \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /usr/share/man/?? \
&& rm -rf /usr/share/man/??_*
#####################################
# Docker Configuration #
#####################################
# Remove every webdevops nginx conf
RUN echo "" > /opt/docker/etc/nginx/vhost.conf
RUN rm /opt/docker/etc/nginx/vhost.common.d/*.conf
# Remove webdevops syslog
RUN find /opt/docker/ -type f -name "*syslog*" -exec rm -rf {} \; ||:
RUN find /opt/docker/ -type d -name "syslog*" -exec rm -rf {} \; ||:
RUN rm /opt/docker/bin/service.d/syslog-ng.d/10-init.sh ||:
# adjust php ini
COPY /docker/laravel/php-conf/local.ini /opt/docker/etc/php/conf.d/local.ini
COPY /docker/laravel/php-conf/lara-www.conf /usr/local/etc/php-fpm.d/crismobile.conf
# Enable Gzip Compression
COPY /docker/laravel/nginx/gzip.conf /opt/docker/etc/nginx/conf.d/gzip.conf
# remove existing default nginx and copy default nginx conf
RUN rm /etc/nginx/sites-enabled/default ||:
RUN rm /etc/nginx/sites-available/default ||:
COPY /docker/laravel/nginx/ais.conf /opt/docker/etc/nginx/conf.d/default.conf
# Add crontab file in the cron directory
RUN touch /var/log/laracron.log
COPY /docker/laravel/cron /opt/docker/etc/cron/laracron
# Add workers for supervisor
COPY /docker/laravel/rsyslog/rsyslog-worker.conf /opt/docker/etc/supervisor.d/rsyslog-worker.conf
COPY /docker/laravel/supervisor/horizon.conf /opt/docker/etc/supervisor.d/horizon-worker.conf
# rsyslog config
COPY /docker/laravel/rsyslog/rsyslog.conf /etc/rsyslog.d/rsyslog.conf
#####################################
# Project Install #
#####################################
WORKDIR /app
RUN chown root:root /app
RUN mkdir ${PROJECT_FOLDER_NAME}
# copy project directory to docker directory
COPY --chown=www-data:www-data . ${PROJECT_FOLDER_NAME}
WORKDIR /app/${PROJECT_FOLDER_NAME}
# copy docker .env to root .env
COPY /docker/laravel/environment/${WHAT_ENVIRONMENT}/.env .env
# Clean up folder which suppose not to be in docker
RUN rm bootstrap/cache/*.php ||:
RUN rm -rf .idea/ ||:
RUN rm -rf .git/ ||:
RUN rm .gitlab-ci.yml ||:
RUN rm -rf docker/ ||:
RUN rm -rf vendor/ ||:
RUN rm -rf node_modules/ ||:
RUN unlink public/storage ||:
# Composer install
RUN composer global require hirak/prestissimo
RUN composer install --no-interaction --no-progress
# fix www-data permission
RUN usermod -a -G www-data www-data
RUN usermod -a -G www-data root
RUN usermod -a -G www-data application
WORKDIR /app
RUN chown -R application:www-data ${PROJECT_FOLDER_NAME}
# Change folder directory permission
RUN find crismobile -type f -exec chmod 664 {} \;
RUN find crismobile -type d -exec chmod 775 {} \;
WORKDIR /app/${PROJECT_FOLDER_NAME}
# give the webserver the rights to read and write to storage and cache
RUN chgrp -R www-data storage bootstrap/cache
RUN chmod -R ug+rwx storage bootstrap/cache
# check if migration is exist and migrate
RUN php artisan migrate --force;
# run seeder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment