Skip to content

Commit 486acd3

Browse files
committed
add missing files
1 parent e984a31 commit 486acd3

File tree

8 files changed

+375
-0
lines changed

8 files changed

+375
-0
lines changed

example-app/entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Check if the storage directory is empty (initial start)
5+
if [ ! "$(ls -A /var/www/storage)" ]; then
6+
echo "Initializing storage directory..."
7+
cp -R /var/www/storage-init/. /var/www/storage
8+
chown -R www-data:www-data /var/www/storage
9+
fi
10+
11+
# Run the default command
12+
exec "$@"

example-app/nginx.conf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
include /etc/nginx/mime.types;
7+
default_type application/octet-stream;
8+
9+
sendfile on;
10+
keepalive_timeout 65;
11+
12+
server {
13+
listen 80;
14+
server_name localhost;
15+
16+
root /var/www/public;
17+
index index.php index.html;
18+
19+
location / {
20+
try_files $uri $uri/ /index.php?$query_string;
21+
}
22+
23+
location ~ \.php$ {
24+
fastcgi_pass php-fpm:9000;
25+
fastcgi_index index.php;
26+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
27+
include fastcgi_params;
28+
}
29+
}
30+
}

production/nginx-fpm/.env.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sync user ID and group ID to avoid permission issues
2+
UID=1000 # Replace with your user ID
3+
GID=1000 # Replace with your group ID
4+
5+
# Path to your Laravel application
6+
LARAVEL_APP_PATH=/absolute/path/to/your-laravel-app
7+
8+
# Nginx configuration
9+
# The host where Nginx will be accessible.
10+
NGINX_HOST=localhost
11+
12+
# The port on which Nginx will listen. Typically, this would be port 80.
13+
NGINX_PORT=80
14+
15+
# PostgreSQL database configuration
16+
# The name of the database to be used by your Laravel application.
17+
DB_DATABASE=laravel
18+
19+
# The username for accessing the PostgreSQL database.
20+
DB_USERNAME=laravel
21+
22+
# The password for accessing the PostgreSQL database.
23+
DB_PASSWORD=secret

production/nginx-fpm/compose.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
services:
2+
web:
3+
image: nginx:alpine
4+
build:
5+
context: ../../example-app
6+
dockerfile: ../production/nginx-fpm/nginx/Dockerfile
7+
volumes:
8+
- laravel-storage:/var/www/storage:ro # Mount the storage volume (in case you want to use storage:link)
9+
networks:
10+
- laravel
11+
ports:
12+
- "${NGINX_PORT}:80"
13+
environment:
14+
- NGINX_HOST=${NGINX_HOST}
15+
depends_on:
16+
php-fpm:
17+
condition: service_healthy # Wait for php-fpm health check
18+
19+
php-fpm:
20+
# For the php-fpm service, we will create a custom image to install the necessary PHP extensions and setup proper permissions.
21+
build:
22+
context: ../../example-app
23+
dockerfile: ../production/nginx-fpm/php-fpm/Dockerfile
24+
volumes:
25+
- laravel-storage:/var/www/storage # Mount the storage volume
26+
environment:
27+
- APP_NAME=TestApp
28+
- APP_KEY=base64:y6S7gjyAO/PVAJMBvJSJUCO8qiz92qvcNAPRKH3hpj4=
29+
- APP_DEBUG=true
30+
networks:
31+
- laravel
32+
healthcheck:
33+
test: ["CMD-SHELL", "php-fpm-healthcheck || exit 1"]
34+
interval: 10s
35+
timeout: 5s
36+
retries: 3
37+
# The `depends_on` section tells Docker Compose to
38+
# start the database before your application.
39+
depends_on:
40+
postgres:
41+
condition: service_healthy
42+
43+
php-cli:
44+
build:
45+
context: ../../example-app
46+
dockerfile: ../production/nginx-fpm/php-cli/Dockerfile
47+
tty: true # Keep the terminal open
48+
stdin_open: true # Keep stdin open to attach to the container
49+
environment:
50+
- APP_NAME=TestApp
51+
networks:
52+
- laravel
53+
54+
postgres:
55+
image: postgres:16
56+
restart: always
57+
user: postgres
58+
secrets:
59+
- db-password
60+
ports:
61+
- "5432:5432"
62+
environment:
63+
- POSTGRES_DB=${DB_DATABASE}
64+
- POSTGRES_USER=${DB_USERNAME}
65+
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
66+
volumes:
67+
- postgres-data:/var/lib/postgresql/data
68+
networks:
69+
- laravel
70+
healthcheck:
71+
test: [ "CMD", "pg_isready" ]
72+
interval: 10s
73+
timeout: 5s
74+
retries: 5
75+
76+
redis:
77+
image: redis:alpine
78+
networks:
79+
- laravel
80+
healthcheck:
81+
test: ["CMD", "redis-cli", "ping"]
82+
interval: 10s
83+
timeout: 5s
84+
retries: 3
85+
86+
networks:
87+
laravel:
88+
89+
volumes:
90+
postgres-data:
91+
laravel-storage:
92+
93+
secrets:
94+
db-password:
95+
file: db-password.txt

