Skip to content

Commit d6e6c58

Browse files
committed
Implement Docker
1 parent 642a2a7 commit d6e6c58

9 files changed

+509
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/public/build/fonts/glyphicons-*
22
/public/build/images/glyphicons-*
33

4+
###> docker ###
5+
docker-compose.override.yml
6+
###< docker ###
7+
48
###> symfony/framework-bundle ###
59
/.env.local
610
/.env.*.local

.php_cs.dist

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ COMMENT;
1212
$finder = PhpCsFixer\Finder::create()
1313
->in(__DIR__)
1414
->exclude('config')
15+
->exclude('src/Migrations')
1516
->exclude('var')
1617
->exclude('public/bundles')
1718
->exclude('public/build')

Dockerfile

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
ARG NODE_VERSION=11.6.0
2+
ARG COMPOSER_VERSION=1.8.0
3+
ARG PHP_VERSION=7.2.13
4+
ARG ICU_VERSION=63.1
5+
ARG APCU_VERSION=5.1.16
6+
ARG XDEBUG_VERSION=2.6.1
7+
8+
9+
#####################################
10+
## APP ##
11+
#####################################
12+
FROM php:${PHP_VERSION}-fpm as app
13+
14+
ARG ICU_VERSION
15+
ARG APCU_VERSION
16+
17+
# Used for the ICU compilation
18+
ENV PHP_CPPFLAGS="${PHP_CPPFLAGS} -std=c++11"
19+
ENV APP_VERSION=0.0.0
20+
21+
WORKDIR /app
22+
23+
# Install paquet requirements
24+
RUN set -ex; \
25+
# Install required system packages
26+
apt-get update; \
27+
apt-get install -qy --no-install-recommends \
28+
zlib1g-dev \
29+
git \
30+
; \
31+
# Compile ICU (required by intl php extension)
32+
curl -L -o /tmp/icu.tar.gz http://download.icu-project.org/files/icu4c/${ICU_VERSION}/icu4c-$(echo ${ICU_VERSION} | sed s/\\./_/g)-src.tgz; \
33+
tar -zxf /tmp/icu.tar.gz -C /tmp; \
34+
cd /tmp/icu/source; \
35+
./configure --prefix=/usr/local; \
36+
make clean; \
37+
make; \
38+
make install; \
39+
#Install the PHP extensions
40+
docker-php-ext-configure intl --with-icu-dir=/usr/local; \
41+
docker-php-ext-install -j "$(nproc)" \
42+
intl \
43+
pdo \
44+
# pdo_mysql \ Uncomment it to use MySQL, and remove the pdo_sqlite (see: docker-compose.yml, docker-compose.override.yml.dist)
45+
zip \
46+
bcmath \
47+
; \
48+
pecl install \
49+
apcu-${APCU_VERSION} \
50+
; \
51+
docker-php-ext-enable \
52+
opcache \
53+
apcu \
54+
; \
55+
docker-php-source delete; \
56+
# Clean aptitude cache and tmp directory
57+
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*;
58+
59+
## set recommended PHP.ini settings
60+
RUN { \
61+
echo 'date.timezone = Europe/Paris'; \
62+
echo 'short_open_tag = off'; \
63+
echo 'expose_php = off'; \
64+
echo 'error_log = /proc/self/fd/2'; \
65+
echo 'memory_limit = 128m'; \
66+
echo 'post_max_size = 110m'; \
67+
echo 'upload_max_filesize = 100m'; \
68+
echo 'opcache.enable = 1'; \
69+
echo 'opcache.enable_cli = 1'; \
70+
echo 'opcache.memory_consumption = 256'; \
71+
echo 'opcache.interned_strings_buffer = 16'; \
72+
echo 'opcache.max_accelerated_files = 20011'; \
73+
echo 'opcache.fast_shutdown = 1'; \
74+
echo 'realpath_cache_size = 4096K'; \
75+
echo 'realpath_cache_ttl = 600'; \
76+
} > /usr/local/etc/php/php.ini
77+
78+
RUN { \
79+
echo 'date.timezone = Europe/Paris'; \
80+
echo 'short_open_tag = off'; \
81+
echo 'memory_limit = 8192M'; \
82+
} > /usr/local/etc/php/php-cli.ini
83+
84+
CMD ["php-fpm"]
85+
86+
87+
#####################################
88+
## APP DEV ##
89+
#####################################
90+
FROM app as app-dev
91+
92+
ARG NODE_VERSION
93+
ARG COMPOSER_VERSION
94+
ARG XDEBUG_VERSION
95+
96+
ENV COMPOSER_ALLOW_SUPERUSER=1
97+
ENV APP_ENV=dev
98+
99+
# Install paquet requirements
100+
RUN set -ex; \
101+
# Install required system packages
102+
apt-get update; \
103+
apt-get install -qy --no-install-recommends \
104+
unzip \
105+
; \
106+
# Clean aptitude cache and tmp directory
107+
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*;
108+
109+
# Install Node
110+
RUN set -ex; \
111+
curl -L -o /tmp/nodejs.tar.gz https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz; \
112+
tar xfvz /tmp/nodejs.tar.gz -C /usr/local --strip-components=1; \
113+
rm -f /tmp/nodejs.tar.gz; \
114+
npm install yarn -g
115+
116+
# Install Composer
117+
RUN set -ex; \
118+
EXPECTED_SIGNATURE="$(curl -L https://getcomposer.org/download/${COMPOSER_VERSION}/composer.phar.sha256sum)"; \
119+
curl -L -o composer.phar https://getcomposer.org/download/${COMPOSER_VERSION}/composer.phar; \
120+
ACTUAL_SIGNATURE="$(sha256sum composer.phar)"; \
121+
if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]; then >&2 echo 'ERROR: Invalid installer signature' && rm /usr/local/bin/composer && exit 1 ; fi; \
122+
chmod +x composer.phar && mv composer.phar /usr/local/bin/composer; \
123+
RESULT=$?; \
124+
exit $RESULT;
125+
126+
# Edit OPCache configuration
127+
RUN set -ex; \
128+
{ \
129+
echo 'opcache.validate_timestamps = 1'; \
130+
echo 'opcache.revalidate_freq = 0'; \
131+
} >> /usr/local/etc/php/php.ini
132+
133+
# Install Xdebug
134+
RUN set -ex; \
135+
if [ "${XDEBUG_VERSION}" != 0 ]; \
136+
then \
137+
pecl install xdebug-${XDEBUG_VERSION}; \
138+
docker-php-ext-enable xdebug; \
139+
{ \
140+
echo 'xdebug.remote_enable = on'; \
141+
echo 'xdebug.remote_connect_back = on'; \
142+
} >> /usr/local/etc/php/php.ini \
143+
; fi
144+
145+
146+
#####################################
147+
## PROD ASSETS BUILDER ##
148+
#####################################
149+
FROM node:${NODE_VERSION} as assets-builder
150+
151+
COPY . /app
152+
WORKDIR /app
153+
154+
RUN yarn install && yarn build && rm -R node_modules
155+
156+
#####################################
157+
## PROD VENDOR BUILDER ##
158+
#####################################
159+
FROM composer:${COMPOSER_VERSION} as vendor-builder
160+
161+
COPY --from=assets-builder /app /app
162+
WORKDIR /app
163+
164+
RUN APP_ENV=prod composer install -o -n --no-ansi --no-dev
165+
166+
167+
#####################################
168+
## APP PROD ##
169+
#####################################
170+
FROM app as app-prod
171+
172+
ENV APP_ENV=prod
173+
174+
COPY --from=vendor-builder /app /app
175+
WORKDIR /app
176+
177+
# Edit OPCache configuration
178+
RUN set -ex; \
179+
{ \
180+
echo 'opcache.validate_timestamps = 0'; \
181+
} >> /usr/local/etc/php/php.ini

