Skip to content

Commit

Permalink
Chore(web-twig): Prepare development server for Symfony application
Browse files Browse the repository at this point in the history
  • Loading branch information
literat committed Mar 23, 2023
1 parent 49d5f7e commit b0d3d7b
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/web-twig/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/*.log
**/*.md
**/*.php~
**/*.dist.php
**/*.dist
**/*.cache
**/._*
**/.dockerignore
**/.DS_Store
**/.git/
**/.gitattributes
**/.gitignore
**/.gitmodules
**/docker-compose.*.yaml
**/docker-compose.*.yml
**/docker-compose.yaml
**/docker-compose.yml
**/Dockerfile
**/Thumbs.db
.github/
docs/
public/bundles/
tests/
var/
vendor/
.editorconfig
.env.*.local
.env.local
.env.local.php
.env.test
118 changes: 118 additions & 0 deletions packages/web-twig/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#syntax=docker/dockerfile:1.4

# The different stages of this Dockerfile are meant to be built into separate images
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
# https://docs.docker.com/compose/compose-file/#target

# Builder images
FROM composer/composer:2-bin AS composer

FROM mlocati/php-extension-installer:latest AS php_extension_installer

# Prod image
FROM php:7.4-fpm AS app_php

# Allow to use development versions of Symfony
ARG STABILITY="stable"
ENV STABILITY ${STABILITY}

# Allow to select Symfony version
ARG SYMFONY_VERSION=""
ENV SYMFONY_VERSION ${SYMFONY_VERSION}

ENV APP_ENV=prod

WORKDIR /srv/www/spirit-web-twig-bundle

# php extensions installer: https://github.com/mlocati/docker-php-extension-installer
COPY --from=php_extension_installer --link /usr/bin/install-php-extensions /usr/local/bin/

# persistent / runtime deps
RUN apt-get update && apt-get install -y \
acl \
libfcgi-bin \
file \
gettext \
git \
zip

RUN set -eux; \
install-php-extensions \
intl \
zip \
# apcu \
# opcache \
;

###> recipes ###
###< recipes ###

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY --link ./php/conf.d/app.ini $PHP_INI_DIR/conf.d/
COPY --link ./php/conf.d/app.prod.ini $PHP_INI_DIR/conf.d/

COPY --link ./php/php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
RUN mkdir -p /var/run/php

COPY --link ./php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
RUN chmod +x /usr/local/bin/docker-healthcheck

HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]

COPY --link ./php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm", "-R"]

# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV PATH="${PATH}:/root/.composer/vendor/bin"

COPY --from=composer --link /composer /usr/bin/composer

# prevent the reinstallation of vendors at every changes in the source code
COPY --link ../composer.* ../symfony.* ./
RUN set -eux; \
if [ -f composer.json ]; then \
composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress; \
composer clear-cache; \
fi

# copy sources
COPY --link ../ ./
RUN rm -Rf docker/

RUN set -eux; \
mkdir -p var/cache var/log; \
if [ -f composer.json ]; then \
composer dump-autoload --classmap-authoritative --no-dev; \
composer dump-env prod; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync; \
fi

# Dev image
FROM app_php AS app_php_dev

ENV APP_ENV=dev XDEBUG_MODE=off
VOLUME /srv/www/spirit-web-twig-bundle/var/

RUN rm "$PHP_INI_DIR/conf.d/app.prod.ini"; \
mv "$PHP_INI_DIR/php.ini" "$PHP_INI_DIR/php.ini-production"; \
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

COPY --link php/conf.d/app.dev.ini $PHP_INI_DIR/conf.d/

RUN set -eux; \
install-php-extensions xdebug

RUN rm -f .env.local.php

# Caddy image
FROM caddy:2.6 AS app_caddy

WORKDIR /srv/www/spirit-web-twig-bundle

# COPY --from=app_php --link /srv/www/spirit-web-twig-bundle/public public/
COPY --link caddy/Caddyfile /etc/caddy/Caddyfile
17 changes: 17 additions & 0 deletions packages/web-twig/docker/caddy/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
# Debug
{$CADDY_DEBUG}
}

{$SERVER_NAME}

{$CADDY_EXTRA_CONFIG}

log

route {
root * /srv/www/spirit-web-twig-bundle/public
php_fastcgi unix//var/run/php/php-fpm.sock
encode zstd gzip
file_server
}
26 changes: 26 additions & 0 deletions packages/web-twig/docker/docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.4'

