Skip to content
Tutorial emka
Menu
  • Home
  • Debian Linux
  • Ubuntu Linux
  • Red Hat Linux
Menu

INILAH Contoh Dockerfile dan Docker Compose untuk Aplikasi Laravel

Posted on January 31, 2024

 Pengembangan aplikasi Laravel memerlukan lingkungan pengembangan yang stabil dan konsisten. Docker, platform kontainerisasi populer, menawarkan cara efisien untuk membuat lingkungan seperti itu. Dalam tutorial komprehensif ini, kita akan menjelajahi cara menetapkan lingkungan pengembangan Laravel yang kuat menggunakan Docker—teknologi kontainerisasi terkemuka. Panduan ini mencakup langkah-langkah untuk mengintegrasikan MySQL dan mengonfigurasi Nginx, memastikan proses pengembangan yang mulus.

Persyaratan

Pengetahuan dasar tentang Laravel, Docker, dan MySQL.

Docker dan Docker Compose terinstal di mesin Anda.

Langkah 1: Menyiapkan Proyek Laravel

Pertama, buat proyek Laravel baru atau masuk ke direktori proyek yang sudah ada. Jika membuat proyek baru, gunakan Composer:

composer create-project –prefer-dist laravel/laravel my-laravel-app

cd my-laravel-app

Langkah 2: Membuat Dockerfile

Buat Dockerfile di akar proyek Laravel Anda. File ini menggunakan gambar PHP 8.2 dan menginstal ekstensi PHP yang diperlukan bersama dengan Composer. Ini menetapkan lingkungan aplikasi Laravel Anda.

FROM php:8.2-fpm

# Install dependencies

RUN apt-get update && apt-get install -y

    libpng-dev

    libonig-dev

    libxml2-dev

    zip

    unzip

# Clear cache

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

# Get latest Composer

COPY –from=composer:latest /usr/bin/composer /usr/bin/composer

# Expose port 9000 and start php-fpm server

EXPOSE 9000

CMD [“php-fpm”]

Langkah 3: Menyiapkan Docker Compose

Buat file ‘docker-compose.yml’ di akar proyek Anda. File ini mengatur kontainer Docker Anda, menentukan layanan untuk Laravel (app), MySQL (db), dan Nginx (web). Ini mendirikan jaringan untuk layanan ini dan volume untuk MySQL.

version: ‘3’

services:

  app:

    build:

      context: .

      dockerfile: Dockerfile

    image: my-laravel-app

    container_name: my-laravel-app

    restart: unless-stopped

    tty: true

    environment:

      SERVICE_NAME: my-laravel-app

      SERVICE_TAGS: dev

    working_dir: /var/www/html

    volumes:

      – ./:/var/www/html

    networks:

      – app-network


  db:

    image: mysql:8

    container_name: my-laravel-mysql

    restart: unless-stopped

    tty: true

    ports:

      – “13306:3306”

    environment:

      MYSQL_DATABASE: laravel

      MYSQL_USER: user

      MYSQL_PASSWORD: password

      MYSQL_ROOT_PASSWORD: password

      SERVICE_TAGS: dev

    volumes:

      – dbdata:/var/lib/mysql

    networks:

      – app-network


  web:

    image: ‘nginx:alpine’

    ports:

      – “8000:80”

    volumes:

      – ./:/var/www/html

      – ./nginx.conf:/etc/nginx/conf.d/default.conf

    depends_on:

      – app

    networks:

      – app-network


networks:

  app-network:

    driver: bridge


volumes:

  dbdata:

    driver: local

Langkah 4: Mengimplementasikan Konfigurasi Nginx

Tempatkan file ‘nginx.conf’ di akar proyek untuk mengonfigurasi server Nginx, yang penting untuk melayani aplikasi Anda.

server {

    listen 80;

    index index.php index.html;

    error_log  /var/log/nginx/error.log;

    access_log /var/log/nginx/access.log;

    root /var/www/html/public;


    location / {

        try_files $uri $uri/ /index.php?$query_string;

    }


    location ~ .php$ {

        try_files $uri =404;

        fastcgi_split_path_info ^(.+.php)(/.+)$;

        fastcgi_pass app:9000;

        fastcgi_index index.php;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param PATH_INFO $fastcgi_path_info;

    }

    location ~ /.ht {

        deny all;

    }

}

Langkah 5: Menjalankan Kontainer Anda

Dengan Dockerfile dan docker-compose.yml yang sudah disiapkan, Anda dapat memulai kontainer Anda.

