Skip to content

Commit 43bb351

Browse files
dunglasLucasHospice
authored andcommitted
refactor: improve image, uniformize with API Platform (dunglas#275)
* refactor: improve image, uniformize with API Platform * feat: remove the SKELETON arg
1 parent 4a378fc commit 43bb351

File tree

9 files changed

+69
-70
lines changed

9 files changed

+69
-70
lines changed

Dockerfile

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ ARG PHP_VERSION=8.1
88
ARG CADDY_VERSION=2
99

1010
# Prod image
11-
FROM php:${PHP_VERSION}-fpm-alpine AS symfony_php
11+
FROM php:${PHP_VERSION}-fpm-alpine AS app_php
12+
13+
# Allow to use development versions of Symfony
14+
ARG STABILITY="stable"
15+
ENV STABILITY ${STABILITY}
16+
17+
# Allow to select Symfony version
18+
ARG SYMFONY_VERSION=""
19+
ENV SYMFONY_VERSION ${SYMFONY_VERSION}
20+
21+
ENV APP_ENV=prod
22+
23+
WORKDIR /srv/app
1224

1325
# persistent / runtime deps
1426
RUN apk add --no-cache \
@@ -48,87 +60,80 @@ RUN set -eux; \
4860
| sort -u \
4961
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
5062
)"; \
51-
apk add --no-cache --virtual .phpexts-rundeps $runDeps; \
63+
apk add --no-cache --virtual .app-phpexts-rundeps $runDeps; \
5264
\
5365
apk del .build-deps
5466

55-
COPY docker/php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
56-
RUN chmod +x /usr/local/bin/docker-healthcheck
57-
58-
HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]
67+
###> recipes ###
68+
###< recipes ###
5969

6070
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
61-
COPY docker/php/conf.d/symfony.ini $PHP_INI_DIR/conf.d/symfony.ini
62-
COPY docker/php/conf.d/symfony.prod.ini $PHP_INI_DIR/conf.d/symfony.prod.ini
71+
COPY docker/php/conf.d/app.ini $PHP_INI_DIR/conf.d/
72+
COPY docker/php/conf.d/app.prod.ini $PHP_INI_DIR/conf.d/
6373

6474
COPY docker/php/php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
75+
RUN mkdir -p /var/run/php
76+
77+
COPY docker/php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
78+
RUN chmod +x /usr/local/bin/docker-healthcheck
79+
80+
HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]
6581

6682
COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
6783
RUN chmod +x /usr/local/bin/docker-entrypoint
6884

69-
VOLUME /var/run/php
70-
71-
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
85+
ENTRYPOINT ["docker-entrypoint"]
86+
CMD ["php-fpm"]
7287

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

78-
WORKDIR /srv/app
79-
80-
# Allow to choose skeleton
81-
ARG SKELETON="symfony/skeleton"
82-
ENV SKELETON ${SKELETON}
92+
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
8393

84-
# Allow to use development versions of Symfony
85-
ARG STABILITY="stable"
86-
ENV STABILITY ${STABILITY}
87-
88-
# Allow to select skeleton version
89-
ARG SYMFONY_VERSION=""
90-
ENV SYMFONY_VERSION ${SYMFONY_VERSION}
91-
92-
# Download the Symfony skeleton and leverage Docker cache layers
93-
RUN composer create-project "${SKELETON} ${SYMFONY_VERSION}" . --stability=$STABILITY --prefer-dist --no-dev --no-progress --no-interaction; \
94-
composer clear-cache
95-
96-
###> recipes ###
97-
###< recipes ###
94+
# prevent the reinstallation of vendors at every changes in the source code
95+
COPY composer.* symfony.* ./
96+
RUN set -eux; \
97+
if [ -f composer.json ]; then \
98+
composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress; \
99+
composer clear-cache; \
100+
fi
98101

102+
# copy sources
99103
COPY . .
104+
RUN rm -Rf docker/
100105

101106
RUN set -eux; \
102107
mkdir -p var/cache var/log; \
103-
composer install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction; \
104-
composer dump-autoload --classmap-authoritative --no-dev; \
105-
composer symfony:dump-env prod; \
106-
composer run-script --no-dev post-install-cmd; \
107-
chmod +x bin/console; sync
108-
VOLUME /srv/app/var
109-
110-
ENTRYPOINT ["docker-entrypoint"]
111-
CMD ["php-fpm"]
108+
if [ -f composer.json ]; then \
109+
composer dump-autoload --classmap-authoritative --no-dev; \
110+
composer dump-env prod; \
111+
composer run-script --no-dev post-install-cmd; \
112+
chmod +x bin/console; sync; \
113+
fi
112114

113115
# Dev image
114-
FROM symfony_php AS symfony_php_dev
116+
FROM app_php AS app_php_dev
117+
118+
ENV APP_ENV=dev
119+
VOLUME /srv/app/var/
120+
121+
RUN rm $PHP_INI_DIR/conf.d/app.prod.ini; \
122+
mv "$PHP_INI_DIR/php.ini" "$PHP_INI_DIR/php.ini-production"; \
123+
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
124+
125+
COPY docker/php/conf.d/app.dev.ini $PHP_INI_DIR/conf.d/
115126

116127
RUN set -eux; \
117128
apk add --no-cache --virtual .build-deps $PHPIZE_DEPS; \
118129
pecl install xdebug; \
119130
docker-php-ext-enable xdebug; \
120131
apk del .build-deps
121132

122-
RUN rm $PHP_INI_DIR/conf.d/symfony.prod.ini; \
123-
mv "$PHP_INI_DIR/php.ini" "$PHP_INI_DIR/php.ini-production"; \
124-
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
125-
126-
COPY docker/php/conf.d/symfony.dev.ini $PHP_INI_DIR/conf.d/symfony.dev.ini
127-
128133
RUN rm -f .env.local.php
129134

