feat: Create a Docker Image around the laravel app
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
# =========
|
||||
# Stage 1: Build (Composer)
|
||||
# =========
|
||||
FROM php:8.4-fpm-alpine AS build
|
||||
# Build deps
|
||||
RUN set -eux; \
|
||||
apk add --no-cache git unzip libzip-dev oniguruma-dev icu-dev autoconf build-base
|
||||
|
||||
# PHP extensions for Laravel
|
||||
RUN docker-php-ext-configure zip; \
|
||||
docker-php-ext-install -j"$(nproc)" pdo_mysql zip opcache intl
|
||||
|
||||
# Composer
|
||||
ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer
|
||||
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
|
||||
|
||||
# Use a neutral build path; we'll copy to /var/www/html later
|
||||
WORKDIR /app
|
||||
|
||||
# Composer layers
|
||||
COPY composer.json composer.lock ./
|
||||
RUN set -eux; \
|
||||
composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-scripts
|
||||
|
||||
# Copy the rest of the app (NO artisan caching here!)
|
||||
COPY . .
|
||||
|
||||
# =========
|
||||
# Stage 2: Runtime (everything runs as 'nginx' user)
|
||||
# =========
|
||||
FROM php:8.4-fpm-alpine
|
||||
|
||||
# Runtime packages
|
||||
RUN set -eux; \
|
||||
apk add --no-cache nginx supervisor curl libzip icu-libs; \
|
||||
mkdir -p /run/nginx /var/log/nginx /var/log/supervisor /etc/nginx/conf.d /var/www/html \
|
||||
/var/cache/nginx /var/lib/nginx/tmp
|
||||
|
||||
# PHP runtime extensions
|
||||
RUN set -eux; \
|
||||
apk add --no-cache libzip-dev icu-dev oniguruma-dev; \
|
||||
docker-php-ext-configure zip; \
|
||||
docker-php-ext-install -j"$(nproc)" pdo_mysql zip opcache intl; \
|
||||
apk del --no-progress --purge libzip-dev icu-dev oniguruma-dev || true
|
||||
|
||||
# PHP production ini + opcache
|
||||
RUN set -eux; \
|
||||
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \
|
||||
{ \
|
||||
echo "opcache.enable=1"; \
|
||||
echo "opcache.enable_cli=0"; \
|
||||
echo "opcache.memory_consumption=128"; \
|
||||
echo "opcache.max_accelerated_files=20000"; \
|
||||
echo "opcache.validate_timestamps=0"; \
|
||||
echo "realpath_cache_size=4096K"; \
|
||||
echo "realpath_cache_ttl=600"; \
|
||||
} >> "$PHP_INI_DIR/conf.d/99-opcache.ini"
|
||||
|
||||
# App into runtime image under final path; own by nginx
|
||||
WORKDIR /var/www/html
|
||||
COPY --from=build --chown=nginx:nginx /app /var/www/html
|
||||
|
||||
# Nginx + Supervisor configs + health + entrypoint
|
||||
COPY ./docker/nginx.conf /etc/nginx/nginx.conf
|
||||
COPY ./docker/site.conf /etc/nginx/conf.d/default.conf
|
||||
COPY ./docker/supervisord.conf /etc/supervisord.conf
|
||||
COPY ./docker/healthcheck.sh /usr/local/bin/healthcheck.sh
|
||||
COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/healthcheck.sh /usr/local/bin/entrypoint.sh
|
||||
|
||||
# Log to STDOUT/ERR
|
||||
RUN set -eux; \
|
||||
ln -sf /dev/stdout /var/log/nginx/access.log; \
|
||||
ln -sf /dev/stderr /var/log/nginx/error.log
|
||||
|
||||
# FPM: pass env, listen on TCP (non-root ok), run workers as nginx
|
||||
RUN set -eux; \
|
||||
sed -ri 's|^;?clear_env\s*=.*|clear_env = no|g' /usr/local/etc/php-fpm.d/www.conf; \
|
||||
sed -ri 's|^listen = .*|listen = 127.0.0.1:9000|g' /usr/local/etc/php-fpm.d/www.conf; \
|
||||
sed -ri 's|^user\s*=.*|user = nginx|g' /usr/local/etc/php-fpm.d/www.conf; \
|
||||
sed -ri 's|^group\s*=.*|group = nginx|g' /usr/local/etc/php-fpm.d/www.conf; \
|
||||
{ \
|
||||
echo "ping.path = /ping"; \
|
||||
echo "pm.status_path = /status"; \
|
||||
echo "catch_workers_output = yes"; \
|
||||
} >> /usr/local/etc/php-fpm.d/www.conf
|
||||
|
||||
# Ensure Laravel writable dirs exist & owned by nginx
|
||||
RUN set -eux; \
|
||||
mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache; \
|
||||
chown -R nginx:nginx storage bootstrap/cache /var/cache/nginx /var/lib/nginx /var/lib/nginx/tmp /run/nginx /var/log/nginx /var/log/supervisor; \
|
||||
chmod -R ug+rwX storage bootstrap/cache
|
||||
|
||||
# ENV / port
|
||||
ENV APP_ENV=production APP_DEBUG=false APP_URL=http://localhost APP_PORT=8080
|
||||
EXPOSE 8080
|
||||
|
||||
# Healthcheck
|
||||
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"]
|
||||
|
||||
# Run everything as nginx
|
||||
USER nginx:nginx
|
||||
|
||||
# Start via supervisor (which will first run entrypoint, then services)
|
||||
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
|
||||
@@ -0,0 +1,21 @@
|
||||
version: "3.9"
|
||||
|
||||
services:
|
||||
app:
|
||||
image: tap:latest # or build: . if you’re building locally
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: laravel_app
|
||||
restart: unless-stopped
|
||||
|
||||
ports:
|
||||
- "81:8080" # host:container
|
||||
|
||||
environment:
|
||||
# --- Laravel Core ---
|
||||
APP_NAME: "Laravel"
|
||||
|
||||
volumes:
|
||||
# - SQLite database file
|
||||
- ./database:/var/www/html/database
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
cd /var/www/html || exit 1
|
||||
|
||||
# Ensure Laravel dirs exist (idempotent)
|
||||
mkdir -p storage/framework/cache storage/framework/sessions storage/framework/views storage/logs bootstrap/cache
|
||||
|
||||
# Fix perms (in case volumes are mounted)
|
||||
chown -R nginx:nginx storage bootstrap/cache || true
|
||||
chmod -R ug+rwX storage bootstrap/cache || true
|
||||
|
||||
# If Laravel present: clear stale build-stage caches pointing to /app, then warm new caches
|
||||
if [ -f artisan ]; then
|
||||
php artisan config:clear --no-ansi || true
|
||||
php artisan cache:clear --no-ansi || true
|
||||
php artisan route:clear --no-ansi || true
|
||||
php artisan view:clear --no-ansi || true
|
||||
# Optional: warm fresh caches in runtime path
|
||||
php artisan config:cache --no-ansi || true
|
||||
php artisan route:cache --no-ansi || true
|
||||
php artisan view:cache --no-ansi || true
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
PORT="${APP_PORT:-8080}"
|
||||
|
||||
# 1) App Health
|
||||
curl -fsS "http://127.0.0.1:${PORT}/healthz" >/dev/null
|
||||
|
||||
# 2) PHP-FPM Ping via Nginx
|
||||
curl -fsS "http://127.0.0.1:${PORT}/fpm-ping" >/dev/null
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,29 @@
|
||||
# keine 'user' Direktive im non-root Betrieb
|
||||
worker_processes auto;
|
||||
error_log /dev/stderr warn;
|
||||
pid /tmp/nginx.pid;
|
||||
|
||||
events { worker_connections 1024; }
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
access_log /dev/stdout main;
|
||||
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
server_tokens off;
|
||||
|
||||
# Temp/Cache-Pfade, die 'nginx' gehören
|
||||
client_body_temp_path /var/lib/nginx/tmp/client_body 1 2;
|
||||
proxy_temp_path /var/lib/nginx/tmp/proxy 1 2;
|
||||
fastcgi_temp_path /var/lib/nginx/tmp/fastcgi 1 2;
|
||||
scgi_temp_path /var/lib/nginx/tmp/scgi 1 2;
|
||||
uwsgi_temp_path /var/lib/nginx/tmp/uwsgi 1 2;
|
||||
|
||||
include /etc/nginx/conf.d/*.conf;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
server {
|
||||
listen 8080;
|
||||
server_name localhost;
|
||||
|
||||
root /var/www/html/public;
|
||||
index index.php index.html;
|
||||
|
||||
# Readiness/Liveness
|
||||
location = /healthz { access_log off; return 200 "ok\n"; }
|
||||
|
||||
# FPM Ping/Status (optional absichern)
|
||||
location = /fpm-ping {
|
||||
access_log off;
|
||||
include fastcgi_params;
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
|
||||
fastcgi_param SCRIPT_NAME /ping;
|
||||
fastcgi_param PATH_INFO /ping;
|
||||
}
|
||||
location = /fpm-status {
|
||||
access_log off;
|
||||
include fastcgi_params;
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
|
||||
fastcgi_param SCRIPT_NAME /status;
|
||||
fastcgi_param PATH_INFO /status;
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php?$query_string;
|
||||
}
|
||||
|
||||
location ~ \.php$ {
|
||||
include fastcgi_params;
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
fastcgi_index index.php;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
fastcgi_read_timeout 60s;
|
||||
fastcgi_buffers 16 16k;
|
||||
}
|
||||
|
||||
location ~ /\.ht { deny all; }
|
||||
|
||||
# optionale Asset-Caches
|
||||
location ~* \.(?:css|js|jpg|jpeg|gif|png|svg|ico|webp|woff2?)$ {
|
||||
access_log off;
|
||||
expires 7d;
|
||||
add_header Cache-Control "public";
|
||||
try_files $uri =404;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
[supervisord]
|
||||
nodaemon=true
|
||||
logfile=/dev/fd/1
|
||||
logfile_maxbytes=0
|
||||
pidfile=/tmp/supervisord.pid
|
||||
childlogdir=/tmp
|
||||
|
||||
; one-shot init before services
|
||||
[program:init]
|
||||
command=/usr/local/bin/entrypoint.sh
|
||||
startsecs=0
|
||||
startretries=1
|
||||
autorestart=false
|
||||
priority=5
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
[program:php-fpm]
|
||||
command=/usr/local/sbin/php-fpm -F
|
||||
priority=10
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stopsignal=QUIT
|
||||
stopasgroup=true
|
||||
killasgroup=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
[program:nginx]
|
||||
command=/usr/sbin/nginx -g "daemon off;"
|
||||
priority=20
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stopsignal=QUIT
|
||||
stopasgroup=true
|
||||
killasgroup=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
Reference in New Issue
Block a user