{"id":139,"date":"2024-01-31T08:11:00","date_gmt":"2024-01-31T08:11:00","guid":{"rendered":"https:\/\/emka.web.id\/tutorial\/2024\/01\/31\/inilah-contoh-dockerfile-dan-docker-compose-untuk-aplikasi-laravel\/"},"modified":"2024-01-31T08:11:00","modified_gmt":"2024-01-31T08:11:00","slug":"inilah-contoh-dockerfile-dan-docker-compose-untuk-aplikasi-laravel","status":"publish","type":"post","link":"https:\/\/emka.web.id\/en\/2024\/01\/inilah-contoh-dockerfile-dan-docker-compose-untuk-aplikasi-laravel.html","title":{"rendered":"INILAH Contoh Dockerfile dan Docker Compose untuk Aplikasi Laravel"},"content":{"rendered":"<p>&nbsp;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\u2014teknologi kontainerisasi terkemuka. Panduan ini mencakup langkah-langkah untuk mengintegrasikan MySQL dan mengonfigurasi Nginx, memastikan proses pengembangan yang mulus.<\/p>\n<p>\n  \n<\/p>\n<div style=\"clear: both;text-align: center\"><a href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEgTsrzmlXHVfXSimzdop1esk2jSSiX5S_c2jEbYu1qypIwD35dQD88diZ2jrYXEhXsBlQ3wEZ6KrSq5lZ4OqPR3lLuwZUL64uZmMMbCFIJdgpVDxWG7YqNk1dN2ANk0SVEXSJzRCqLxGzzcc5AyrK1xvzLu64hrU7pWJU6VxhRl-2zo3ZAbA083ShxXQuk\/s2200\/laravel-10-featured.webp\" style=\"margin-left: 1em;margin-right: 1em\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"1100\" data-original-width=\"2200\" height=\"320\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEgTsrzmlXHVfXSimzdop1esk2jSSiX5S_c2jEbYu1qypIwD35dQD88diZ2jrYXEhXsBlQ3wEZ6KrSq5lZ4OqPR3lLuwZUL64uZmMMbCFIJdgpVDxWG7YqNk1dN2ANk0SVEXSJzRCqLxGzzcc5AyrK1xvzLu64hrU7pWJU6VxhRl-2zo3ZAbA083ShxXQuk\/w640-h320\/laravel-10-featured.webp\" width=\"640\" \/><\/a><\/div>\n<p><\/p>\n<h2 style=\"text-align: left\">Persyaratan<\/h2>\n<p>Pengetahuan dasar tentang Laravel, Docker, dan MySQL.<\/p>\n<p>Docker dan Docker Compose terinstal di mesin Anda.<\/p>\n<p>\n  \n<\/p>\n<p><span><a name=\"more\"><\/a><\/span><\/p>\n<h4 style=\"text-align: left\">Langkah 1: Menyiapkan Proyek Laravel<\/h4>\n<p>Pertama, buat proyek Laravel baru atau masuk ke direktori proyek yang sudah ada. Jika membuat proyek baru, gunakan Composer:<\/p>\n<p>\n  \n<\/p>\n<p>composer create-project &#8211;prefer-dist laravel\/laravel my-laravel-app<\/p>\n<p>cd my-laravel-app<\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 2: Membuat Dockerfile<\/h4>\n<p>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.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">FROM php:8.2-fpm<\/span><\/p>\n<p><span style=\"font-family: courier\"># Install dependencies<\/span><\/p>\n<p><span style=\"font-family: courier\">RUN apt-get update &amp;&amp; apt-get install -y <\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; libpng-dev <\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; libonig-dev <\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; libxml2-dev <\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; zip <\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; unzip<\/span><\/p>\n<p><span style=\"font-family: courier\"># Clear cache<\/span><\/p>\n<p><span style=\"font-family: courier\">RUN apt-get clean &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\"># Install PHP extensions<\/span><\/p>\n<p><span style=\"font-family: courier\">RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd<\/span><\/p>\n<p><span style=\"font-family: courier\"># Get latest Composer<\/span><\/p>\n<p><span style=\"font-family: courier\">COPY &#8211;from=composer:latest \/usr\/bin\/composer \/usr\/bin\/composer<\/span><\/p>\n<p><span style=\"font-family: courier\"># Expose port 9000 and start php-fpm server<\/span><\/p>\n<p><span style=\"font-family: courier\">EXPOSE 9000<\/span><\/p>\n<p><span style=\"font-family: courier\">CMD [&#8220;php-fpm&#8221;]<\/span><\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 3: Menyiapkan Docker Compose<\/h4>\n<p>Buat file &#8216;docker-compose.yml&#8217; 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.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">version: &#8216;3&#8217;<\/span><\/p>\n<p><span style=\"font-family: courier\">services:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; app:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; build:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; context: .<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; dockerfile: Dockerfile<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; image: my-laravel-app<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; container_name: my-laravel-app<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; restart: unless-stopped<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; tty: true<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; environment:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; SERVICE_NAME: my-laravel-app<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; SERVICE_TAGS: dev<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; working_dir: \/var\/www\/html<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; volumes:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; .\/:\/var\/www\/html<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; networks:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; app-network<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; db:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; image: mysql:8<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; container_name: my-laravel-mysql<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; restart: unless-stopped<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; tty: true<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; ports:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; &#8220;13306:3306&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; environment:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; MYSQL_DATABASE: laravel<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; MYSQL_USER: user<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; MYSQL_PASSWORD: password<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; MYSQL_ROOT_PASSWORD: password<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; SERVICE_TAGS: dev<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; volumes:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; dbdata:\/var\/lib\/mysql<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; networks:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; app-network<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; web:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; image: &#8216;nginx:alpine&#8217;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; ports:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; &#8220;8000:80&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; volumes:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; .\/:\/var\/www\/html<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; .\/nginx.conf:\/etc\/nginx\/conf.d\/default.conf<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; depends_on:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; app<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; networks:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &#8211; app-network<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">networks:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; app-network:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; driver: bridge<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">volumes:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; dbdata:<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; driver: local<\/span><\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 4: Mengimplementasikan Konfigurasi Nginx<\/h4>\n<p>Tempatkan file &#8216;nginx.conf&#8217; di akar proyek untuk mengonfigurasi server Nginx, yang penting untuk melayani aplikasi Anda.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">server {<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; listen 80;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; index index.php index.html;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; error_log&nbsp; \/var\/log\/nginx\/error.log;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; access_log \/var\/log\/nginx\/access.log;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; root \/var\/www\/html\/public;<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; location \/ {<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; try_files $uri $uri\/ \/index.php?$query_string;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; }<\/span><\/p>\n<p>\n  <span style=\"font-family: courier\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; location ~ .php$ {<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; try_files $uri =404;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; fastcgi_split_path_info ^(.+.php)(\/.+)$;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; fastcgi_pass app:9000;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; fastcgi_index index.php;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; include fastcgi_params;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; fastcgi_param PATH_INFO $fastcgi_path_info;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; }<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; location ~ \/.ht {<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; &nbsp; &nbsp; deny all;<\/span><\/p>\n<p><span style=\"font-family: courier\">&nbsp; &nbsp; }<\/span><\/p>\n<p><span style=\"font-family: courier\">}<\/span><\/p>\n<h4 style=\"text-align: left\">Langkah 5: Menjalankan Kontainer Anda<\/h4>\n<p>Dengan Dockerfile dan docker-compose.yml yang sudah disiapkan, Anda dapat memulai kontainer Anda.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">docker-compose up -d<\/span><\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 6: Mengonfigurasi Database Laravel<\/h4>\n<p>\n  \n<\/p>\n<p>Ubah file .env di proyek Laravel Anda untuk menggunakan layanan MySQL.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">DB_CONNECTION=mysql<\/span><\/p>\n<p><span style=\"font-family: courier\">DB_HOST=db<\/span><\/p>\n<p><span style=\"font-family: courier\">DB_PORT=3306<\/span><\/p>\n<p><span style=\"font-family: courier\">DB_DATABASE=laravel<\/span><\/p>\n<p><span style=\"font-family: courier\">DB_USERNAME=user<\/span><\/p>\n<p><span style=\"font-family: courier\">DB_PASSWORD=password<\/span><\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 7: Menjalankan Migrasi Database<\/h4>\n<p>Jalankan migrasi Laravel untuk menyiapkan database Anda.<\/p>\n<p>\n  \n<\/p>\n<p><span style=\"font-family: courier\">docker-compose exec app php artisan migrate<\/span><\/p>\n<p>\n  \n<\/p>\n<h4 style=\"text-align: left\">Langkah 8: Mengakses Aplikasi<\/h4>\n<p>Setelah kontainer berjalan, Anda dapat mengakses aplikasi Laravel di http:\/\/localhost:8000.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;Pengembangan aplikasi Laravel memerlukan lingkungan pengembangan yang stabil dan konsisten. Docker, platform kontainerisasi populer, menawarkan cara efisien untuk membuat lingkungan seperti itu. Dalam tutorial&#8230;<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":0,"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"wp:attachment":[{"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emka.web.id\/en\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}