Manual Docker For Laravel Apps(LAMP)

Laravel sebenarnya sudah memiliki docker template tersendiri, Sail namanya kalau tidak salah. Cukup mudah memakainya karena pada dasarnya sudah terintegrasi dengan Artisan command dari Laravel.

Hanya saja, saya lebih suka membuat sendiri karena pasti ada ilmu lebih daripada sekedar asal langsung pakai.

Berikut adalah template dockerfile yang saya gunakan untuk melakukan development keseharian.

FROM php:8.0-apache

# Initial PHP extension installation
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libonig-dev \
        zlib1g-dev \
        libzip-dev \
        libicu-dev \
        git \
        cron \
        curl \
        supervisor \ 
        nano \
        apt-transport-https \
        libpq-dev \
        unzip libaio1 \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo \
    && docker-php-ext-install pdo_mysql exif pcntl mysqli intl \
    && pecl install -o -f zip &&  rm -rf /tmp/pear && docker-php-ext-enable zip \
    && docker-php-ext-install pdo pdo_pgsql pgsql \
    && pecl install -o -f redis &&  rm -rf /tmp/pear && docker-php-ext-enable redis 

# Install nodeJS for assetes compile if required(skip it if you don't ahve to compile anything)
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt update && apt-get install -y nodejs

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


# copy additional php ini values (seperti timezone, max limit upload etc) - contoh lihat di bawah
COPY php-overide.ini /usr/local/etc/php/conf.d/php-overide.ini

# copy virtualhost - Contoh lihat di bawah
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

#aktifin mod rewrite(requirted by laravel)
RUN a2enmod rewrite

# Install Composer, skip if you don have plan to use composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /var/www/app/

Berikut adalah file “php-overide.ini” yang mana berisi konfigurasi kustom untuk php.ini yang henndak saya pakai(biasanya tuh isinya seperti max upload size, timezone etc). Isinya terserah Anda.

#Error display
display_errors = On

#upload override
post_max_size = 2500M
upload_max_filesize = 2400M

# Default timezone
date.timezone = Asia/Jakarta

# Max execution time
max_execution_time = 60
max_input_vars = 5000

Kemudian sisanya adalah virtualhost untuk si Apache

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin [email protected]
	DocumentRoot /var/www/app/public

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/app/public">
        AllowOverride All
    </Directory>

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf

</VirtualHost>
Content folder

Setelah semua file konfigurasi disiapkan(asumsi susunan folder seperti gambar di atas), sekarang saatnya melakukan build dengan perintah:

docker build -t lamp-laravel:latest .

Lalu jalankan docker dengan command

docker run -itd --name [container-name] -v ./laravel-app/:/var/www/app/ lamp-laravel

Leave a Reply

Your email address will not be published. Required fields are marked *