130135
# Build Caddy with the Mercure and Vulcain modules
131-
FROM caddy:${CADDY_VERSION}-builder-alpine AS symfony_caddy_builder
136+
FROM caddy:${CADDY_VERSION}-builder-alpine AS app_caddy_builder
132137

133138
RUN xcaddy build \
134139
--with github.com/dunglas/mercure \
@@ -137,10 +142,10 @@ RUN xcaddy build \
137142
--with github.com/dunglas/vulcain/caddy
138143

139144
# Caddy image
140-
FROM caddy:${CADDY_VERSION} AS symfony_caddy
145+
FROM caddy:${CADDY_VERSION} AS app_caddy
141146

142147
WORKDIR /srv/app
143148

144-
COPY --from=symfony_caddy_builder /usr/bin/caddy /usr/bin/caddy
145-
COPY --from=symfony_php /srv/app/public public/
149+
COPY --from=app_caddy_builder /usr/bin/caddy /usr/bin/caddy
150+
COPY --from=app_php /srv/app/public public/
146151
COPY docker/caddy/Caddyfile /etc/caddy/Caddyfile

docker-compose.override.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ version: "3.4"
44
services:
55
php:
66
build:
7-
target: symfony_php_dev
7+
target: app_php_dev
88
volumes:
99
- ./:/srv/app
10-
- ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.dev.ini:ro
10+
- ./docker/php/conf.d/app.dev.ini:/usr/local/etc/php/conf.d/app.dev.ini:ro
1111
# If you develop on Mac or Windows you can remove the var/ and vendor/ directories
1212
# from the bind-mount for better performance by enabling the next 2 lines:
1313
#- /srv/app/var

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ services:
44
php:
55
build:
66
context: .
7-
target: symfony_php
7+
target: app_php
88
args:
99
SYMFONY_VERSION: ${SYMFONY_VERSION:-}
10-
SKELETON: ${SKELETON:-symfony/skeleton}
1110
STABILITY: ${STABILITY:-stable}
1211
restart: unless-stopped
1312
volumes:
@@ -28,7 +27,7 @@ services:
2827
caddy:
2928
build:
3029
context: .
31-
target: symfony_caddy
30+
target: app_caddy
3231
depends_on:
3332
- php
3433
environment:
File renamed without changes.
File renamed without changes.
File renamed without changes.

docker/php/docker-entrypoint.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ fi
99
if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
1010
mkdir -p var/cache var/log
1111

12-
# The first time volumes are mounted, the project needs to be created
12+
# Install the project the first time PHP is started
13+
# After the installation, the following block can be deleted
1314
if [ ! -f composer.json ]; then
1415
CREATION=1
15-
composer create-project "$SKELETON $SYMFONY_VERSION" tmp --stability="$STABILITY" --prefer-dist --no-progress --no-interaction --no-install
16+
composer create-project "symfony/skeleton $SYMFONY_VERSION" tmp --stability="$STABILITY" --prefer-dist --no-progress --no-interaction --no-install
1617

1718
cd tmp
1819
composer require "php:>=$PHP_VERSION"
@@ -28,6 +29,7 @@ if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
2829
fi
2930

3031
if grep -q ^DATABASE_URL= .env; then
32+
# After the installation, the following block can be deleted
3133
if [ "$CREATION" = "1" ]; then
3234
echo "To finish the installation please press Ctrl+C to stop Docker Compose and run: docker compose up --build"
3335
sleep infinity

docs/build.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
# Build Options
22

3-
## Selecting a Symfony Skeleton
4-
5-
By default, Symfony Docker will install the minimalist skeleton.
6-
To install the ["website skeleton"](https://symfony.com/doc/current/setup.html#creating-symfony-applications), use the following command:
7-
8-
SKELETON=symfony/website-skeleton docker compose up --build
9-
103
## Selecting a Specific Symfony Version
114

125
Use the `SYMFONY_VERSION` environment variable to select a specific Symfony version.
136

14-
For instance, use the following command to install Symfony 4.4:
7+
For instance, use the following command to install Symfony 5.4:
158

16-
SYMFONY_VERSION=4.4.* docker compose up --build
9+
SYMFONY_VERSION=5.4.* docker compose up --build
1710

1811
## Installing Development Versions of Symfony
1912

2013
To install a non-stable version of Symfony, use the `STABILITY` environment variable during the build.
2114
The value must be [a valid Composer stability option](https://getcomposer.org/doc/04-schema.md#minimum-stability)) .
2215

23-
For instance, use the following command to use the `master` branch of Symfony:
16+
For instance, use the following command to use the development branch of Symfony:
2417

2518
STABILITY=dev docker compose up --build
2619

2720
## Customizing the Server Name
2821

2922
Use the `SERVER_NAME` environment variable to define your custom server name(s).
3023

31-
SERVER_NAME="symfony.localhost, caddy:80" docker compose up --build
24+
SERVER_NAME="app.localhost, caddy:80" docker compose up --build
3225

3326
If you use Mercure, keep `caddy:80` in the list to allow the PHP container to request the caddy service.
3427

docs/makefile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ SYMFONY = $(PHP_CONT) bin/console
4848
.DEFAULT_GOAL = help
4949
.PHONY = help build up start down logs sh composer vendor sf cc
5050

51-
## —— 🎵 🐳 The Symfony-docker Makefile 🐳 🎵 ——————————————————————————————————
51+
## —— 🎵 🐳 The Symfony Docker Makefile 🐳 🎵 ——————————————————————————————————
5252
help: ## Outputs this help screen
5353
@grep -E '(^[a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'
5454

0 commit comments

Comments
 (0)