docker-compose up -d

Langkah 6: Mengonfigurasi Database Laravel

Ubah file .env di proyek Laravel Anda untuk menggunakan layanan MySQL.

DB_CONNECTION=mysql

DB_HOST=db

DB_PORT=3306

DB_DATABASE=laravel

DB_USERNAME=user

DB_PASSWORD=password

Langkah 7: Menjalankan Migrasi Database

Jalankan migrasi Laravel untuk menyiapkan database Anda.

docker-compose exec app php artisan migrate

Langkah 8: Mengakses Aplikasi

Setelah kontainer berjalan, Anda dapat mengakses aplikasi Laravel di http://localhost:8000.

Selamat! Anda telah berhasil membuat lingkungan pengembangan Laravel berbasis Docker dengan integrasi MySQL dan konfigurasi Nginx. Penyiapan ini meningkatkan konsistensi di berbagai skenario pengembangan dan memudahkan kolaborasi tim. Untuk menyempurnakan lingkungan Anda, jelajahi fitur Docker tambahan dan kemampuan lanjutan Laravel.

Recent Posts

  •  How to Fix Windows 11 ISO Download Blocked and Error Messages
  • How to Make Your Website Vibrate with Web Haptics
  • Measuring LLM Bullshit Benchmark
  • A Step-by-Step Guide to ZITADEL Identity Infrastructure
  • How NVIDIA G-SYNC Pulsar is Finally Fixing Motion Blur Forever
  • How Multipathing Keeps Your Linux Systems Running Smoothly!
  • Forgejo: A Self-hosted Github Alternative You Should Try
  • Introducing Zo Computer, How it Will Changing Personal Data Science Forever
  • Which AI Brain Should Your Coding Agent Use? A Deep Dive into the OpenHands Index
  • Hoppscotch, The Postman Killer: Why You Should Switch from Postman to Hoppscotch Right Now
  • Nitrux 6.0 Released with Linux Kernel 6.19: What’s New?
  • How to Upgrade Pop!_OS 22.04 LTS to 24.04 LTS: A Step-by-Step Guide
  • KDE Plasma 6.6.2 Released: Key Bug Fixes and Enhancements Explained
  • Meet the Huawei NetEngine 8000: The Router Powering the Next Generation of AI-Driven Networks!
  • LLM Settings That Every AI Developer Must Know
  • Is Your Second Monitor a Mess? Kubuntu 26.04 Resolute Raccoon Finally Fixes Multi-Display Woes!
  • How to Run Massive AI Models on Your Mac: Unlocking Your Hidden VRAM Secrets
  • How to Create Gemini CLI Agent Skills
  • WTF? Ubuntu Planning Mandatory Age Verification
  • Why This Retro PC is Actually a Modern Beast: Maingear Retro98
  •  Windows 11 Taskbar Update: How to Move and Resize Your Taskbar Again
  • Does KDE Plasma Require Systemd? Debunking the Mandatory Dependency Myths
  •  How to Fix ‘docs.google.com Refused to Connect’ Error in Windows 10/11
  • Aerynos Feb 2026 Update: Faster Desktops and Moss Performance Boost
  • Pangolin 1.16 Adds SSH Auth Daemon: What You Need to Know
  • Inilah 10 Kesalahan Fatal Saat Beli Properti yang Bisa Bikin Kalian Bangkrut!
  • Belum Tahu Cara Masuk Simpatika Terbaru? Ini Cara Login PTK EMIS GTK IMP 2026 Supaya Cek TPG Jadi Lebih Gampang!
  • Inilah Cara Bikin Konten Animasi AI Cuma Modal HP Supaya Bisa Gajian Rutin dari YouTube
  • Inilah Alasan Kenapa Zakat ke Ormas yang Belum Diakui Negara Nggak Bisa Dipakai Buat Ngurangin Pajak!
  • Inilah Cara Belanja di Indomaret Pakai Shopee PayLater yang Praktis dan Bikin Hemat!
  • The Secret Reason Seedance 2.0 is Realistic
  • Exploring Microsoft Phi-4 Reasoning Vision 15B
  • Gemini 3.1 Flash-Lite Released: How to Master Google’s Fastest AI Model for Real-World Projects
  • Qwen Is Ruined! Why the Masterminds Behind Qwen 3.5 Left Alibaba Cloud
  • GPT-5.3 Instant Revealed: How the New OpenAI Update Changes Everything for AI Users
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025
©2026 Tutorial emka | Design: Newspaperly WordPress Theme