Skip to content

Commit 9bcfdd9

Browse files
committed
Implement Docker
1 parent 642a2a7 commit 9bcfdd9

9 files changed

+517
-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

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