Makefile

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
DOCKER_COMPOSE?=docker-compose
2+
EXEC?=$(DOCKER_COMPOSE) exec app
3+
CONSOLE=bin/console
4+
PHPCSFIXER?=$(EXEC) php -d memory_limit=1024m vendor/bin/php-cs-fixer
5+
6+
.DEFAULT_GOAL := help
7+
.PHONY: help start stop restart install uninstall reset clear-cache tty clear clean
8+
.PHONY: db-diff db-migrate db-rollback db-reset db-validate wait-for-db
9+
.PHONY: watch assets assets-build
10+
.PHONY: tests lint lint-symfony lint-yaml lint-twig lint-twig php-cs php-cs-fix security-check test-schema test-all
11+
.PHONY: build up perm
12+
.PHONY: docker-compose.override.yml
13+
14+
help:
15+
@grep -E '(^[a-zA-Z_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'
16+
17+
##
18+
## Project setup
19+
##---------------------------------------------------------------------------
20+
21+
start: ## Start docker containers
22+
$(DOCKER_COMPOSE) start
23+
24+
stop: ## Stop docker containers
25+
$(DOCKER_COMPOSE) stop
26+
27+
restart: ## Restart docker containers
28+
$(DOCKER_COMPOSE) restart
29+
30+
install: docker-compose.override.yml build up vendor perm ## Create and start docker containers
31+
32+
uninstall: stop ## Remove docker containers
33+
$(DOCKER_COMPOSE) rm -vf
34+
35+
reset: uninstall install ## Remove and re-create docker containers
36+
37+
clear-cache: perm
38+
$(EXEC) $(CONSOLE) cache:clear --no-warmup
39+
$(EXEC) $(CONSOLE) cache:warmup
40+
41+
tty: ## Run app container in interactive mode
42+
$(EXEC) /bin/bash
43+
44+
clear: perm ## Remove all the cache, the logs, the sessions and the built assets
45+
$(EXEC) rm -rf var/cache/*
46+
$(EXEC) $(CONSOLE) redis:flushall -n
47+
rm -rf var/log/*
48+
rm -rf public/build
49+
rm -f var/.php_cs.cache
50+
51+
clean: clear ## Clear and remove dependencies
52+
rm -rf vendor node_modules
53+
54+
55+
##
56+
## Database
57+
##---------------------------------------------------------------------------
58+
59+
wait-for-db:
60+
$(EXEC) php -r "set_time_limit(60);for(;;){if(@fsockopen('db',3306)){break;}echo \"Waiting for MySQL\n\";sleep(1);}"
61+
62+
db-diff: vendor wait-for-db ## Generate a migration by comparing your current database to your mapping information
63+
$(EXEC) $(CONSOLE) doctrine:migration:diff
64+
65+
db-migrate: vendor wait-for-db ## Migrate database schema to the latest available version
66+
$(EXEC) $(CONSOLE) doctrine:migration:migrate -n
67+
68+
db-rollback: vendor wait-for-db ## Rollback the latest executed migration
69+
$(EXEC) $(CONSOLE) doctrine:migration:migrate prev -n
70+
71+
db-reset: vendor wait-for-db ## Reset the database
72+
$(EXEC) $(CONSOLE) doctrine:database:drop --force --if-exists
73+
$(EXEC) $(CONSOLE) doctrine:database:create --if-not-exists
74+
$(EXEC) $(CONSOLE) doctrine:migrations:migrate -n
75+
76+
db-fixtures: vendor wait-for-db ## Apply doctrine fixtures
77+
$(EXEC) $(CONSOLE) doctrine:fixtures:load -n
78+
79+
db-validate: vendor wait-for-db ## Check the ORM mapping
80+
$(EXEC) $(CONSOLE) doctrine:schema:validate
81+
82+
83+
##
84+
## Assets
85+
##---------------------------------------------------------------------------
86+
87+
watch: node_modules ## Watch the assets and build their development version on change
88+
$(EXEC) yarn watch
89+
90+
assets: node_modules ## Build the development version of the assets
91+
$(EXEC) yarn dev
92+
93+
assets-build: node_modules ## Build the production version of the assets
94+
$(EXEC) yarn build
95+
96+
##
97+
## Tests
98+
##---------------------------------------------------------------------------
99+
100+
tests: ## Run all the PHP tests
101+
$(EXEC) bin/phpunit
102+
103+
lint: lint-symfony php-cs ## Run lint on Twig, YAML, PHP and Javascript files
104+
105+
lint-symfony: lint-yaml lint-twig lint-xliff ## Lint Symfony (Twig and YAML) files
106+
107+
lint-yaml: ## Lint YAML files
108+
$(EXEC) $(CONSOLE) lint:yaml config
109+
110+
lint-twig: ## Lint Twig files
111+
$(EXEC) $(CONSOLE) lint:twig templates
112+
113+
lint-xliff: ## Lint Translation files
114+
$(EXEC) $(CONSOLE) lint:xliff translations
115+
116+
php-cs: vendor ## Lint PHP code
117+
$(PHPCSFIXER) fix --diff --dry-run --no-interaction -v
118+
119+
php-cs-fix: vendor ## Lint and fix PHP code to follow the convention
120+
$(PHPCSFIXER) fix
121+
122+
security-check: vendor ## Check for vulnerable dependencies
123+
$(EXEC) vendor/bin/security-checker security:check
124+
125+
test-schema: vendor ## Test the doctrine Schema
126+
$(EXEC) $(CONSOLE) doctrine:schema:validate --skip-sync -vvv --no-interaction
127+
128+
test-all: lint test-schema security-check tests ## Lint all, check vulnerable dependencies, run PHP tests
129+
130+
##
131+
132+
133+
# Internal rules
134+
135+
build:
136+
$(DOCKER_COMPOSE) pull --ignore-pull-failures
137+
$(DOCKER_COMPOSE) build --force-rm
138+
139+
up:
140+
$(DOCKER_COMPOSE) up -d --remove-orphans
141+
142+
perm:
143+
$(EXEC) chmod -R 777 node_modules vendor
144+
$(EXEC) chown -R www-data:root node_modules vendor
145+
146+
docker-compose.override.yml:
147+
ifneq ($(wildcard docker-compose.override.yml),docker-compose.override.yml)
148+
@echo docker-compose.override.yml do not exists, copy docker-compose.override.yml.dist to create it, and fill it.
149+
exit 1
150+
endif
151+
152+
153+
# Rules from files
154+
155+
vendor: composer.lock
156+
$(EXEC) composer install -n
157+
158+
composer.lock: composer.json
159+
@echo compose.lock is not up to date.
160+
161+
node_modules: yarn.lock
162+
$(EXEC) yarn install
163+
164+
yarn.lock: package.json
165+
@echo yarn.lock is not up to date.

docker-compose.override.yml.dist

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3.4'
2+
3+
services:
4+
nginx:
5+
ports:
6+
- 127.0.0.1:8080:80
7+
8+
# Uncomment it, if you want to use MySQL (see: Dockerfile, docker-compose.yml)
9+
# db:
10+
# ports:
11+
# - 127.0.0.1:3306:3306

0 commit comments

Comments
 (0)