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

  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Clear Copilot Memory in Windows 11 Step by Step
  • How to Show Battery Percentage on Windows 11
  • How to Fix VMSp Service Failed to Start on Windows 10/11
  • How to Fix Taskbar Icon Order in Windows 11/10
  • How to Disable Personalized Ads in Copilot on Windows 11
  • What is the Microsoft Teams Error “We Couldn’t Connect the Call” Error?
  • Why Does the VirtualBox System Service Terminate Unexpectedly? Here is the Full Definition
  • Why is Your Laptop Touchpad Overheating? Here are the Causes and Fixes
  • How to Disable All AI Features in Chrome Using Windows 11 Registry
  • How to Avoid Problematic Windows Updates: A Guide to System Stability
  • What is Microsoft Visual C++ Redistributable and How to Fix Common Errors?
  • What is the 99% Deletion Bug? Understanding and Fixing Windows 11 File Errors
  • How to Add a Password to WhatsApp for Extra Security
  • How to Recover Lost Windows Passwords with a Decryptor Tool
  • How to Fix Python Not Working in VS Code Terminal: A Troubleshooting Guide
  • Game File Verification Stuck at 0% or 99%: What is it and How to Fix the Progress Bar?
  • Why Does PowerPoint Underline Hyperlinks? Here is How to Remove Them
  • AI Bug Hunting with Semgrep
  • What is the Excel Power Query 0xc000026f Error?
  • How to Build Your Own Homelab AI Supercomputer 2026
  • Inilah Lenovo Legion Y70 (2026), HP Gaming yang Siap Guncang Pasar dengan Teknologi AI
  • Inilah Rekomendasi HP Oppo Murah Spek Mewah Tahun 2026 yang Nggak Bakal Bikin Kantong Kalian Jebol!
  • Inilah 15 Situs Nonton Film Gratis Legal 2026, Bye-Bye Iklan Ganggu dan Drama Situs Ilegal!
  • Inilah Beasiswa Bekraf Digital Talent 2026: Solusi Buat Kalian yang Mau Jago AI dan Makin Produktif
  • Inilah Cara Memilih Power Bank yang Aman Dibawa ke Pesawat Agar Tidak Disita Petugas
  • How to use the Wan 2.7 AI model: A complete walkthrough
  • How to Run Minimax M2-7 Locally: A Step-by-Step Guide for Beginners
  • How to Build Self-Driving Codebases with Async Agents and NVIDIA Hardware
  • How to create a professional AI influencer using Apob AI
  • How to Use AI-Powered Tools to Level Up Your Stock Market Research Game with Claude Code
  • New Text Document
  • 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?
©2026 Tutorial emka | Design: Newspaperly WordPress Theme