Skip to content
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

Isolate all containers configuration #572

Draft
wants to merge 2 commits into
base: 1.12
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
33 changes: 31 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file
# 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

###> symfony/framework-bundle ###
APP_ENV=dev
APP_DEBUG=1
Expand All @@ -12,7 +11,7 @@ APP_SECRET=EDITME
# 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://root@127.0.0.1/sylius_%kernel.environment%
DATABASE_URL=mysql://root:nopassword@mysql/sylius_${APP_ENV}
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
Expand All @@ -34,3 +33,33 @@ JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=e7c5fca1060bdf6ad23c33e4c236081f
###< lexik/jwt-authentication-bundle ###


###> php ###
PHP_VERSION=7.4
PHP_DATE_TIMEZONE=UTC
PHP_POST_MAX_SIZE=6M
PHP_UPLOAD_MAX_FILESIZE=5M
PHP_OPCACHE_ENABLED=1
PHP_OPCACHE_ENABLED_CLI=1
###< php ###

###> php/fpm ###
PHP_FPM_LOG_LEVEL=notice
PHP_FPM_ERROR_LOG=/srv/sylius/var/log/php-fpm-error.log
PHP_FPM_WWW_LISTEN=9000
# check how to tuning you php-fpm
# https://tideways.com/profiler/blog/an-introduction-to-php-fpm-tuning
#
# | Setting | Value |
# | max_children | (Total RAM – Memory used for Linux, DB, etc.) / process size |
# | start_servers | Number of CPU cores x 4 |
# | min_spare_servers | Number of CPU cores x 2 |
# | max_spare_servers | Same as start_servers |
PHP_FPM_PM=dynamic
PHP_FPM_WWW_PM_MAX_CHILDREN=16
PHP_FPM_WWW_PM_START_SERVERS=16
PHP_FPM_WWW_PM_MIN_SPARE_SERVERS=8
PHP_FPM_WWW_PM_MAX_SPARE_SERVERS=16
PHP_FPM_WWW_PM_MAX_REQUESTS=200
###< php/fpm ###
27 changes: 21 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# 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.4
ARG PHP_VERSION=${PHP_VERSION:-7.4}
ARG NODE_VERSION=10
ARG NGINX_VERSION=1.16
ARG NGINX_VERSION=1.17

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

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

ARG APCU_VERSION=5.1.17
ARG APCU_VERSION=5.1.18
RUN set -eux; \
apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
Expand All @@ -33,6 +34,7 @@ RUN set -eux; \
; \
\
docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-webp=/usr/include --with-freetype=/usr/include/; \
docker-php-ext-configure zip; \
docker-php-ext-install -j$(nproc) \
exif \
gd \
Expand All @@ -59,19 +61,25 @@ RUN set -eux; \
\
apk del .build-deps
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY docker/php/php.ini /usr/local/etc/php/php.ini
COPY docker/php/php-cli.ini /usr/local/etc/php/php-cli.ini
COPY docker/php/php.ini /usr/local/etc/php/php.tmp
COPY docker/php/php-cli.ini /usr/local/etc/php/php-cli.tmp

ARG PHP_DATE_TIMEZONE=${PHP_DATE_TIMEZONE:-UTC}
RUN sh -c "envsubst < /usr/local/etc/php/php.tmp > /usr/local/etc/php/php.ini"
RUN sh -c "envsubst < /usr/local/etc/php/php-cli.tmp > /usr/local/etc/php/php-cli.ini"

# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1
# install Symfony Flex globally to speed up download of Composer packages (parallelized prefetching)
RUN set -eux; \
composer global require "symfony/flex" --prefer-dist --no-progress --classmap-authoritative; \
composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"

WORKDIR /srv/sylius

# build for production
ARG APP_ENV=prod
ARG APP_ENV=${APP_ENV}

# prevent the reinstallation of vendors at every changes in the source code
COPY composer.* symfony.lock ./
Expand All @@ -93,13 +101,20 @@ RUN set -eux; \
composer dump-autoload --classmap-authoritative; \
APP_SECRET='' composer run-script post-install-cmd; \
chmod +x bin/console; sync; \
bin/console assets:install --no-interaction; \
bin/console sylius:install:assets; \
bin/console sylius:theme:assets:install public

VOLUME /srv/sylius/var

VOLUME /srv/sylius/public/media

COPY docker/php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
RUN chmod +x /usr/local/bin/docker-healthcheck

HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]

COPY docker/php/sylius.conf /usr/local/etc/php-fpm.d/zzz-sylius.conf
COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,50 @@ $ php bin/console server:start
$ open http://localhost:8000/
```

### For Docker installation

Running the local environment with containers:

```bash
$ docker-compose pull
$ docker-compose build
$ docker-compose -f docker-compose.yml -f docker/docker-compose.setup.yml run setup

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --rm switch should be added tot he run command in order to remove the created container after finishing the setup. Currently if setup is run multiple times, it will create as many containers, leaving mess behind.

$ docker-compose up -d
```

> follow the installation instructions to setup the application
> database. Also you may add sample data.

> You may check if php-fpm is ready executing the command:
> `docker-composer ps`. When it is ready, the State will be
> **Up (healthy)**

Write the command on terminal or just click on [localhost](http://localhost)
to open the Sylius Shop
```
$ open http://localhost/
```

or [localhost/admin](http://localhost/admin) to open the Sylius Backoffice
```
$ open http://localhost/admin
```

If the pages not load the assets, just run the below commands:

```bash
$ docker-compose run assets yarn install

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--rm switch is needed here too.

