Skip to content

Instantly share code, notes, and snippets.

@autotrof
Last active May 5, 2024 16:31
Show Gist options
  • Save autotrof/d4dce58cfcf39fc9f42e3ad058d19061 to your computer and use it in GitHub Desktop.
Save autotrof/d4dce58cfcf39fc9f42e3ad058d19061 to your computer and use it in GitHub Desktop.
docker for laravel full
* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1
version: '3.8'
networks:
global:
external: true
services:
app:
container_name: ${APP_ID}-app
labels:
- dev.orbstack.domains=*.${APP_ID}.orb.local
build:
context: .
dockerfile: nginx.dockerfile
args:
- BASE_IMAGE_VERSION=${PHP_VERSION}
- UID=${UID:-1000}
- GID=${GID:-1000}
# ports:
# - "80:80"
volumes:
- ./:/var/www/html:delegated
working_dir: /var/www/html
depends_on:
- php
- redis
- mysql
# - mailhog
networks:
- global
mysql:
container_name: ${APP_ID}-db
image: mariadb:10.6.16
command: --max_allowed_packet=32505856
restart: unless-stopped
tty: true
# ports:
# - "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
SERVICE_NAME: mysql
TZ: Asia/Jakarta
volumes:
- ./mysql:/var/lib/mysql
networks:
- global
adminer:
container_name: ${APP_ID}-adminer
image: adminer:latest
restart: unless-stopped
environment:
- ADMINER_PLUGINS=tinymce dump-json dump-xml dump-zip dump-alter tables-filter
- ADMINER_DESIGN=nette
networks:
- global
php:
image: php.dockerfile:${APP_ID}
container_name: ${APP_ID}-php
build:
context: .
dockerfile: php.dockerfile
args:
- BASE_IMAGE_VERSION=${PHP_VERSION}
- UID=${UID:-1000}
- GID=${GID:-1000}
# ports:
# - "9000:9000"
volumes:
- ./:/var/www/html:delegated
- ./php.ini:$PHP_INI_DIR/php.ini
networks:
- global
redis:
container_name: ${APP_ID}-redis
image: redis:alpine
restart: unless-stopped
command: >
--requirepass ${REDIS_PASSWORD}
# ports:
# - "6379:6379"
volumes:
- ./redis:/data
networks:
- global
composer:
image: php.dockerfile:${APP_ID}
volumes:
- ./:/var/www/html
- ./php.ini:$PHP_INI_DIR/php.ini
depends_on:
- php
entrypoint: [ 'composer', '--ignore-platform-reqs' ]
networks:
- global
cron:
image: php.dockerfile:${APP_ID}
depends_on:
- php
volumes:
- ./:/var/www/html
- ./crontab:/etc/crontabs/root
- ./php.ini:$PHP_INI_DIR/php.ini
entrypoint: [ 'crond', '-f' ]
networks:
- global
artisan:
image: php.dockerfile:${APP_ID}
volumes:
- ./:/var/www/html:delegated
- ./php.ini:$PHP_INI_DIR/php.ini
depends_on:
- php
entrypoint: [ 'php', '/var/www/html/artisan' ]
networks:
- global
supervisor:
build:
context: .
dockerfile: supervisor.dockerfile
args:
- BASE_IMAGE_VERSION=${PHP_VERSION}
- APP_ID=${APP_ID}
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: ${APP_ID}-supervisor
depends_on:
- php
volumes:
- ./:/var/www/html
- ./supervisor.conf:/etc/supervisor/conf.d/supervisor.conf
- ./php.ini:$PHP_INI_DIR/php.ini
networks:
- global
server {
listen 80;
index index.php index.html;
server_name _;
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 php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
FROM nginx:stable-alpine
ARG UID
ARG GID
ENV UID=${UID}
ENV GID=${GID}
# MacOS staff group's gid is 20, so is the dialout group in alpine linux. We're not using it, let's just remove it.
RUN delgroup dialout
RUN addgroup -g ${GID} --system laravel
RUN adduser -G laravel --system -D -s /bin/sh -u ${UID} laravel
RUN sed -i "s/user nginx/user laravel/g" /etc/nginx/nginx.conf
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
RUN mkdir -p /var/www/html
ARG BASE_IMAGE_VERSION
FROM php:${BASE_IMAGE_VERSION}-fpm-alpine
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN sed -i "s/user = www-data/user = root/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = root/g" /usr/local/etc/php-fpm.d/www.conf
RUN echo "php_admin_flag[log_errors] = on" >> /usr/local/etc/php-fpm.d/www.conf
RUN docker-php-ext-install pdo pdo_mysql
RUN mkdir -p /usr/src/php/ext/redis \
&& curl -L https://github.com/phpredis/phpredis/archive/5.3.4.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis
RUN apk add --no-cache zip libzip-dev
RUN docker-php-ext-configure zip
RUN docker-php-ext-install zip
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY php.ini "$PHP_INI_DIR/conf.d/custom.ini"
RUN apk add git curl
USER root
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]
memory_limit = 2048M
upload_max_filesize=20M
max_input_vars=20000
post_max_size=20M
SERVICE="$1"
shift
ARGS="$@"
# Jalankan perintah docker-compose
docker-compose run --rm "$SERVICE" $ARGS
[supervisord]
nodaemon=true
[supervisorctl]
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --sleep=3
autostart=true
autorestart=true
user=www-data
numprocs=7
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
ARG APP_ID
FROM php.dockerfile:${APP_ID}
RUN apk add --no-cache supervisor
# Start Supervisor
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisor.conf"]
@autotrof
Copy link
Author

autotrof commented May 3, 2024

  1. create .env file
  2. make sure there are these variables in .env file:
  • PHP_VERSION=8.3
  • APP_ID=yourappid
  • DB_DATABASE=yourdbname
  • DB_PASSWORD=yourdbpass
  • REDIS_PASSWORD=yourredispass
  1. create docker network named "global" or other name, make sure it same as in docker-compose.yml
  2. run docker compose up -d php mysql redis app cron supervisor adminer mailhog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment