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

[WIP] PoC - add Docker for Monofony #239

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
**/*.log
**/*.md
**/*.php~
**/._*
**/.dockerignore
**/.DS_Store
**/.git/
**/.gitattributes
**/.gitignore
**/.gitkeep
**/.gitmodules
**/Dockerfile
**/Thumbs.db
.editorconfig
.php_cs.cache
.travis.yml
composer.phar
docker-compose*.yaml
docker-compose*.yml
docker/mysql/data/
etc/build/*
node_modules/
var/*
vendor/
public/assets/
public/bundles/
public/css/
public/js/
public/media
15 changes: 13 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@
APP_ENV=dev
APP_DEBUG=1
APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629
APP_LOCALE=en_US
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
DATABASE_URL=mysql://[email protected]/app_name_%kernel.environment%
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=application_prod
MYSQL_USER=application
MYSQL_PASSWORD=application_passwd
MYSQL_HOST=mysql

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://$MYSQL_USER:$MYSQL_PASSWORD@$MYSQL_HOST:3306/$MYSQL_DATABASE
###< doctrine/doctrine-bundle ###

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

###> nelmio/cors-bundle ###
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629
MYSQL_DATABASE=application_test

KERNEL_CLASS='App\Kernel'
44 changes: 44 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ env:
- APP_ENV=test
- APP_NAME_CACHE_DIR=$HOME/.app_name-cache
- APP_NAME_BUILD_DIR=etc/build
- MYSQL_HOST=127.0.0.1
- MYSQL_USER=root
- MYSQL_PASSWORD=
- MYSQL_DATABASE=application_test

matrix:
include:
Expand All @@ -18,6 +22,46 @@ matrix:
- TRAVIS_NODE_VERSION="7.5"
services:
- memcached
-
name: "Docker build"

sudo: required

env:
- DOCKER_COMPOSE_VERSION=1.22.0
- APP_NAME_SUITE="docker"

services:
- docker

addons:
apt:
packages:
- docker-ce

before_install:
# Install custom version of docker-composer
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin

# Shutdown vanilla mysql
- sudo service mysql stop
- while sudo lsof -Pi :3306 -sTCP:LISTEN -t; do sleep 1; done

script:
- id
- docker-compose --version
- docker-compose -f docker-compose.yml -f etc/travis/docker-compose.override.yml pull --ignore-pull-failures || true
- docker-compose -f docker-compose.yml -f etc/travis/docker-compose.override.yml build --pull
- docker-compose -f docker-compose.yml -f etc/travis/docker-compose.override.yml up -d

- sleep 60

- docker-compose -f docker-compose.yml -f etc/travis/docker-compose.override.yml logs --no-color --tail=100000

- curl http://localhost/
-
sudo: false
php: 7.2
Expand Down
183 changes: 183 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# the different stages of this Dockerfile are meant to be built into separate images
# https://docs.docker.com/compose/compose-file/#target

ARG PHP_VERSION=7.3
ARG NODE_VERSION=10
ARG NGINX_VERSION=1.16

FROM scratch as scratch

COPY . /code/

FROM php:${PHP_VERSION}-fpm-alpine AS application_php

# persistent / runtime deps
RUN apk add --no-cache \
acl \
file \
gettext \
git \
mariadb-client \
shadow \
;

ARG APCU_VERSION=5.1.17
RUN set -eux; \
apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
coreutils \
freetype-dev \
icu-dev \
libjpeg-turbo-dev \
libpng-dev \
libtool \
libwebp-dev \
libzip-dev \
mariadb-dev \
zlib-dev \
; \
\
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/; \
docker-php-ext-configure zip --with-libzip; \
docker-php-ext-install -j$(nproc) \
exif \
gd \
intl \
pdo_mysql \
zip \
; \
pecl install \
apcu-${APCU_VERSION} \
redis \
; \
pecl clear-cache; \
docker-php-ext-enable \
apcu \
opcache \
redis \
; \
\
runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)"; \
apk add --no-cache --virtual .application-phpexts-rundeps $runDeps; \
\
apk del .build-deps

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY --from=scratch code/docker/php/php.ini /usr/local/etc/php/php.ini
COPY --from=scratch code/docker/php/php-cli.ini /usr/local/etc/php/php-cli.ini
COPY --from=scratch code/docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ARG USER_UID=1000

RUN usermod -u $USER_UID www-data -s /bin/sh

USER www-data
WORKDIR /home/www-data

RUN set -eux; \
composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative; \
composer clear-cache

ENV PATH="${PATH}:/home/www-data/.composer/vendor/bin"

# build for production
ARG APP_ENV=prod

RUN mkdir /home/www-data/application

WORKDIR /home/www-data/application

# prevent the reinstallation of vendors at every changes in the source code
COPY --from=scratch --chown=www-data:www-data code/composer.json code/composer.lock code/symfony.lock ./
RUN set -eux; \
composer install --prefer-dist --no-autoloader --no-scripts --no-progress --no-suggest; \
composer clear-cache

# copy only specifically what we need
COPY --from=scratch --chown=www-data:www-data code/.env code/.env.test ./
COPY --from=scratch --chown=www-data:www-data code/webpack.config.js ./
COPY --from=scratch --chown=www-data:www-data code/assets assets/
COPY --from=scratch --chown=www-data:www-data code/bin bin/
COPY --from=scratch --chown=www-data:www-data code/config config/
COPY --from=scratch --chown=www-data:www-data code/config config/
COPY --from=scratch --chown=www-data:www-data code/public public/
COPY --from=scratch --chown=www-data:www-data code/templates templates/
COPY --from=scratch --chown=www-data:www-data code/translations translations/

RUN set -eux; \
mkdir -p var/cache var/log; \
composer dump-autoload --classmap-authoritative; \
APP_SECRET='' composer run-script post-install-cmd; \
chmod +x bin/console; sync;

VOLUME /home/www-data/application/var

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]

FROM node:${NODE_VERSION}-alpine AS application_nodejs

RUN set -eux; \
apk add --no-cache --virtual .build-deps \
g++ \
gcc \
git \
make \
python \
shadow \
;

COPY --from=scratch code/docker/nodejs/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ARG USER_UID=1000

RUN usermod -u $USER_UID node

USER node

RUN mkdir /home/node/application
RUN mkdir /home/node/application/public

WORKDIR /home/node/application

COPY --from=scratch --chown=node:node code/package.json code/yarn.lock code/webpack.config.js ./
COPY --from=scratch --chown=node:node code/assets assets/
RUN set -eux; \
yarn install; \
yarn cache clean

#COPY --from=scratch --from=application_php --chown=node:node /home/www-data/application/public public/
COPY --from=application_php --chown=node:node /home/www-data/application/vendor/sylius/ui-bundle/Resources/private/ vendor/sylius/ui-bundle/Resources/private/

RUN set -eux; \
NODE_ENV=prod yarn build

ENTRYPOINT ["docker-entrypoint"]
CMD ["yarn", "dev"]

FROM nginx:${NGINX_VERSION}-alpine AS application_nginx

COPY --from=scratch code/docker/nginx/conf.d/default.conf /etc/nginx/conf.d/

WORKDIR /home/www-data/application/

COPY --from=application_php /home/www-data/application/public public/
COPY --from=application_nodejs /home/node/application/public public/

FROM application_php as application_php_runtime

USER www-data

WORKDIR /home/www-data/application

COPY --from=application_nodejs --chown=www-data:www-data /home/node/application/public public/

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
2 changes: 1 addition & 1 deletion config/packages/framework.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
locale: fr_FR
locale: '%env(resolve:APP_LOCALE)%'
secret: '%env(resolve:APP_SECRET)%'

framework:
Expand Down
Loading