Skip to content

Commit 2349860

Browse files
authored
Use docker & Franken PHP for production (#167)
* Use docker & Franken PHP for production * Fix deps * Fix env test * ¨Fix phpcs
1 parent 0cbdced commit 2349860

22 files changed

+1742
-1163
lines changed

.dockerignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**/*.log
2+
**/*.md
3+
**/*.php~
4+
**/*.dist.php
5+
**/*.dist
6+
**/*.cache
7+
**/._*
8+
**/.dockerignore
9+
**/.DS_Store
10+
**/.git/
11+
**/.gitattributes
12+
**/.gitignore
13+
**/.gitmodules
14+
**/compose.*.yaml
15+
**/compose.*.yml
16+
**/compose.yaml
17+
**/compose.yml
18+
**/docker-compose.*.yaml
19+
**/docker-compose.*.yml
20+
**/docker-compose.yaml
21+
**/docker-compose.yml
22+
**/Dockerfile
23+
**/Thumbs.db
24+
.github/
25+
docs/
26+
public/bundles/
27+
tests/
28+
var/
29+
vendor/
30+
.editorconfig
31+
.env.*.local
32+
.env.local
33+
.env.local.php
34+
.env.test

.editorconfig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
# Change these settings to your own preference
9+
indent_style = space
10+
indent_size = 4
11+
12+
# We recommend you to keep these unchanged
13+
end_of_line = lf
14+
charset = utf-8
15+
trim_trailing_whitespace = true
16+
insert_final_newline = true
17+
18+
[*.{js,html,ts,tsx}]
19+
indent_size = 2
20+
21+
[*.json]
22+
indent_size = 2
23+
24+
[*.md]
25+
trim_trailing_whitespace = false
26+
27+
[*.sh]
28+
indent_style = tab
29+
30+
[*.xml{,.dist}]
31+
indent_style = space
32+
indent_size = 4
33+
34+
[*.{yaml,yml}]
35+
trim_trailing_whitespace = false
36+
37+
[.github/workflows/*.yml]
38+
indent_size = 2
39+
40+
[.gitmodules]
41+
indent_style = tab
42+
43+
[.php_cs{,.dist}]
44+
indent_style = space
45+
indent_size = 4
46+
47+
[composer.json]
48+
indent_size = 4
49+
50+
[{,docker-}compose{,.*}.{yaml,yml}]
51+
indent_style = space
52+
indent_size = 2
53+
54+
[{,*.*}Dockerfile]
55+
indent_style = tab
56+
57+
[{,*.*}Caddyfile]
58+
indent_style = tab

.env

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@
1616

1717
###> symfony/framework-bundle ###
1818
APP_ENV=dev
19-
APP_SECRET=8a7dbef62a01a71c70f60699dab35f8d
19+
APP_SECRET=0aa93032faae15a4ca1d4f9a5bd681de
2020
###< symfony/framework-bundle ###
2121

2222
###> doctrine/doctrine-bundle ###
2323
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
2424
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
2525
#
26-
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
27-
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7&charset=utf8mb4"
28-
DATABASE_URL="postgresql://killer:112358@127.0.0.1:5432/symfokiller?serverVersion=13&charset=utf8"
26+
DATABASE_URL="postgresql://killer:112358@127.0.0.1:5432/symfokiller?serverVersion=16&charset=utf8"
2927
DATABASE_URL_TEST="postgresql://killer:112358@127.0.0.1:5432/symfokiller_test?serverVersion=13&charset=utf8"
3028
POSTGRES_DB=symfokiller
3129
POSTGRES_USER=killer
3230
POSTGRES_PASSWORD=112358
3331
###< doctrine/doctrine-bundle ###
3432

33+
###> lexik/jwt-authentication-bundle ###
34+
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
35+
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
36+
JWT_PASSPHRASE=11f52873414740deef38cd13bb7ef28f
37+
###< lexik/jwt-authentication-bundle ###
38+
3539
###> nelmio/cors-bundle ###
3640
CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
3741
###< nelmio/cors-bundle ###
@@ -48,9 +52,3 @@ MERCURE_PATH=/.well-known/mercure
4852
# The secret used to sign the JWTs
4953
MERCURE_JWT_SECRET=dev-@1123581321-killer-mercure-secret
5054
###< symfony/mercure-bundle ###
51-
52-
###> lexik/jwt-authentication-bundle ###
53-
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
54-
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
55-
JWT_PASSPHRASE=11f52873414740deef38cd13bb7ef28f
56-
###< lexik/jwt-authentication-bundle ###

.env.test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ SYMFONY_DEPRECATIONS_HELPER=999999
55
PANTHER_APP_ENV=panther
66
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
77

8-
DATABASE_URL_TEST="postgresql://killer:112358@127.0.0.1:5432/symfokiller_test?serverVersion=13&charset=utf8"
9-
DATABASE_URL="postgresql://killer:112358@127.0.0.1:5432/symfokiller_test?serverVersion=13&charset=utf8"
8+
# Use database instead of 127.0.0.1 in docker env
9+
DATABASE_URL_TEST="postgresql://killer:112358@127.0.0.1:5432/symfokiller_test?serverVersion=16&charset=utf8"
10+
DATABASE_URL="postgresql://killer:112358@127.0.0.1:5432/symfokiller_test?serverVersion=16&charset=utf8"
1011
POSTGRES_DB=symfokiller_test
1112
POSTGRES_USER=killer
1213
POSTGRES_PASSWORD=112358

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
* text=auto eol=lf
2+
3+
*.conf text eol=lf
4+
*.html text eol=lf
5+
*.ini text eol=lf
6+
*.js text eol=lf
7+
*.json text eol=lf
8+
*.md text eol=lf
9+
*.php text eol=lf
10+
*.sh text eol=lf
11+
*.yaml text eol=lf
12+
*.yml text eol=lf
13+
bin/console text eol=lf
14+
composer.lock text eol=lf merge=ours
15+
16+
*.ico binary
17+
*.png binary

Dockerfile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#syntax=docker/dockerfile:1
2+
3+
# Versions
4+
FROM dunglas/frankenphp:1-php8.3 AS frankenphp_upstream
5+
6+
# The different stages of this Dockerfile are meant to be built into separate images
7+
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
8+
# https://docs.docker.com/compose/compose-file/#target
9+
10+
11+
# Base FrankenPHP image
12+
FROM frankenphp_upstream AS frankenphp_base
13+
14+
WORKDIR /app
15+
16+
VOLUME /app/var/
17+
18+
# persistent / runtime deps
19+
# hadolint ignore=DL3008
20+
RUN apt-get update && apt-get install -y --no-install-recommends \
21+
acl \
22+
file \
23+
gettext \
24+
git \
25+
&& rm -rf /var/lib/apt/lists/*
26+
27+
RUN set -eux; \
28+
install-php-extensions \
29+
@composer \
30+
apcu \
31+
intl \
32+
opcache \
33+
zip \
34+
;
35+
36+
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
37+
ENV COMPOSER_ALLOW_SUPERUSER=1
38+
39+
ENV PHP_INI_SCAN_DIR=":$PHP_INI_DIR/app.conf.d"
40+
41+
###> recipes ###
42+
###> doctrine/doctrine-bundle ###
43+
RUN install-php-extensions pdo_pgsql
44+
###< doctrine/doctrine-bundle ###
45+
###< recipes ###
46+
47+
COPY --link frankenphp/conf.d/10-app.ini $PHP_INI_DIR/app.conf.d/
48+
COPY --link --chmod=755 frankenphp/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
49+
COPY --link frankenphp/Caddyfile /etc/caddy/Caddyfile
50+
51+
ENTRYPOINT ["docker-entrypoint"]
52+
53+
HEALTHCHECK --start-period=60s CMD curl -f http://localhost:2019/metrics || exit 1
54+
CMD [ "frankenphp", "run", "--config", "/etc/caddy/Caddyfile" ]
55+
56+
# Dev FrankenPHP image
57+
FROM frankenphp_base AS frankenphp_dev
58+
59+
ENV APP_ENV=dev XDEBUG_MODE=off
60+
61+
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
62+
63+
RUN set -eux; \
64+
install-php-extensions \
65+
xdebug \
66+
;
67+
68+
COPY --link frankenphp/conf.d/20-app.dev.ini $PHP_INI_DIR/app.conf.d/
69+
70+
CMD [ "frankenphp", "run", "--config", "/etc/caddy/Caddyfile", "--watch" ]
71+
72+
# Prod FrankenPHP image
73+
FROM frankenphp_base AS frankenphp_prod
74+
75+
ENV APP_ENV=prod
76+
ENV FRANKENPHP_CONFIG="import worker.Caddyfile"
77+
78+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
79+
80+
COPY --link frankenphp/conf.d/20-app.prod.ini $PHP_INI_DIR/app.conf.d/
81+
COPY --link frankenphp/worker.Caddyfile /etc/caddy/worker.Caddyfile
82+
83+
# prevent the reinstallation of vendors at every changes in the source code
84+
COPY --link composer.* symfony.* ./
85+
RUN set -eux; \
86+
composer install --no-cache --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress
87+
88+
# copy sources
89+
COPY --link . ./
90+
RUN rm -Rf frankenphp/
91+
92+
RUN set -eux; \
93+
mkdir -p var/cache var/log; \
94+
composer dump-autoload --classmap-authoritative --no-dev; \
95+
composer dump-env prod; \
96+
composer run-script --no-dev post-install-cmd; \
97+
chmod +x bin/console; sync;

bin/console

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
use App\Kernel;
55
use Symfony\Bundle\FrameworkBundle\Console\Application;
66

7+
if (!is_dir(dirname(__DIR__).'/vendor')) {
8+
throw new LogicException('Dependencies are missing. Try running "composer install".');
9+
}
10+
711
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
812
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
913
}

compose.override.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Development environment override
2+
services:
3+
php:
4+
build:
5+
context: .
6+
target: frankenphp_dev
7+
volumes:
8+
- ./:/app
9+
- ./frankenphp/Caddyfile:/etc/caddy/Caddyfile:ro
10+
- ./frankenphp/conf.d/20-app.dev.ini:/usr/local/etc/php/app.conf.d/20-app.dev.ini:ro
11+
# If you develop on Mac or Windows you can remove the vendor/ directory
12+
# from the bind-mount for better performance by enabling the next line:
13+
#- /app/vendor
14+
environment:
15+
MERCURE_EXTRA_DIRECTIVES: |
16+
cors_origins "https://localhost:4000" "http://localhost:4000"
17+
# See https://xdebug.org/docs/all_settings#mode
18+
XDEBUG_MODE: "${XDEBUG_MODE:-off}"
19+
extra_hosts:
20+
# Ensure that host.docker.internal is correctly defined on Linux
21+
- host.docker.internal:host-gateway
22+
tty: true
23+
24+
###> symfony/mercure-bundle ###
25+
mercure:
26+
ports:
27+
- "80"
28+
###< symfony/mercure-bundle ###
29+
30+
###> doctrine/doctrine-bundle ###
31+
database:
32+
ports:
33+
- "5432"
34+
###< doctrine/doctrine-bundle ###

compose.prod.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Production environment override
2+
services:
3+
php:
4+
build:
5+
context: .
6+
target: frankenphp_prod
7+
environment:
8+
APP_SECRET: ${APP_SECRET}
9+
MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
10+
MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}

0 commit comments

Comments
 (0)