Skip to content

Commit 7339764

Browse files
committed
✨ Enhanced entrypoint setup
1 parent 2bb54b7 commit 7339764

File tree

6 files changed

+114
-45
lines changed

6 files changed

+114
-45
lines changed

Dockerfile

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
ARG php_version=8.3
22

33
FROM dunglas/frankenphp:1.1-php${php_version} AS base
4-
WORKDIR /app
4+
WORKDIR /laravel
5+
SHELL ["/bin/bash", "-eou", "pipefail", "-c"]
56

67
ENV SERVER_NAME=:80
78
ARG user=laravel
89

910
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
1011
COPY --chmod=755 src/entrypoint.sh /entrypoint.sh
12+
COPY --chmod=755 src/common /common
13+
COPY --chown=${user}:${user} src/artisan artisan
1114
COPY src/php.ini "${PHP_INI_DIR}/php.ini"
1215

1316
RUN apt-get update \
1417
&& apt-get satisfy -y --no-install-recommends \
1518
"curl (>=7.88)" \
1619
"supervisor (>=4.2)" \
1720
"unzip (>=6.0)" \
21+
"vim-tiny (>=2)" \
1822
&& rm -rf /var/lib/apt/lists/*
1923

2024
RUN useradd \
@@ -23,25 +27,29 @@ RUN useradd \
2327
"${user}" \
2428
&& setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp \
2529
&& chown -R "${user}:${user}" \
26-
/app \
30+
/laravel \
2731
/data/caddy \
2832
/config/caddy \
29-
&& mv "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini"
33+
/var/{log,run} \
34+
&& chmod -R a+rw \
35+
/var/{log,run}
3036

3137
RUN install-php-extensions \
38+
bcmath \
39+
bz2 \
3240
curl \
41+
exif \
3342
gd \
3443
intl \
3544
pcntl \
3645
pdo_pgsql \
3746
opcache \
3847
redis \
48+
sockets \
3949
zip
4050

4151
USER ${user}
4252

43-
COPY --chown=${user}:${user} src/artisan artisan
44-
4553
RUN mkdir -p \
4654
bootstrap/cache \
4755
storage/framework/cache \

README.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
55
## Environment Variables
66

7-
| `env` | Default | Mode |
8-
| ------------------------------ | ------- | --------- |
9-
| `CONTAINER_MANUAL_SETUP` || `*` |
10-
| `CONTAINER_MODE` | `"app"` | `*` |
11-
| `CONTAINER_PORT` | `8000` | app |
12-
| `CONTAINER_SCHEDULER_INTERVAL` | `60` | scheduler |
13-
| `CONTAINER_WORKER_DELAY` | `10` | worker |
14-
| `CONTAINER_WORKER_SLEEP` | `5` | worker |
15-
| `CONTAINER_WORKER_TRIES` | `3` | worker |
16-
| `CONTAINER_WORKER_TIMEOUT` | `300` | worker |
7+
| `env` | Default | Mode |
8+
| -------------------------- | ------- | ------ |
9+
| `CONTAINER_MANUAL_SETUP` || `*` |
10+
| `CONTAINER_MODE` | `"app"` | `*` |
11+
| `CONTAINER_PORT` | `8000` | app |
12+
| `CONTAINER_WORKER_DELAY` | `10` | worker |
13+
| `CONTAINER_WORKER_SLEEP` | `5` | worker |
14+
| `CONTAINER_WORKER_TRIES` | `3` | worker |
15+
| `CONTAINER_WORKER_TIMEOUT` | `300` | worker |

src/artisan

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ define('LARAVEL_START', microtime(true));
88
require __DIR__.'/vendor/autoload.php';
99

1010
$status = (require_once __DIR__.'/bootstrap/app.php')
11-
->handleCommand(new ArgvInput);
11+
->handleCommand(new ArgvInput);
1212

1313
exit($status);

src/common/test_db_connection.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require '/laravel/vendor/autoload.php';
4+
5+
use Illuminate\Support\Facades\DB;
6+
7+
$app = require_once '/laravel/bootstrap/app.php';
8+
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
9+
$kernel->bootstrap();
10+
11+
try {
12+
DB::connection()->getPdo();
13+
14+
if (DB::connection()->getDatabaseName()) {
15+
exit(0);
16+
}
17+
18+
echo 'Database name not found.';
19+
exit(1);
20+
21+
} catch (Exception $e) {
22+
echo 'Database connection error: ' . $e->getMessage();
23+
exit(1);
24+
}

src/entrypoint.sh

+47-21
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,68 @@ set -e
88
: "${CONTAINER_WORKER_SLEEP:=5}"
99
: "${CONTAINER_WORKER_TIMEOUT:=300}"
1010
: "${CONTAINER_WORKER_TRIES:=3}"
11-
: "${CONTAINER_SCHEDULER_INTERVAL:=60}"
1211
: "${APP_ENV:=production}"
1312

14-
ARTISAN="php -d variables_order=EGPCS /app/artisan"
13+
ARTISAN="php -d variables_order=EGPCS /laravel/artisan"
14+
15+
_migrate() {
16+
local count=0
17+
local timeout=20
18+
19+
while [ $count -lt "${timeout}" ]; do
20+
php -f /common/test_db_connection.php > /dev/null 2>&1
21+
22+
status=$?
23+
24+
if [ $status -eq 0 ]; then
25+
echo "✅ Database connection successful."
26+
break
27+
fi
28+
29+
echo "⏱ Waiting on database connection, retrying... $((timeout - count)) seconds left"
30+
count=$((count + 1))
31+
sleep 1
32+
done
33+
34+
if [ $count -eq "${timeout}" ]; then
35+
echo "⛔ Database connection failed after multiple attempts."
36+
exit 1
37+
fi
38+
39+
echo "🚀 Running migrations..."
40+
${ARTISAN} migrate --force --isolated
41+
}
1542

1643
_setup() {
1744
if [ -n "${CONTAINER_MANUAL_SETUP}" ]; then
18-
echo DEBUG: Skipping setup...
45+
echo ": Skipping setup..."
1946

2047
return
2148
fi
2249

23-
echo DEBUG: Preparing application...
50+
_migrate
2451

25-
${ARTISAN} storage:link || true
26-
27-
${ARTISAN} optimize || true
28-
${ARTISAN} config:cache || true
29-
${ARTISAN} route:cache || true
30-
${ARTISAN} view:cache || true
31-
${ARTISAN} events:cache || true
52+
if [ -d "/laravel/app/public/storage" ]; then
53+
echo "✅ Storage already linked..."
54+
else
55+
echo "🔐 Linking the storage..."
56+
${ARTISAN} storage:link
57+
fi
3258

33-
${ARTISAN} migrate --force || true
59+
${ARTISAN} config:cache
60+
${ARTISAN} events:cache
61+
${ARTISAN} route:cache
62+
${ARTISAN} view:cache
3463
}
3564

3665
_run() {
3766
case "${CONTAINER_MODE}" in
3867
app)
39-
echo INFO: Running octane...
68+
echo "🚀 Running octane..."
4069
exec "${ARTISAN}" octane:frankenphp --host=0.0.0.0 --port="${CONTAINER_PORT}"
4170
;;
4271
worker)
43-
echo INFO: Running the queue...
72+
echo " Running the queue..."
4473
exec "${ARTISAN}" queue:work -vv \
4574
--no-interaction \
4675
--tries="${CONTAINER_WORKER_TRIES}" \
@@ -49,18 +78,15 @@ _run() {
4978
--delay="${CONTAINER_WORKER_DELAY}"
5079
;;
5180
horizon)
52-
echo INFO: Running horizon...
81+
echo "Running horizon..."
5382
exec "${ARTISAN}" horizon
5483
;;
5584
scheduler)
56-
while true; do
57-
echo "INFO: Running scheduled tasks."
58-
"${ARTISAN}" schedule:run --verbose --no-interaction &
59-
sleep "${CONTAINER_SCHEDULER_INTERVAL}s"
60-
done
85+
echo "📆 Running scheduled tasks..."
86+
exec "${ARTISAN}" schedule:work --verbose --no-interaction
6187
;;
6288
*)
63-
echo "Could not match the container mode [${CONTAINER_MODE}]"
89+
echo "Could not match the container mode [${CONTAINER_MODE}]"
6490
exit 1
6591
;;
6692
esac

src/php.ini

+20-8
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,23 @@ session.sid_length = 26
7777
session.trans_sid_tags = "a=href,area=href,frame=src,form="
7878
session.sid_bits_per_character = 5
7979

80-
[opcache]
81-
opcache.enable=1
82-
opcache.interned_strings_buffer=64
83-
opcache.max_accelerated_files=5000
84-
opcache.max_wasted_percentage=15
85-
opcache.memory_consumption=512
86-
opcache.revalidate_freq=2
87-
opcache.validate_timestamps=0
80+
[Opcache]
81+
opcache.enable = 1
82+
opcache.enable_cli = 1
83+
opcache.file_cache = 60
84+
opcache.file_update_protection = 0
85+
opcache.interned_strings_buffer = 64
86+
opcache.max_accelerated_files = 32531
87+
opcache.max_file_size = 0
88+
opcache.max_wasted_percentage = 15
89+
opcache.memory_consumption = 256M
90+
opcache.revalidate_freq = 2
91+
opcache.use_cwd = 0
92+
opcache.validate_timestamps = 0
93+
94+
[JIT]
95+
opcache.jit = function
96+
opcache.jit_buffer_size = 128M
97+
opcache.jit_max_root_traces = 2048
98+
opcache.jit_max_side_traces = 256
99+
opcache.jit_prof_threshold = 0.001

0 commit comments

Comments
 (0)