Skip to content

Commit 957aede

Browse files
committed
feat(docker): Add app-sf-worker for asynchronous message handling
Referenced from dunglas/symfony-docker#681
1 parent 3f0d296 commit 957aede

File tree

3 files changed

+124
-31
lines changed

3 files changed

+124
-31
lines changed

compose.yaml

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ services:
4646
sqlrunner:
4747
image: ghcr.io/database-playground/sqlrunner-v2:main
4848
php:
49-
image: ${IMAGES_PREFIX:-}app-php
49+
image: ${IMAGES_PREFIX:-}app-sf
5050
restart: unless-stopped
5151
environment:
5252
SERVER_NAME: ${SERVER_NAME:-localhost}, php:80
@@ -72,6 +72,17 @@ services:
7272
- target: 443
7373
published: ${HTTP3_PORT:-443}
7474
protocol: udp
75+
worker:
76+
image: ${IMAGES_PREFIX:-}app-sf-worker
77+
build:
78+
dockerfile: worker.Dockerfile
79+
context: .
80+
restart: unless-stopped
81+
environment:
82+
- RUN_MIGRATIONS=false
83+
depends_on:
84+
php:
85+
condition: service_healthy
7586

7687
volumes:
7788
###> doctrine/doctrine-bundle ###

frankenphp/docker-entrypoint.sh

+36-30
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,46 @@ if [ "$1" = 'frankenphp' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
77
fi
88

99
if [ -z "$(ls -A 'node_modules/' 2>/dev/null)" ]; then
10-
corepack prepare && pnpm install --prod --prefer-frozen-lockfile
10+
if node -v; then
11+
corepack prepare && pnpm install --prod --prefer-frozen-lockfile
12+
else
13+
echo "Node.js is not installed. Skipping npm packages installation."
14+
fi
1115
fi
1216

13-
if grep -q ^DATABASE_URL= .env; then
14-
echo "Waiting for database to be ready..."
15-
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
16-
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
17-
if [ $? -eq 255 ]; then
18-
# If the Doctrine command exits with 255, an unrecoverable error occurred
19-
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
20-
break
21-
fi
22-
sleep 1
23-
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
24-
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
25-
done
26-
27-
if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
28-
echo "The database is not up or not reachable:"
29-
echo "$DATABASE_ERROR"
30-
exit 1
31-
else
32-
echo "The database is now ready and reachable"
33-
fi
34-
35-
if [ "$( find ./migrations -iname '*.php' -print -quit )" ]; then
36-
php bin/console doctrine:migrations:migrate --no-interaction --all-or-nothing
37-
fi
17+
if [ "${RUN_MIGRATIONS:-true}" = "true" ]; then
18+
if grep -q ^DATABASE_URL= .env; then
19+
echo "Waiting for database to be ready..."
20+
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
21+
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
22+
if [ $? -eq 255 ]; then
23+
# If the Doctrine command exits with 255, an unrecoverable error occurred
24+
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
25+
break
26+
fi
27+
sleep 1
28+
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
29+
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
30+
done
31+
32+
if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
33+
echo "The database is not up or not reachable:"
34+
echo "$DATABASE_ERROR"
35+
exit 1
36+
else
37+
echo "The database is now ready and reachable"
38+
fi
39+
40+
if [ "$( find ./migrations -iname '*.php' -print -quit )" ]; then
41+
php bin/console doctrine:migrations:migrate --no-interaction --all-or-nothing
42+
fi
43+
fi
44+
45+
echo "Updating Meilisearch indexes..."
46+
php bin/console meili:clear || true
47+
php bin/console meili:import --update-settings || true
3848
fi
3949

40-
echo "Updating Meilisearch indexes..."
41-
php bin/console meili:clear || true
42-
php bin/console meili:import --update-settings || true
43-
4450
setfacl -R -m u:www-data:rwX -m u:"$(whoami)":rwX var
4551
setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX var
4652
fi

worker.Dockerfile

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM php:8.3-cli AS base
4+
5+
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
6+
7+
WORKDIR /app
8+
VOLUME /app/var/
9+
10+
RUN set -eux; \
11+
apt-get update \
12+
&& apt-get install -y --no-install-recommends \
13+
acl=* \
14+
file=* \
15+
gettext=* \
16+
git=* \
17+
&& rm -rf /var/lib/apt/lists/* \
18+
;
19+
20+
RUN set -eux; \
21+
install-php-extensions \
22+
@composer \
23+
apcu \
24+
curl \
25+
intl \
26+
opcache \
27+
zip \
28+
redis \
29+
pdo_pgsql \
30+
sysvsem \
31+
;
32+
33+
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
34+
ENV COMPOSER_ALLOW_SUPERUSER=1
35+
36+
ENV PHP_INI_SCAN_DIR=":$PHP_INI_DIR/app.conf.d"
37+
38+
COPY --link frankenphp/conf.d/10-app.ini $PHP_INI_DIR/app.conf.d/
39+
COPY --link --chmod=755 frankenphp/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
40+
41+
ENTRYPOINT ["docker-entrypoint"]
42+
43+
# Worker
44+
FROM base AS worker
45+
LABEL org.opencontainers.image.source="https://github.com/database-playground/app-sf"
46+
47+
ENV APP_ENV=prod
48+
ENV APP_DEBUG=0
49+
50+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
51+
52+
COPY --link frankenphp/conf.d/20-app.prod.ini $PHP_INI_DIR/app.conf.d/
53+
54+
# prevent the reinstallation of vendors at every changes in the source code
55+
COPY --link composer.* symfony.* package.json* ./
56+
RUN set -eux; \
57+
composer install --no-cache --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress;
58+
59+
# copy sources
60+
COPY --link . ./
61+
RUN rm -Rf frankenphp/ config/packages/debug.php config/packages/web_profiler.php
62+
63+
RUN set -eux; \
64+
mkdir -p var/cache var/log; \
65+
composer dump-autoload --classmap-authoritative --no-dev; \
66+
composer dump-env prod; \
67+
composer run-script --no-dev post-install-cmd; \
68+
chmod +x bin/console; sync;
69+
70+
# build route cache, sass and asset maps
71+
RUN set -eux; \
72+
chmod +x bin/console; sync; \
73+
./bin/console cache:clear; \
74+
./bin/console cache:warmup;
75+
76+
CMD ["php", "bin/console", "messenger:consume", "async", "-vvv"]

0 commit comments

Comments
 (0)