Skip to content

Implementation of the Docker environment #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# Set variables here that may be different on each deployment target of the app, e.g. development, staging, production.
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

POSTGRES_USER=symfony
POSTGRES_PASSWORD=YourPwdShouldBeLongAndSecure!
POSTGRES_DB=demo

###> symfony/framework-bundle ###
APP_ENV=dev
APP_DEBUG=1
Expand Down
135 changes: 135 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
PHP_SERVICE := docker-compose exec php sh -c

export APP_ENV := $(shell grep APP_ENV $(SELF_DIR).env | awk -F '=' '{print $$NF}')

##
## ----------------------------------------------------------------------------
## Environment
## ----------------------------------------------------------------------------
##

build: ## Build the environment
docker-compose build

config: ## Generate the ".env" and "phpunit.xml" files if they don't already exist and update Doctrine configuration
-@test -f .env || \
(cp .env.dist .env && \
export $$(cat .env | grep -v ^\# | xargs) && \
perl -i -pe "s|^DATABASE_URL=.*|DATABASE_URL=postgres://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}\@postgres:5432/$${POSTGRES_DB}|g" .env)
-@test -f phpunit.xml || \
(cp phpunit.xml.dist phpunit.xml && \
export $$(cat .env | grep -v ^\# | xargs) && \
perl -i -pe "s|\"DATABASE_URL\" value=\".*\"|\"DATABASE_URL\" value=\"postgres://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}\@postgres:5432/$${POSTGRES_DB}\"|g" phpunit.xml)
-perl -i -pe "s|driver: 'pdo_sqlite'|driver: 'pdo_pgsql'|g" config/packages/doctrine.yaml
-perl -i -pe "s|server_version: '3.15'|server_version: '10.5'|g" config/packages/doctrine.yaml
-perl -i -pe "s|charset: utf8mb4|charset: 'utf8'|g" config/packages/doctrine.yaml
@printf "doctrine:\n dbal:\n url: '%%env(resolve:DATABASE_URL)%%_test'\n" > config/packages/test/doctrine.yaml

install: ## Install the environment
make config build start composer yarn
@make reset
-open http://localhost:8000

logs: ## Follow logs generated by all containers
docker-compose logs -f --tail=0

logs-full: ## Follow logs generated by all containers from the containers creation
docker-compose logs -f

ps: ## List all containers managed by the environment
docker-compose ps

restart: ## Restart the environment
docker-compose restart

start: ## Start the environment
docker-compose up -d --remove-orphans

stats: ## Print real-time statistics about containers ressources usage
docker stats $(docker ps --format={{.Names}})

stop: ## Stop the environment
docker-compose stop

uninstall: ## Uninstall the environment
make config
docker-compose kill
docker-compose down --volumes --remove-orphans

.PHONY: build config install logs logs-full ps restart start stats stop uninstall

##
## ----------------------------------------------------------------------------
## Project
## ----------------------------------------------------------------------------
##

composer: ## Install Composer dependencies from the "php" container
$(PHP_SERVICE) "composer install --optimize-autoloader"

encore-dev: ## Compile assets once with Encore/Webpack
$(PHP_SERVICE) "yarn run encore dev"

encore-prod: ## Compile assets once with Encore/Webpack and minify & optimize them
$(PHP_SERVICE) "yarn run encore production"

encore-watch: ## Compile assets automatically with Encore/Webpack when files change
$(PHP_SERVICE) "yarn run encore dev --watch"

nginx: ## Open a terminal in the "nginx" container
docker-compose exec nginx sh

php: ## Open a terminal in the "php" container
docker-compose exec php sh

reset: ## Reset the database used by the specified environment
$(PHP_SERVICE) "export APP_ENV=${APP_ENV} && \
php bin/console doctrine:database:drop --if-exists --force && \
php bin/console doctrine:database:create --if-not-exists && \
php bin/console doctrine:schema:create --no-interaction && \
php bin/console doctrine:fixtures:load --no-interaction"

yarn: ## Install Yarn dependencies from the "php" container"
$(PHP_SERVICE) "yarn install"

.PHONY: composer encore-dev encore-prod encore-watch nginx php reset yarn

##
## ----------------------------------------------------------------------------
## Quality
## ----------------------------------------------------------------------------
##

check: ## Execute all quality assurance tools
make lint phpcsfixer phpunit security

lint: ## Lint YAML configuration, Twig templates and JavaScript files
$(PHP_SERVICE) "php bin/console lint:yaml config"
$(PHP_SERVICE) "php bin/console lint:twig templates"

phpcsfixer: ## Run the PHP coding standards fixer on dry-run mode
@test -f .php_cs || cp .php_cs.dist .php_cs
$(PHP_SERVICE) "php vendor/bin/php-cs-fixer fix --config=.php_cs \
--cache-file=var/cache/.php_cs --verbose --dry-run"

phpcsfixer-apply: ## Run the PHP coding standards fixer on apply mode
@test -f .php_cs || cp .php_cs.dist .php_cs && \
$(PHP_SERVICE) "php vendor/bin/php-cs-fixer fix --config=.php_cs \
--cache-file=var/cache/.php_cs --verbose"

phpunit: ## Run the tests suit (unit & functional)
@make reset APP_ENV=test
$(PHP_SERVICE) "./bin/phpunit"

security: ## Run a security analysis on dependencies
$(PHP_SERVICE) "php bin/console security:check"

.PHONY: check lint phpcsfixer phpcsfixer-apply phpunit security

.DEFAULT_GOAL := help
help:
@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/'
.PHONY: help
36 changes: 36 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3"

services:
nginx:
image: nginx:1-alpine
env_file: .env
depends_on:
- php
ports:
- 8000:80
volumes:
- .:/var/www/html:rw,delegated
- ./docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
working_dir: /var/www/html/
tty: true

php:
build: docker/php
env_file: .env
volumes:
- ~/.ssh:/root/.ssh:ro
- .:/var/www/html:rw,delegated
- ./docker/php/conf.d/custom.ini:/usr/local/etc/php/conf.d/custom.ini:ro
tty: true

postgres:
image: postgres:10-alpine
env_file: .env
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data
tty: true

volumes:
postgres: {}
34 changes: 34 additions & 0 deletions docker/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
server {
root /var/www/html/public;

location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}

location ~ ^/index\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;

# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}

# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
26 changes: 26 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM php:7.2-fpm-alpine

# Install Symfony requirements
RUN \
apk add --no-cache --virtual .persistent-deps \
git \
icu-libs \
postgresql-libs \
yarn \
zlib-dev && \
apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
icu-dev \
postgresql-dev && \
docker-php-ext-install -j$(nproc) \
intl \
opcache \
pdo_pgsql \
zip && \
apk del .build-deps

# Install Composer globally
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /var/www/html/
14 changes: 14 additions & 0 deletions docker/php/conf.d/custom.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[core]
date.timezone = UTC
session.auto_start = Off
short_open_tag = Off
realpath_cache_size = 4096K
realpath_cache_ttl = 600

[opcache]
opcache.enable_file_override = On
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 20000
opcache.memory_consumption = 256
#opcache.revalidate_freq = 2
opcache.validate_timestamps = On
2 changes: 1 addition & 1 deletion tests/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testNewComment()
public function testAjaxSearch()
{
$client = static::createClient();
$client->xmlHttpRequest('GET', '/en/blog/search', ['q' => 'lorem']);
$client->xmlHttpRequest('GET', '/en/blog/search', ['q' => 'Lorem']);

$results = json_decode($client->getResponse()->getContent(), true);

Expand Down