production/nginx-fpm/db-password.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secret

production/nginx-fpm/nginx/Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Stage 1: Build assets
2+
FROM debian AS builder
3+
4+
# Install Node.js and build tools
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
curl \
7+
nodejs \
8+
npm \
9+
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
10+
11+
# Set working directory
12+
WORKDIR /var/www
13+
14+
# Copy Laravel application code
15+
COPY . /var/www
16+
17+
# Install Node.js dependencies and build assets
18+
RUN npm install && npm run build
19+
20+
# Stage 2: Nginx production image
21+
FROM nginx:alpine
22+
23+
# Copy custom Nginx configuration
24+
COPY nginx.conf /etc/nginx/nginx.conf
25+
26+
# Copy Laravel's public assets (from the builder stage)
27+
COPY --from=builder /var/www/public /var/www/public
28+
29+
# Ensure proper permissions for www-data (Nginx default user)
30+
#RUN chown -R www-data:www-data /var/www/public
31+
32+
# Expose port 80
33+
EXPOSE 80
34+
35+
CMD ["nginx", "-g", "daemon off;"]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Stage 1: Build environment and Composer dependencies
2+
FROM php:8.3-cli AS builder
3+
4+
# Install system dependencies and PHP extensions required for Laravel + MySQL/PostgreSQL support
5+
# Some dependencies are required for PHP extensions only in the build stage
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
curl \
8+
unzip \
9+
libpq-dev \
10+
libonig-dev \
11+
libssl-dev \
12+
libxml2-dev \
13+
libcurl4-openssl-dev \
14+
libicu-dev \
15+
libzip-dev \
16+
&& docker-php-ext-install -j$(nproc) \
17+
pdo_mysql \
18+
pdo_pgsql \
19+
pgsql \
20+
opcache \
21+
intl \
22+
zip \
23+
bcmath \
24+
soap \
25+
&& pecl install redis \
26+
&& docker-php-ext-enable redis \
27+
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
28+
29+
# Set the working directory inside the container
30+
WORKDIR /var/www
31+
32+
# Copy the entire Laravel application code into the container
33+
COPY . /var/www
34+
35+
# Install Composer and dependencies
36+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
37+
&& composer install --no-dev --optimize-autoloader --no-interaction --no-progress --prefer-dist
38+
39+
# Stage 2: Production environment
40+
FROM php:8.3-cli
41+
42+
# Install client libraries required for php extensions in runtime
43+
RUN apt-get update && apt-get install -y --no-install-recommends \
44+
libpq-dev \
45+
libicu-dev \
46+
libzip-dev \
47+
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
48+
49+
# Copy PHP extensions and libraries from the builder stage
50+
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
51+
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
52+
COPY --from=builder /usr/local/bin/docker-php-ext-* /usr/local/bin/
53+
54+
# Use the default production configuration for PHP runtime arguments
55+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
56+
57+
# Copy the application code and dependencies from the build stage
58+
COPY --from=builder /var/www /var/www
59+
60+
# Set working directory
61+
WORKDIR /var/www
62+
63+
# Ensure correct permissions
64+
RUN chown -R www-data:www-data /var/www
65+
66+
# Switch to the non-privileged user to run the application
67+
USER www-data
68+
69+
# Default command: Provide a bash shell to allow running any command
70+
CMD ["bash"]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Stage 1: Build environment and Composer dependencies
2+
FROM php:8.3-fpm AS builder
3+
4+
# Install system dependencies and PHP extensions required for Laravel + MySQL/PostgreSQL support
5+
# Some dependencies are required for PHP extensions only in the build stage
6+
# We don't need to install Node.js or build assets, as it was done in the Nginx image
7+
RUN apt-get update && apt-get install -y --no-install-recommends \
8+
curl \
9+
unzip \
10+
libpq-dev \
11+
libonig-dev \
12+
libssl-dev \
13+
libxml2-dev \
14+
libcurl4-openssl-dev \
15+
libicu-dev \
16+
libzip-dev \
17+
&& docker-php-ext-install -j$(nproc) \
18+
pdo_mysql \
19+
pdo_pgsql \
20+
pgsql \
21+
opcache \
22+
intl \
23+
zip \
24+
bcmath \
25+
soap \
26+
&& pecl install redis \
27+
&& docker-php-ext-enable redis \
28+
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
29+
30+
# Set the working directory inside the container
31+
WORKDIR /var/www
32+
33+
# Copy the entire Laravel application code into the container
34+
# -----------------------------------------------------------
35+
# In Laravel, the `composer install` command often triggers
36+
# scripts that need access to the entire application code.
37+
# For example, the `post-autoload-dump` event might execute
38+
# Artisan commands like `php artisan package:discover`. If the
39+
# application code (including the `artisan` file) is not
40+
# present, these commands will fail, leading to build errors.
41+
#
42+
# By copying the entire application code before running
43+
# `composer install`, we ensure that all necessary files are
44+
# available, allowing these scripts to run successfully.
45+
# In other cases, it would be possible to copy composer files
46+
# first, to leverage Docker's layer caching mechanism.
47+
# -----------------------------------------------------------
48+
COPY . /var/www
49+
50+
# Install Composer and dependencies
51+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
52+
&& composer install --no-dev --optimize-autoloader --no-interaction --no-progress --prefer-dist
53+
54+
# Stage 2: Production environment
55+
FROM php:8.3-fpm
56+
57+
# Install only runtime libraries needed in production
58+
# libfcgi-bin and procps are required for the php-fpm-healthcheck script
59+
RUN apt-get update && apt-get install -y --no-install-recommends \
60+
libpq-dev \
61+
libicu-dev \
62+
libzip-dev \
63+
libfcgi-bin \
64+
procps \
65+
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
66+
67+
# Download and install php-fpm health check script
68+
RUN curl -o /usr/local/bin/php-fpm-healthcheck \
69+
https://raw.githubusercontent.com/renatomefi/php-fpm-healthcheck/master/php-fpm-healthcheck \
70+
&& chmod +x /usr/local/bin/php-fpm-healthcheck
71+
72+
# Copy the initialization script
73+
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
74+
RUN chmod +x /usr/local/bin/entrypoint.sh
75+
76+
# Copy the initial storage structure
77+
COPY ./storage /var/www/storage-init
78+
79+
# Copy PHP extensions and libraries from the builder stage
80+
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
81+
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
82+
COPY --from=builder /usr/local/bin/docker-php-ext-* /usr/local/bin/
83+
84+
# Use the default production configuration for PHP runtime arguments
85+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
86+
87+
# Enable PHP-FPM status page by modifying zz-docker.conf with sed
88+
RUN sed -i '/\[www\]/a pm.status_path = /status' /usr/local/etc/php-fpm.d/zz-docker.conf
89+
# Update the variables_order to include E (for ENV)
90+
#RUN sed -i 's/variables_order = "GPCS"/variables_order = "EGPCS"/' "$PHP_INI_DIR/php.ini"
91+
92+
# Copy the application code and dependencies from the build stage
93+
COPY --from=builder /var/www /var/www
94+
95+
# Set working directory
96+
WORKDIR /var/www
97+
98+
# Ensure correct permissions
99+
RUN chown -R www-data:www-data /var/www
100+
101+
# Switch to the non-privileged user to run the application
102+
USER www-data
103+
104+
# Change the default command to run the entrypoint script
105+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
106+
107+
# Expose port 9000 and start php-fpm server
108+
EXPOSE 9000
109+
CMD ["php-fpm"]

0 commit comments

Comments
 (0)