# Development environment override
services:
php:
build:
target: app_php_devj
volumes:
- ./../:/srv/www/spirit-web-twig-bundle/:delegated
- ./../../icons/src/svg/:/srv/www/spirit-web-twig-bundle/static/:delegated
- ./../docker/php/conf.d/app.dev.ini:/usr/local/etc/php/conf.d/app.dev.ini:ro
# If you develop on Mac or Windows you can remove the vendor/ directory
# from the bind-mount for better performance by enabling the next line:
- /srv/app/vendor
environment:
# See https://xdebug.org/docs/all_settings#mode
XDEBUG_MODE: '${XDEBUG_MODE:-off}'
extra_hosts:
# Ensure that host.docker.internal is correctly defined on Linux
- host.docker.internal:host-gateway

caddy:
command: ['caddy', 'run', '--config', '/etc/caddy/Caddyfile', '--adapter', 'caddyfile', '--watch']
volumes:
- ./../public:/srv/www/spirit-web-twig-bundle/public:ro
- ./../docker/caddy/Caddyfile:/etc/caddy/Caddyfile:ro
9 changes: 9 additions & 0 deletions packages/web-twig/docker/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.4'

# Production environment override
services:
php:
environment:
APP_SECRET: ${APP_SECRET}

caddy:
49 changes: 49 additions & 0 deletions packages/web-twig/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
services:
php:
build:
context: .
dockerfile: 'Dockerfile'
target: app_php
args:
SYMFONY_VERSION: ${SYMFONY_VERSION:-}
STABILITY: ${STABILITY:-stable}
restart: unless-stopped
volumes:
- php_socket:/var/run/php
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s

caddy:
build:
context: .
target: app_caddy
depends_on:
- php
environment:
SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80}
restart: unless-stopped
volumes:
- php_socket:/var/run/php
- caddy_data:/data
- caddy_config:/config
ports:
# HTTP
- target: 80
published: ${HTTP_PORT:-8080}
protocol: tcp
# HTTPS
- target: 443
published: ${HTTPS_PORT:-4443}
protocol: tcp
# HTTP/3
- target: 443
published: ${HTTP3_PORT:-4443}
protocol: udp

volumes:
php_socket:
caddy_data:
caddy_config:
5 changes: 5 additions & 0 deletions packages/web-twig/docker/php/conf.d/app.dev.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; See https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
; See https://github.com/docker/for-linux/issues/264
; The `client_host` below may optionally be replaced with `discover_client_host=yes`
; Add `start_with_request=yes` to start debug session on each request
xdebug.client_host = 'host.docker.internal'
13 changes: 13 additions & 0 deletions packages/web-twig/docker/php/conf.d/app.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expose_php = 0
date.timezone = UTC
apc.enable_cli = 1
session.use_strict_mode = 1
zend.detect_unicode = 0

; https://symfony.com/doc/current/performance.html
realpath_cache_size = 4096K
realpath_cache_ttl = 600
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 20000
opcache.memory_consumption = 256
opcache.enable_file_override = 1
2 changes: 2 additions & 0 deletions packages/web-twig/docker/php/conf.d/app.prod.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opcache.preload_user = www-data
opcache.preload = /srv/www/spirit-web-twig-bundle/config/preload.php
19 changes: 19 additions & 0 deletions packages/web-twig/docker/php/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
set -e

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@"
fi

if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then

if [ "$APP_ENV" != 'prod' ]; then
composer install --prefer-dist --no-progress --no-interaction
fi

setfacl -R -m u:www-data:rwX -m u:"$(whoami)":rwX var
setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX var
fi

exec docker-php-entrypoint "$@"
8 changes: 8 additions & 0 deletions packages/web-twig/docker/php/docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
set -e

if env -i REQUEST_METHOD=GET SCRIPT_NAME=/ping SCRIPT_FILENAME=/ping cgi-fcgi -bind -connect /var/run/php/php-fpm.sock; then
exit 0
fi

exit 1
8 changes: 8 additions & 0 deletions packages/web-twig/docker/php/php-fpm.d/zz-docker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[global]
daemonize = no
process_control_timeout = 20

[www]
listen = /var/run/php/php-fpm.sock
listen.mode = 0666
ping.path = /ping
1 change: 1 addition & 0 deletions packages/web-twig/public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*

0 comments on commit b0d3d7b

Please sign in to comment.