Kali ini saya pilih project yang saya forked dari github hako-975 - aplikasi uang kas dengan php native. Saya coba update di bagian insert saja karena command insert dengan increment ga bisa null (’’) tapi harus di isi dengan DEFAULT

Kita menggunakan image yang sudah terinstall php-fpm dan nginx di buat berdasarkan dari image docker-php-nginx Trafex, jadi nanti tinggal di reverse proxy nginx di containernya. Langsung saja, pertama buat Dockerfile

FROM alpine:3.12
LABEL Maintainer="Tim de Pater <code@trafex.nl>" \
      Description="Lightweight container with Nginx 1.18 & PHP-FPM 7.3 based on Alpine Linux." \
      Modified="ipang"

# Install packages and remove default server definition
RUN apk --no-cache add php7 php7-fpm php7-opcache php7-mysqli php7-json php7-openssl php7-curl \
    php7-zlib php7-xml php7-phar php7-intl php7-dom php7-xmlreader php7-ctype php7-session \
    php7-mbstring php7-gd php7-pdo php7-pdo_mysql nginx supervisor curl tzdata && \
    rm /etc/nginx/conf.d/default.conf

# change timezone to Jakarta (WIB)
RUN cp /usr/share/zoneinfo/Asia/Jakarta /etc/localtime
RUN echo "Asia/Jakarta" > /etc/timezone

# Configure nginx
COPY docker/nginx.conf /etc/nginx/nginx.conf

# Configure PHP-FPM
COPY docker/fpm-pool.conf /etc/php7/php-fpm.d/www.conf
COPY docker/php.ini /etc/php7/conf.d/custom.ini

# Configure supervisord
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Setup document root
RUN mkdir -p /var/www/html

# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /var/www/html && \
  chown -R nobody.nobody /run && \
  chown -R nobody.nobody /var/lib/nginx && \
  chown -R nobody.nobody /var/log/nginx

# Switch to use a non-root user from here on
USER nobody

# Add application
WORKDIR /var/www/html
COPY --chown=nobody . /var/www/html/

# Expose the port nginx is reachable on
EXPOSE 8080

# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

# Configure a healthcheck to validate that everything is up&running
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping

Kedua buat docker-compose.yml

version: "2"

services:
   uang-kas:
      image: uang-kas-img
      container_name: uang-kas-ctr
      build: .
      restart: always
      ports:
         - 8080:8080
      depends_on:
         - uang-kas-mysql
      networks:
         - uang-kas-network

   uang-kas-mysql:
       image: mysql:5.7
       container_name: uang-kas-mysql-ctr
       ports:
           - 3307:3306
       environment:
           - MYSQL_ROOT_PASSWORD=19second
           - MYSQL_USER=eren
           - MYSQL_PASSWORD=wanttorevenge
           - MYSQL_DATABASE=uang_kas
       volumes:
           - uang-kas-mysql-vol:/var/lib/mysql
       networks:
           - uang-kas-network

volumes:
    uang-kas-mysql-vol:
networks:
    uang-kas-network:
        driver: bridge

Buat config supervisor nya, ini berguna agar bisa menjalankan dua aplikasi secara bersamaan (di sini php-fpm dan nginx)

[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid

[program:php-fpm]
command=php-fpm7 -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

Di dalam folder docker juga terdapat config nginx.conf, fpm-pool.conf & php.ini, silahkan dimodifikasi sesuai kebutuhan kalian. Sekarang tinggal docker-compose up lalu import sql nya docker exec -i uang-kas-mysql-ctr mysql -u root -p19second uang_kas < uang_kas.sql

Yes, kontener nya jalan

Yes, kontener nya jalan

Note:

  1. config ini sudah di coba di 3 PHP native dan aplikasi berbasis Code Igniter, semua berhasil berjalan, tidak ada kendala
  2. CRUD saya kok ga jalan? di saya ini biasanya karena masalah koneksi ke DB nya
  3. Routing nya kok ngaco? mau ke menu ini kok ga bisa di klik? permasalahan ini biasanya di nginx.conf, biasanya di bagian try_files

Jika kamu merasa tulisan ini bermanfaat & membantu kamu, kamu bisa berdonasi lewat saweria

If you feel this website help you, you can donate at saweria