Daftar Isi:

#membuat Dockerfile

berikut adalah script untuk dockerfile versi saya sendiri


FROM php:8.2-fpm

COPY composer.* /var/www/laraveldocker

RUN rm -rf /var/www/laraveldocker

WORKDIR /var/www/laraveldocker

# Install required packages and Imagick dependencies
RUN apt-get update && apt-get install -y \
  build-essential \
  libmcrypt-dev \
  mariadb-client \
  libpng-dev \
  libjpeg62-turbo-dev \
  libfreetype6-dev \
  locales \
  jpegoptim optipng pngquant gifsicle \
  vim \
  nano \
  unzip \
  git \
  curl \
  libzip-dev \
  zip \
  libmagickwand-dev \
  imagemagick \
  gnupg2 # For adding Node.js PPA

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

RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs

RUN docker-php-ext-install pdo pdo_mysql gd zip mysqli && \
    pecl install imagick && \
    docker-php-ext-enable imagick

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

RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

COPY . .
COPY --chown=www:www . .

RUN chown -R www:www /var/www/laraveldocker/storage \
  && chmod -R 775 /var/www/laraveldocker/storage \
  && mkdir -p /var/www/laraveldocker/storage/framework/views \
  && chown -R www:www /var/www/laraveldocker/storage/framework/views


USER www

EXPOSE 9000

CMD ["php-fpm"]

#membuat docker-compose.yml

sebelum menulis docker-compose.yml buat 2 file di salam folder storage app.conf dan php.ini untuk isian file app.conf seperti ini:

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/laraveldocker/public;

	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 / {
		try_files $uri $uri/ /index.php?$query_string;
		gzip_static on;
	}
}

sedangkan unutk isian php.ini seperti ini:

upload_max_filesize = 40M
post_max_size = 40M
max_execution_time = 180
memory_limit = 3000M

disini saya akan menggunakan nginx sebagai webserver dan juga mysql sebagai databasenya. berikut ada script docker-compose yang saya buat

version: "3"
services:
    app:
        build: .
        image: urillldocker
        container_name: urillldocker
        restart: unless-stopped
        tty: true
        environment:
            SERVICE_NAME: app
        working_dir: /var/www/laraveldocker
        volumes:
            - .:/var/www/laraveldocker
            - ./storage/php.ini:/usr/local/etc/php/conf.d/local.ini
        networks:
            - laraveldocker
    webserver:
        image: nginx:alpine
        container_name: nginxlaraveldocker
        restart: unless-stopped
        tty: true
        ports:
            - "2024:80"
        volumes:
            - .:/var/www/laraveldocker
            - ./storage/:/etc/nginx/conf.d/
        networks:
            - laraveldocker
    db:
        image: mysql:5.7
        container_name: dblaraveldocker
        restart: always
        tty: true
        ports:
            - "2025:3306"
        volumes:
            - lbdata:/var/lib/mysql
        environment:
            MYSQL_PASSWORD: urilll
            MYSQL_ROOT_PASSWORD: urilll
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
        networks:
            - laraveldocker
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: pmalaraveldocker
        links:
            - db
        restart: always
        ports:
            - 7000:80
        networks:
            - laraveldocker
networks:
    laraveldocker:
        driver: bridge
volumes:
    lbdata:
        driver: local

#menambahkan banyak server di PhpMyAdmin Docker

Untuk menambahkan pilihan server di phpMyAdmin (PMA) sehingga Anda bisa mengelola banyak database dari berbagai server, Anda perlu mengonfigurasi file config.inc.php di container phpMyAdmin. Sayangnya, file ini tidak bisa diakses langsung melalui docker-compose.yml, tapi Anda bisa mengatasinya dengan membuat file konfigurasi sendiri dan memasukkannya ke dalam container.

Berikut adalah langkah-langkah untuk menambahkan pilihan server di phpMyAdmin:

Buat file config.user.inc.php di direktori lokal Anda:

Buat sebuah file config.user.inc.php di direktori yang sama dengan file docker-compose.yml Anda. Isi file ini dengan konfigurasi server-server yang ingin ditambahkan ke phpMyAdmin:

<?php
// Example servers configuration for multiple databases

$i = 1;

// First server
$cfg['Servers'][$i]['host'] = 'db'; // Default database server
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'urilll';
$cfg['Servers'][$i]['auth_type'] = 'config';
$i++;

// Second server
$cfg['Servers'][$i]['host'] = 'another_db_server'; // Ganti dengan nama atau IP server lain
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'another_password';
$cfg['Servers'][$i]['auth_type'] = 'config';
$i++;

// Tambahkan server lainnya dengan cara yang sama

untuk di docker-composer.yml nya seperti ini kurang lebihnya

phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: pmaprinthub
        links:
            - db
        restart: always
        ports:
            - 7000:80
        volumes:
            - ./config.user.inc.php:/etc/phpmyadmin/config.user.inc.php
        networks:
            - laraveldocker

#instalasi nodejs via Dockerfile

berikut adalah node js v20.xx karena kebetulan saya sedang membutuhkan nodejs v20

RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs

#record domain ke laravel docker

  1. ubah settingan nginx di docker-compose.yml menjadi seperti ini kurang lebihnya
services:
  webserver:
    image: nginx:alpine
    container_name: nginxprinthub
    restart: unless-stopped
    tty: true
    ports:
      - "80:80" # Atur agar Nginx mendengar di port 80 untuk semua domain
    volumes:
      - .:/var/www/laraveldocker
      - ./nginx/conf.d/:/etc/nginx/conf.d/ # Perbarui volume untuk Nginx config
    networks:
      - laraveldocker
  1. Buat dua file Nginx config Buat dua file konfigurasi di dalam folder nginx/conf.d/, satu untuk aplikasi dan satu untuk phpMyAdmin. File untuk aplikasi: nginx/conf.d/testing.printhub.my.id.conf
server {
    listen 80;
    server_name testing.printhub.my.id;
    
    root /var/www/laraveldocker/public;
    index index.php index.html;
    
    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;
    }
}

#install certbot di laravel docker

sudo apt install certbot
sudo certbot --nginx -d testing.printhub.my.id -d database.printhub.my.id