Skip to content

Commit 05cff5d

Browse files
authored
Merge pull request dunglas#51 from dunglas/upgrade-docker
Upgrade Dockerfile
2 parents af583bc + 59b584a commit 05cff5d

File tree

9 files changed

+184
-111
lines changed

9 files changed

+184
-111
lines changed

.dockerignore

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
**/*.log
2+
**/*.md
3+
**/*.php~
24
**/._*
5+
**/.dockerignore
36
**/.DS_Store
4-
**/.gitignore
7+
**/.git/
58
**/.gitattributes
9+
**/.gitignore
10+
**/.gitmodules
11+
**/Dockerfile
612
**/Thumbs.db
7-
.dockerignore
8-
Dockerfile
9-
docker-compose.yaml
13+
.editorconfig
14+
.env*
15+
.php_cs.cache
16+
bin/*
17+
!bin/console
18+
docker/db/data/
19+
helm/
20+
public/bundles/
21+
var/
22+
vendor/

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ script:
2626
- docker-compose build --pull
2727
- docker-compose up -d
2828
- sleep 30
29-
- docker-compose exec -T app composer req sensiolabs/security-checker
30-
- docker-compose exec -T app bin/console security:check
31-
- docker-compose run --no-deps -T app composer validate --no-check-publish
29+
- docker-compose exec -T php composer req sensiolabs/security-checker
30+
- docker-compose exec -T php bin/console security:check
31+
- docker-compose run --no-deps -T php composer validate --no-check-publish
3232
- curl http://localhost # Client
3333
- curl -k https://localhost # Client (HTTP/2)
3434
- docker-compose logs # Display logs if anything wrong

Dockerfile

Lines changed: 114 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,81 @@
11
# the different stages of this Dockerfile are meant to be built into separate images
2+
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
23
# https://docs.docker.com/compose/compose-file/#target
34

4-
ARG PHP_VERSION=7.2
5-
ARG NGINX_VERSION=1.15
65

7-
### NGINX
8-
FROM nginx:${NGINX_VERSION}-alpine AS symfony_docker_nginx
6+
# https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
7+
ARG PHP_VERSION=7.4
8+
ARG NGINX_VERSION=1.17
99

10-
COPY docker/nginx/conf.d /etc/nginx/conf.d/
11-
COPY public /srv/app/public/
12-
13-
### H2 PROXY
14-
FROM alpine:latest AS symfony_docker_h2-proxy-cert
15-
16-
RUN apk add --no-cache openssl
17-
18-
# Use this self-generated certificate only in dev, IT IS NOT SECURE!
19-
RUN openssl genrsa -des3 -passout pass:NotSecure -out server.pass.key 2048
20-
RUN openssl rsa -passin pass:NotSecure -in server.pass.key -out server.key
21-
RUN rm server.pass.key
22-
RUN openssl req -new -passout pass:NotSecure -key server.key -out server.csr \
23-
-subj '/C=SS/ST=SS/L=Gotham City/O=Symfony/CN=localhost'
24-
RUN openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt
25-
26-
FROM nginx:${NGINX_VERSION}-alpine AS symfony_docker_h2-proxy
27-
28-
RUN mkdir -p /etc/nginx/ssl/
29-
COPY --from=symfony_docker_h2-proxy-cert server.key server.crt /etc/nginx/ssl/
30-
COPY ./docker/h2-proxy/default.conf /etc/nginx/conf.d/default.conf
31-
32-
### PHP
33-
FROM php:${PHP_VERSION}-fpm-alpine AS symfony_docker_php
10+
# "php" stage
11+
FROM php:${PHP_VERSION}-fpm-alpine AS symfony_php
3412

13+
# persistent / runtime deps
3514
RUN apk add --no-cache \
36-
git \
37-
icu-libs \
38-
zlib \
39-
jq
40-
41-
ENV APCU_VERSION 5.1.12
42-
RUN set -eux \
43-
&& apk add --no-cache --virtual .build-deps \
44-
$PHPIZE_DEPS \
45-
icu-dev \
46-
zlib-dev \
47-
&& docker-php-ext-install -j$(nproc) \
48-
intl \
49-
zip \
50-
&& pecl install \
51-
apcu-${APCU_VERSION} \
52-
&& docker-php-ext-enable --ini-name 20-apcu.ini apcu \
53-
&& docker-php-ext-enable --ini-name 05-opcache.ini opcache \
54-
&& runDeps="$( \
55-
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
56-
| tr ',' '\n' \
57-
| sort -u \
58-
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
59-
)" \
60-
&& apk add --no-cache --virtual .api-phpexts-rundeps $runDeps \
61-
&& apk del .build-deps
15+
acl \
16+
fcgi \
17+
file \
18+
gettext \
19+
git \
20+
jq \
21+
;
22+
23+
ARG APCU_VERSION=5.1.18
24+
RUN set -eux; \
25+
apk add --no-cache --virtual .build-deps \
26+
$PHPIZE_DEPS \
27+
icu-dev \
28+
libzip-dev \
29+
zlib-dev \
30+
; \
31+
\
32+
docker-php-ext-configure zip --with-libzip; \
33+
docker-php-ext-install -j$(nproc) \
34+
intl \
35+
zip \
36+
; \
37+
pecl install \
38+
apcu-${APCU_VERSION} \
39+
; \
40+
pecl clear-cache; \
41+
docker-php-ext-enable \
42+
apcu \
43+
opcache \
44+
; \
45+
\
46+
runDeps="$( \
47+
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
48+
| tr ',' '\n' \
49+
| sort -u \
50+
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
51+
)"; \
52+
apk add --no-cache --virtual .phpexts-rundeps $runDeps; \
53+
\
54+
apk del .build-deps
6255

63-
RUN ln -s $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
64-
COPY docker/app/conf.d/symfony.ini $PHP_INI_DIR/conf.d/symfony.ini
6556
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
66-
COPY docker/app/docker-entrypoint.sh /usr/local/bin/docker-app-entrypoint
67-
RUN chmod +x /usr/local/bin/docker-app-entrypoint
6857

69-
WORKDIR /srv/app
70-
ENTRYPOINT ["docker-app-entrypoint"]
71-
CMD ["php-fpm"]
58+
RUN ln -s $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
59+
COPY docker/php/conf.d/symfony.ini $PHP_INI_DIR/conf.d/symfony.ini
60+
61+
RUN set -eux; \
62+
{ \
63+
echo '[www]'; \
64+
echo 'ping.path = /ping'; \
65+
} | tee /usr/local/etc/php-fpm.d/docker-healthcheck.conf
7266

7367
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
74-
ENV COMPOSER_ALLOW_SUPERUSER 1
68+
ENV COMPOSER_ALLOW_SUPERUSER=1
69+
# install Symfony Flex globally to speed up download of Composer packages (parallelized prefetching)
70+
RUN set -eux; \
71+
composer global require "symfony/flex" --prefer-dist --no-progress --no-suggest --classmap-authoritative; \
72+
composer clear-cache
73+
ENV PATH="${PATH}:/root/.composer/vendor/bin"
74+
75+
WORKDIR /srv/app
7576

76-
# Use prestissimo to speed up builds
77-
RUN composer global require "symfony/flex" --prefer-dist --no-progress --no-suggest --classmap-authoritative --no-interaction
77+
# build for production
78+
ARG APP_ENV=prod
7879

7980
# Allow to use development versions of Symfony
8081
ARG STABILITY="stable"
@@ -84,14 +85,58 @@ ENV STABILITY ${STABILITY:-stable}
8485
ARG SYMFONY_VERSION=""
8586

8687
# Download the Symfony skeleton and leverage Docker cache layers
87-
RUN composer create-project "symfony/skeleton ${SYMFONY_VERSION}" . --stability=$STABILITY --prefer-dist --no-dev --no-progress --no-scripts --no-plugins --no-interaction
88+
RUN composer create-project "symfony/skeleton ${SYMFONY_VERSION}" . --stability=$STABILITY --prefer-dist --no-dev --no-progress --no-scripts --no-interaction; \
89+
composer clear-cache
8890

8991
###> recipes ###
9092
###< recipes ###
9193

9294
COPY . .
9395

94-
RUN mkdir -p var/cache var/logs var/sessions \
95-
&& composer install --prefer-dist --no-dev --no-scripts --no-progress --no-suggest --classmap-authoritative --no-interaction \
96-
&& composer clear-cache \
97-
&& chown -R www-data var
96+
RUN set -eux; \
97+
mkdir -p var/cache var/log; \
98+
composer dump-autoload --classmap-authoritative --no-dev; \
99+
composer run-script --no-dev post-install-cmd; sync
100+
VOLUME /srv/app/var
101+
102+
COPY docker/php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
103+
RUN chmod +x /usr/local/bin/docker-healthcheck
104+
105+
HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]
106+
107+
COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
108+
RUN chmod +x /usr/local/bin/docker-entrypoint
109+
110+
ENTRYPOINT ["docker-entrypoint"]
111+
CMD ["php-fpm"]
112+
113+
114+
# "nginx" stage
115+
# depends on the "php" stage above
116+
FROM nginx:${NGINX_VERSION}-alpine AS symfony_nginx
117+
118+
COPY docker/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
119+
120+
WORKDIR /srv/app
121+
122+
COPY --from=symfony_php /srv/app/public public/
123+
124+
# "h2-proxy-cert" stage
125+
FROM alpine:latest AS symfony_h2-proxy-cert
126+
127+
RUN apk add --no-cache openssl
128+
129+
# Use this self-generated certificate only in dev, IT IS NOT SECURE!
130+
RUN openssl genrsa -des3 -passout pass:NotSecure -out server.pass.key 2048
131+
RUN openssl rsa -passin pass:NotSecure -in server.pass.key -out server.key
132+
RUN rm server.pass.key
133+
RUN openssl req -new -passout pass:NotSecure -key server.key -out server.csr \
134+
-subj '/C=SS/ST=SS/L=Gotham City/O=Symfony/CN=localhost'
135+
RUN openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt
136+
137+
### "h2-proxy" stage
138+
FROM nginx:${NGINX_VERSION}-alpine AS symfony_h2-proxy
139+
140+
RUN mkdir -p /etc/nginx/ssl/
141+
COPY --from=symfony_h2-proxy-cert server.key server.crt /etc/nginx/ssl/
142+
COPY ./docker/h2-proxy/default.conf /etc/nginx/conf.d/default.conf

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ it's recommended to add a custom stage to the end of the `Dockerfile`.
3737

3838
```Dockerfile
3939
# Dockerfile
40-
FROM symfony_docker_php as symfony_docker_php_dev
40+
FROM symfony_php as symfony_php_dev
4141

42-
ARG XDEBUG_VERSION=2.6.0
42+
ARG XDEBUG_VERSION=2.8.0
4343
RUN set -eux; \
4444
apk add --no-cache --virtual .build-deps $PHPIZE_DEPS; \
4545
pecl install xdebug-$XDEBUG_VERSION; \
@@ -55,13 +55,13 @@ configuration remains untouched.
5555
As example, an override could look like this:
5656

5757
```yaml
58-
version: '3.4'
58+
version: "3.4"
5959

6060
services:
61-
app:
61+
php:
6262
build:
6363
context: .
64-
target: symfony_docker_php_dev
64+
target: symfony_php_dev
6565
environment:
6666
# See https://docs.docker.com/docker-for-mac/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
6767
# See https://github.com/docker/for-linux/issues/264
@@ -73,34 +73,31 @@ services:
7373
idekey=PHPSTORM
7474
# This should correspond to the server declared in PHPStorm `Preferences | Languages & Frameworks | PHP | Servers`
7575
# Then PHPStorm will use the corresponding path mappings
76-
PHP_IDE_CONFIG: serverName=symfony-docker
76+
PHP_IDE_CONFIG: serverName=symfony
7777
```
7878
7979
Then run:
8080
81-
````bash
81+
```bash
8282
docker-compose up -d
83-
````
83+
```
8484

85-
If `docker-compose.yaml` and a `docker-compose.override.yaml` are present on the same directory level, Docker Compose combines the two files into a single configuration, applying the configuration in the `docker-compose.override.yaml` file over and in addition to the values in the `docker-compose.yaml` file.
85+
If `docker-compose.yml` and a `docker-compose.override.yml` are present on the same directory level, Docker Compose combines the two files into a single configuration, applying the configuration in the `docker-compose.override.yml` file over and in addition to the values in the `docker-compose.yml` file.
8686

8787
### Troubleshooting
8888

8989
Inspect the installation with the following command. The requested Xdebug version should be displayed in the output.
9090

9191
```bash
92-
$ docker-compose exec app php --version
92+
$ docker-compose exec php php --version
9393

94-
PHP 7.2.8 (cli) (built: Jul 21 2018 08:09:37) ( NTS )
95-
Copyright (c) 1997-2018 The PHP Group
96-
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
97-
with Zend OPcache v7.2.8, Copyright (c) 1999-2018, by Zend Technologies
98-
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
94+
PHP ...
95+
with Xdebug v2.8.0 ...
9996
```
10097

10198
### Editing Permissions on Linux
10299

103-
If you work on linux and cannot edit some of the project files right after the first installation, you can run `docker-compose run --rm app chown -R $(id -u):$(id -g) .` to set yourself as owner of the project files that were created by the docker container.
100+
If you work on linux and cannot edit some of the project files right after the first installation, you can run `docker-compose run --rm php chown -R $(id -u):$(id -g) .` to set yourself as owner of the project files that were created by the docker container.
104101

105102
## Credits
106103

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
version: '3.4'
1+
version: "3.4"
22

33
services:
4-
app:
4+
php:
55
build:
66
context: .
7-
target: symfony_docker_php
7+
target: symfony_php
88
args:
99
SYMFONY_VERSION: ${SYMFONY_VERSION:-}
1010
STABILITY: ${STABILITY:-stable}
11+
healthcheck:
12+
interval: 10s
13+
timeout: 3s
14+
retries: 3
15+
start_period: 30s
1116
volumes:
1217
# Comment out the next line in production
1318
- ./:/srv/app:rw,cached
@@ -22,24 +27,24 @@ services:
2227
nginx:
2328
build:
2429
context: .
25-
target: symfony_docker_nginx
30+
target: symfony_nginx
2631
depends_on:
27-
- app
32+
- php
2833
volumes:
2934
# Comment out the next line in production
3035
- ./docker/nginx/conf.d:/etc/nginx/conf.d:ro
3136
- ./public:/srv/app/public:ro
3237
ports:
33-
- '80:80'
38+
- "80:80"
3439

3540
# This HTTP/2 proxy is not secure: it should only be used in dev
3641
h2-proxy:
3742
build:
3843
context: .
39-
target: symfony_docker_h2-proxy
44+
target: symfony_h2-proxy
4045
depends_on:
4146
- nginx
4247
volumes:
4348
- ./docker/h2-proxy/default.conf:/etc/nginx/conf.d/default.conf:ro
4449
ports:
45-
- '443:443'
50+
- "443:443"

docker/nginx/conf.d/default.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ server {
77
}
88
location ~ ^/index\.php(/|$) {
99
#resolver 127.0.0.11;
10-
#set $upstream_host app;
10+
#set $upstream_host php;
1111
#fastcgi_pass $upstream_host:9000;
1212
# Uncomment the previous lines and comment the next one to enable dynamic resolution (incompatible with Kubernetes)
13-
fastcgi_pass app:9000;
13+
fastcgi_pass php:9000;
1414
fastcgi_split_path_info ^(.+\.php)(/.*)$;
1515
include fastcgi_params;
1616
# When you are using symlinks to link the document root to the
File renamed without changes.

0 commit comments

Comments
 (0)