Skip to content
This repository was archived by the owner on Mar 7, 2020. It is now read-only.

Commit df99307

Browse files
committed
PoC - add Docker for Monofony
1 parent e7da5f2 commit df99307

File tree

11 files changed

+436
-4
lines changed

11 files changed

+436
-4
lines changed

.dockerignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**/*.log
2+
**/*.md
3+
**/*.php~
4+
**/._*
5+
**/.dockerignore
6+
**/.DS_Store
7+
**/.git/
8+
**/.gitattributes
9+
**/.gitignore
10+
**/.gitkeep
11+
**/.gitmodules
12+
**/Dockerfile
13+
**/Thumbs.db
14+
.editorconfig
15+
.php_cs.cache
16+
.travis.yml
17+
composer.phar
18+
docker-compose*.yaml
19+
docker-compose*.yml
20+
docker/mysql/data/
21+
etc/build/*
22+
node_modules/
23+
var/*
24+
vendor/
25+
public/assets/
26+
public/bundles/
27+
public/css/
28+
public/js/
29+
public/media

.env

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@
1717
APP_ENV=dev
1818
APP_DEBUG=1
1919
APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629
20+
APP_LOCALE=en_US
2021
###< symfony/framework-bundle ###
2122

2223
###> doctrine/doctrine-bundle ###
2324
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
2425
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
2526
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
26-
DATABASE_URL=mysql://[email protected]/app_name_%kernel.environment%
27+
MYSQL_ROOT_PASSWORD=root
28+
MYSQL_DATABASE=application_prod
29+
MYSQL_USER=application
30+
MYSQL_PASSWORD=application_passwd
31+
32+
###> doctrine/doctrine-bundle ###
33+
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
34+
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
35+
# Configure your db driver and server_version in config/packages/doctrine.yaml
36+
DATABASE_URL=mysql://$MYSQL_USER:$MYSQL_PASSWORD@mysql:3306/$MYSQL_DATABASE
2737
###< doctrine/doctrine-bundle ###
2838

2939
###> symfony/swiftmailer-bundle ###
3040
# For Gmail as a transport, use: "gmail://username:password@localhost"
3141
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
3242
# Delivery is disabled by default via "null://localhost"
33-
MAILER_URL=null://localhost
43+
MAILER_URL=smtp://mailhog:1025
3444
###< symfony/swiftmailer-bundle ###
3545

3646
###> nelmio/cors-bundle ###

Dockerfile

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# the different stages of this Dockerfile are meant to be built into separate images
2+
# https://docs.docker.com/compose/compose-file/#target
3+
4+
ARG PHP_VERSION=7.3
5+
ARG NODE_VERSION=10
6+
ARG NGINX_VERSION=1.16
7+
8+
FROM scratch as scratch
9+
10+
COPY . /code/
11+
12+
FROM php:${PHP_VERSION}-fpm-alpine AS application_php
13+
14+
# persistent / runtime deps
15+
RUN apk add --no-cache \
16+
acl \
17+
file \
18+
gettext \
19+
git \
20+
mariadb-client \
21+
shadow \
22+
;
23+
24+
ARG APCU_VERSION=5.1.17
25+
RUN set -eux; \
26+
apk add --no-cache --virtual .build-deps \
27+
$PHPIZE_DEPS \
28+
coreutils \
29+
freetype-dev \
30+
icu-dev \
31+
libjpeg-turbo-dev \
32+
libpng-dev \
33+
libtool \
34+
libwebp-dev \
35+
libzip-dev \
36+
mariadb-dev \
37+
zlib-dev \
38+
; \
39+
\
40+
docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include --with-webp-dir=/usr/include --with-freetype-dir=/usr/include/; \
41+
docker-php-ext-configure zip --with-libzip; \
42+
docker-php-ext-install -j$(nproc) \
43+
exif \
44+
gd \
45+
intl \
46+
pdo_mysql \
47+
zip \
48+
; \
49+
pecl install \
50+
apcu-${APCU_VERSION} \
51+
redis \
52+
; \
53+
pecl clear-cache; \
54+
docker-php-ext-enable \
55+
apcu \
56+
opcache \
57+
redis \
58+
; \
59+
\
60+
runDeps="$( \
61+
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
62+
| tr ',' '\n' \
63+
| sort -u \
64+
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
65+
)"; \
66+
apk add --no-cache --virtual .application-phpexts-rundeps $runDeps; \
67+
\
68+
apk del .build-deps
69+
70+
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
71+
COPY --from=scratch code/docker/php/php.ini /usr/local/etc/php/php.ini
72+
COPY --from=scratch code/docker/php/php-cli.ini /usr/local/etc/php/php-cli.ini
73+
COPY --from=scratch code/docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
74+
RUN chmod +x /usr/local/bin/docker-entrypoint
75+
76+
ARG USER_UID=1000
77+
78+
RUN usermod -u $USER_UID www-data -s /bin/sh
79+
80+
USER www-data
81+
WORKDIR /home/www-data
82+
83+
RUN set -eux; \
84+
composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative; \
85+
composer clear-cache
86+
87+
ENV PATH="${PATH}:/home/www-data/.composer/vendor/bin"
88+
89+
# build for production
90+
ARG APP_ENV=prod
91+
92+
RUN mkdir /home/www-data/application
93+
94+
WORKDIR /home/www-data/application
95+
96+
# prevent the reinstallation of vendors at every changes in the source code
97+
COPY --from=scratch --chown=www-data:www-data code/composer.json code/composer.lock code/symfony.lock ./
98+
RUN set -eux; \
99+
composer install --prefer-dist --no-autoloader --no-scripts --no-progress --no-suggest; \
100+
composer clear-cache
101+
102+
# copy only specifically what we need
103+
COPY --from=scratch --chown=www-data:www-data code/.env code/.env.test ./
104+
COPY --from=scratch --chown=www-data:www-data code/webpack.config.js ./
105+
COPY --from=scratch --chown=www-data:www-data code/assets assets/
106+
COPY --from=scratch --chown=www-data:www-data code/bin bin/
107+
COPY --from=scratch --chown=www-data:www-data code/config config/
108+
COPY --from=scratch --chown=www-data:www-data code/config config/
109+
COPY --from=scratch --chown=www-data:www-data code/public public/
110+
COPY --from=scratch --chown=www-data:www-data code/templates templates/
111+
COPY --from=scratch --chown=www-data:www-data code/translations translations/
112+
113+
RUN set -eux; \
114+
mkdir -p var/cache var/log; \
115+
composer dump-autoload --classmap-authoritative; \
116+
APP_SECRET='' composer run-script post-install-cmd; \
117+
chmod +x bin/console; sync;
118+
119+
VOLUME /home/www-data/application/var
120+
121+
ENTRYPOINT ["docker-entrypoint"]
122+
CMD ["php-fpm"]
123+
124+
FROM node:${NODE_VERSION}-alpine AS application_nodejs
125+
126+
RUN set -eux; \
127+
apk add --no-cache --virtual .build-deps \
128+
g++ \
129+
gcc \
130+
git \
131+
make \
132+
python \
133+
shadow \
134+
;
135+
136+
COPY --from=scratch code/docker/nodejs/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
137+
RUN chmod +x /usr/local/bin/docker-entrypoint
138+
139+
ARG USER_UID=1000
140+
141+
RUN usermod -u $USER_UID node
142+
143+
USER node
144+
145+
RUN mkdir /home/node/application
146+
RUN mkdir /home/node/application/public
147+
148+
WORKDIR /home/node/application
149+
150+
COPY --from=scratch --chown=node:node code/package.json code/yarn.lock code/webpack.config.js ./
151+
COPY --from=scratch --chown=node:node code/assets assets/
152+
RUN set -eux; \
153+
yarn install; \
154+
yarn cache clean
155+
156+
#COPY --from=scratch --from=application_php --chown=node:node /home/www-data/application/public public/
157+
COPY --from=application_php --chown=node:node /home/www-data/application/vendor/sylius/ui-bundle/Resources/private/ vendor/sylius/ui-bundle/Resources/private/
158+
159+
RUN set -eux; \
160+
NODE_ENV=prod yarn build
161+
162+
ENTRYPOINT ["docker-entrypoint"]
163+
CMD ["yarn", "dev"]
164+
165+
FROM nginx:${NGINX_VERSION}-alpine AS application_nginx
166+
167+
COPY --from=scratch code/docker/nginx/conf.d/default.conf /etc/nginx/conf.d/
168+
169+
WORKDIR /home/www-data/application/
170+
171+
COPY --from=application_php /home/www-data/application/public public/
172+
COPY --from=application_nodejs /home/node/application/public public/
173+
174+
FROM application_php as application_php_runtime
175+
176+
USER www-data
177+
178+
WORKDIR /home/www-data/application
179+
180+
COPY --from=application_nodejs --chown=www-data:www-data /home/node/application/public public/
181+
182+
ENTRYPOINT ["docker-entrypoint"]
183+
CMD ["php-fpm"]

config/packages/framework.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
locale: fr_FR
2+
locale: '%env(resolve:APP_LOCALE)%'
33
secret: '%env(resolve:APP_SECRET)%'
44

55
framework:

docker-compose.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
version: '3.4'
2+
3+
services:
4+
php:
5+
build:
6+
context: .
7+
target: application_php_runtime
8+
cache_from:
9+
- registry.gitlab.com/gabiudrescu/symfonystarter/php:master
10+
- registry.gitlab.com/gabiudrescu/symfonystarter/nodejs:master
11+
- registry.gitlab.com/gabiudrescu/symfonystarter/nginx:master
12+
args:
13+
- USER_UID=1000
14+
image: registry.gitlab.com/gabiudrescu/symfonystarter/php:master
15+
depends_on:
16+
- mysql
17+
environment:
18+
- PHP_DATE_TIMEZONE=${PHP_DATE_TIMEZONE:-UTC}
19+
volumes:
20+
- .:/home/www-data/application:rw,cached
21+
- ~/.ssh:/home/www-data/.ssh
22+
- ~/.composer:/home/www-data/.composer
23+
24+
mysql:
25+
image: percona:5.7
26+
command: mysqld --sql_mode=""
27+
env_file:
28+
- .env
29+
volumes:
30+
- mysql-data:/var/lib/mysql:rw
31+
ports:
32+
- "3306:3306"
33+
34+
nodejs:
35+
build:
36+
context: .
37+
target: application_nodejs
38+
cache_from:
39+
- registry.gitlab.com/gabiudrescu/symfonystarter/php:master
40+
- registry.gitlab.com/gabiudrescu/symfonystarter/nodejs:master
41+
- registry.gitlab.com/gabiudrescu/symfonystarter/nginx:master
42+
args:
43+
- USER_UID=1000
44+
image: registry.gitlab.com/gabiudrescu/symfonystarter/nodejs:master
45+
depends_on:
46+
- php
47+
environment:
48+
- NODE_ENV=dev
49+
- PHP_HOST=php
50+
- PHP_PORT=9000
51+
volumes:
52+
- .:/home/node/application:rw,cached
53+
ports:
54+
- "35729:35729"
55+
- "8080:8080"
56+
57+
nginx:
58+
build:
59+
context: .
60+
target: application_nginx
61+
cache_from:
62+
- registry.gitlab.com/gabiudrescu/symfonystarter/php:master
63+
- registry.gitlab.com/gabiudrescu/symfonystarter/nodejs:master
64+
- registry.gitlab.com/gabiudrescu/symfonystarter/nginx:master
65+
image: registry.gitlab.com/gabiudrescu/symfonystarter/nginx:master
66+
depends_on:
67+
- php
68+
- nodejs
69+
volumes:
70+
- ./public:/home/www-data/application/public:ro
71+
ports:
72+
- "80:80"
73+
74+
mailhog:
75+
# do not use in production!
76+
image: mailhog/mailhog:latest
77+
environment:
78+
- MH_STORAGE=maildir
79+
volumes:
80+
- mailhog-data:/maildir:rw
81+
ports:
82+
- "8025:8025"
83+
84+
redis:
85+
image: redis:5-alpine
86+
87+
volumes:
88+
mysql-data:
89+
mailhog-data:

docker/nginx/conf.d/default.conf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
server {
2+
root /home/www-data/application/public;
3+
4+
location / {
5+
# try to serve file directly, fallback to index.php
6+
try_files $uri /index.php$is_args$args;
7+
}
8+
9+
location ~ ^/index\.php(/|$) {
10+
# Comment the next line and uncomment the next to enable dynamic resolution (incompatible with Kubernetes)
11+
fastcgi_pass php:9000;
12+
#resolver 127.0.0.11;
13+
#set $upstream_host php;
14+
#fastcgi_pass $upstream_host:9000;
15+
16+
fastcgi_split_path_info ^(.+\.php)(/.*)$;
17+
include fastcgi_params;
18+
# When you are using symlinks to link the document root to the
19+
# current version of your application, you should pass the real
20+
# application path instead of the path to the symlink to PHP
21+
# FPM.
22+
# Otherwise, PHP's OPcache may not properly detect changes to
23+
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
24+
# for more information).
25+
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
26+
fastcgi_param DOCUMENT_ROOT $realpath_root;
27+
# Prevents URIs that include the front controller. This will 404:
28+
# http://domain.tld/index.php/some-path
29+
# Remove the internal directive to allow URIs like this
30+
internal;
31+
}
32+
33+
# return 404 for all other php files not matching the front controller
34+
# this prevents access to other php files you don't want to be accessible.
35+
location ~ \.php$ {
36+
return 404;
37+
}
38+
39+
client_max_body_size 6m;
40+
}

0 commit comments

Comments
 (0)