Page cover

Laravel

Dockerize Laravel Application

Menyiapkan File Docker

Siapkan beberapa file docker yang diperlukan

  • nginx.conf: file konfigurasi dari nginx

  • Dockerfile: berisi file konfigurasi dari docker

  • .dockerignore: list file dan directory yang diabaikan

  • docker-compose.yml: untuk mempermudah menjalankan container

nginx.conf

Buat directory .config terlebih dahulu, kemudian buat file nginx.conf

nginx.conf
server {
    listen 5000;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}

Dockerfile

Pada directory utama, buat file Dockerfile

Dockerfile
# Gunakan image PHP dengan PHP-FPM
FROM php:7.4-fpm

# Instal ekstensi PHP yang diperlukan
RUN apt-get update && apt-get install -y \
    nano \
    nginx \
    git \
    unzip \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    zip \
    libzip-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd pdo pdo_mysql zip

# Instal Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Set folder kerja
WORKDIR /var/www
COPY . /var/www
RUN chown -R www-data:www-data /var/www && \
    find /var/www/ -type d -exec chmod 755 {} \; && \
    find /var/www/ -type f -exec chmod 644 {} \; && \
    chmod 777 -R storage/

RUN chmod 600 -R .config/

RUN composer install
RUN php artisan optimize

EXPOSE 5000

COPY .config/default.conf /etc/nginx/sites-available/default
CMD service nginx start && php-fpm

docker-compose.yml

Isi file docker-compose.yml

docker-compose.yml
version: '3'
services:
  webapp:
    build: .
    image: laravel-deploy
    container_name: laravel-deploy
    ports:
      - "5000:5000"

.dockerignore

Isi file .dockerignore

.dockerignore
.git
.gitignore
.dockerignore
Dockerfile
docker-compose.yml

Menjalankan Container

Build image dan jalankan container

docker compose up -d --build

Seharusnya aplikasi sudah bisa berjalan, buka browser dan akses IP server dengan port 5000 (http://127.0.0.1:5000). Untuk menggunakan port lain, ganti setiap port pada file nginx.conf, Dockerfile, dan docker-compose.yml

Last updated