$ docker-compose run assets yarn build
```

If you want to see any php information or run any command from
`bin/console`, you can run one of below commands:
```bash
$ docker-compose run php php -i
$ docker-compose run php php-fpm -tt
$ docker-compose run php php bin/console
```

Troubleshooting
---------------

Expand Down
172 changes: 80 additions & 92 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,101 +1,89 @@
version: '3.4'

services:
php:
build:
context: .
target: sylius_php
# Quay does not work, should be replaced in future with f.e. ghcr.io
# cache_from:
# - quay.io/sylius/php:latest
# - quay.io/sylius/nodejs:latest
# - quay.io/sylius/nginx:latest
image: php:latest
depends_on:
- mysql
environment:
- APP_ENV=dev
- APP_DEBUG=1
- APP_SECRET=EDITME
- DATABASE_URL=mysql://sylius:nopassword@mysql/sylius
- MAILER_URL=smtp://mailhog:1025
- PHP_DATE_TIMEZONE=${PHP_DATE_TIMEZONE:-UTC}
volumes:
- .:/srv/sylius:rw,cached
# if you develop on Linux, you may use a bind-mounted host directory instead
# - ./var:/srv/sylius/var:rw
- ./public:/srv/sylius/public:rw,delegated
# if you develop on Linux, you may use a bind-mounted host directory instead
# - ./public/media:/srv/sylius/public/media:rw
- public-media:/srv/sylius/public/media:rw
sylius:
build:
context: .
target: sylius_server
dockerfile: docker/sylius/Dockerfile
image: sylius/server:latest
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s
depends_on:
- mysql
env_file:
- .env
volumes:
- .:/srv/sylius:rw,cached
- ./public:/srv/sylius/public:rw,delegated
- public-media:/srv/sylius/public/media:rw
- sylius-vendor:/srv/sylius/vendor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a named volume makes the vendor folder empty on the host machine. It's a problem, because your IDE can't see and the installed dependencies.

- sylius-node_modules:/srv/sylius/node_modules

mysql:
image: percona:5.7
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-nopassword}
- MYSQL_DATABASE=sylius
- MYSQL_USER=sylius
- MYSQL_PASSWORD=${MYSQL_PASSWORD:-nopassword}
volumes:
- mysql-data:/var/lib/mysql:rw
# you may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
# - ./docker/mysql/data:/var/lib/mysql:rw,delegated
ports:
- "3306:3306"
assets:
build:
context: .
dockerfile: docker/assets/Dockerfile
target: sylius_assets
args:
- APP_ENV=${APP_ENV}
image: sylius/assets:latest
depends_on:
- sylius
env_file:
- .env
volumes:
- .:/srv/sylius:rw,cached
- ./public:/srv/sylius/public:rw,delegated
- public-media:/srv/sylius/public/media:rw
- sylius-node_modules:/srv/sylius/node_modules
ports:
- "35729:35729"

node:
build:
context: .
target: sylius_node
# Quay does not work, should be replaced in future with f.e. ghcr.io
# cache_from:
# - quay.io/sylius/php:latest
# - quay.io/sylius/nodejs:latest
# - quay.io/sylius/nginx:latest
image: node:latest
depends_on:
- php
environment:
- GULP_ENV=dev
- PHP_HOST=php
- PHP_PORT=9000
volumes:
- .:/srv/sylius:rw,cached
- ./public:/srv/sylius/public:rw,delegated
ports:
- "35729:35729"
webserver:
build:
context: .
target: sylius_nginx
dockerfile: docker/nginx/Dockerfile
image: nginx:latest
env_file:
- .env
depends_on:
- sylius
- assets
volumes:
- ./public:/srv/sylius/public:ro
- public-media:/srv/sylius/public/media:ro,delegated
ports:
- "80:80"

nginx:
build:
context: .
target: sylius_nginx
image: nginx:latest
# Quay does not work, should be replaced in future with f.e. ghcr.io
# cache_from:
# - quay.io/sylius/php:latest
# - quay.io/sylius/nodejs:latest
# - quay.io/sylius/nginx:latest
depends_on:
- php
- node # to ensure correct build order
volumes:
- ./public:/srv/sylius/public:ro
# if you develop on Linux, you may use a bind-mounted host directory instead
# - ./public/media:/srv/sylius/public/media:ro
- public-media:/srv/sylius/public/media:ro,nocopy
ports:
- "80:80"
mysql:
image: percona:5.7
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-nopassword}
- MYSQL_DATABASE=sylius_${APP_ENV}
- MYSQL_USER=${MYSQL_USER:-sylius}
- MYSQL_PASSWORD=${MYSQL_PASSWORD:-nopassword}
volumes:
- mysql-data:/var/lib/mysql:rw
ports:
- "3306:3306"

mailhog:
# do not use in production!
image: mailhog/mailhog:latest
environment:
- MH_STORAGE=maildir
# volumes:
# - ./docker/mailhog/maildir:/maildir:rw,delegated
ports:
- "8025:8025"
mailhog:
# do not use in production!
image: mailhog/mailhog:latest
environment:
- MH_STORAGE=maildir
# volumes:
# - ./docker/mailhog/maildir:/maildir:rw,delegated
ports:
- "8025:8025"

volumes:
mysql-data:
public-media:
mysql-data:
public-media:
sylius-vendor:
sylius-node_modules:
Loading