diff --git a/.docker/frankenphp/Caddyfile b/.docker/frankenphp/Caddyfile new file mode 100644 index 0000000..87d83da --- /dev/null +++ b/.docker/frankenphp/Caddyfile @@ -0,0 +1,13 @@ +{ + auto_https off + frankenphp + order php_server before file_server + log { + level DEBUG + } +} + +:80, :443 { + root * {$PUBLIC_DIR:/app/public} + php_server +} diff --git a/.docker/frankenphp/php.ini b/.docker/frankenphp/php.ini new file mode 100644 index 0000000..a581750 --- /dev/null +++ b/.docker/frankenphp/php.ini @@ -0,0 +1,11 @@ +[PHP] +memory_limit=1024M +session.auto_start = Off +short_open_tag = Off +post_max_size = 6M +upload_max_filesize = 5M + +[date] +date.timezone=Europe/Warsaw + +memory_limit=-1 diff --git a/.docker/frankenphp/xdebug.ini b/.docker/frankenphp/xdebug.ini new file mode 100644 index 0000000..9188f92 --- /dev/null +++ b/.docker/frankenphp/xdebug.ini @@ -0,0 +1,6 @@ +ient_host=host.docker.internal +xdebug.client_port=9003 +xdebug.idekey=PHPSTORM +xdebug.start_with_request=trigger +xdebug.log=/tmp/xdebug.log +xdebug.log_level=7 diff --git a/.env b/.env index e536f00..ce61c0b 100644 --- a/.env +++ b/.env @@ -10,7 +10,7 @@ APP_SECRET=EDITME ###> doctrine/doctrine-bundle ### # Choose one of the following DBMS, adjust the server version and charset if needed -DATABASE_URL=mysql://root@127.0.0.1/sylius_%kernel.environment%?serverVersion=8&charset=utf8mb4 +DATABASE_URL=mysql://root:root_password@db/sylius_%kernel.environment%?serverVersion=8&charset=utf8mb4 #DATABASE_URL=pgsql://postgres:postgres@127.0.0.1/sylius_%kernel.environment%?serverVersion=15&charset=utf8 ###< doctrine/doctrine-bundle ### diff --git a/Dockerfile b/Dockerfile index 63b9f8d..473ac07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,171 +1,30 @@ -# the different stages of this Dockerfile are meant to be built into separate images -# https://docs.docker.com/compose/compose-file/#target +FROM dunglas/frankenphp:alpine AS frankenphp -ARG PHP_VERSION=8.1 -ARG NODE_VERSION=16 -ARG NGINX_VERSION=1.21 -ARG ALPINE_VERSION=3.15 -ARG COMPOSER_VERSION=2.4 -ARG PHP_EXTENSION_INSTALLER_VERSION=latest +ADD .docker/frankenphp/Caddyfile /etc/caddy/ +ADD .docker/frankenphp/php.ini "$PHP_INI_DIR/" +ADD .docker/frankenphp/xdebug.ini "$PHP_INI_DIR/conf.d/" -FROM composer:${COMPOSER_VERSION} AS composer +RUN install-php-extensions ast exif gd intl pdo pdo_mysql pdo_pgsql soap zip xdebug xsl ftp redis -FROM mlocati/php-extension-installer:${PHP_EXTENSION_INSTALLER_VERSION} AS php_extension_installer - -FROM php:${PHP_VERSION}-fpm-alpine${ALPINE_VERSION} AS base - -# persistent / runtime deps -RUN apk add --no-cache \ - acl \ - file \ - gettext \ - unzip \ - ; - -COPY --from=php_extension_installer /usr/bin/install-php-extensions /usr/local/bin/ - -# default PHP image extensions -# ctype curl date dom fileinfo filter ftp hash iconv json libxml mbstring mysqlnd openssl pcre PDO pdo_sqlite Phar -# posix readline Reflection session SimpleXML sodium SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib -RUN install-php-extensions apcu exif gd intl pdo_mysql opcache zip - -COPY --from=composer /usr/bin/composer /usr/bin/composer -COPY docker/php/prod/php.ini $PHP_INI_DIR/php.ini -COPY docker/php/prod/opcache.ini $PHP_INI_DIR/conf.d/opcache.ini - -# copy file required by opcache preloading -COPY config/preload.php /srv/sylius/config/preload.php - -# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser ENV COMPOSER_ALLOW_SUPERUSER=1 -RUN set -eux; \ - composer clear-cache -ENV PATH="${PATH}:/root/.composer/vendor/bin" - -WORKDIR /srv/sylius - -# build for production -ENV APP_ENV=prod - -# prevent the reinstallation of vendors at every changes in the source code -COPY composer.* symfony.lock ./ -RUN set -eux; \ - composer install --prefer-dist --no-autoloader --no-interaction --no-scripts --no-progress --no-dev; \ - composer clear-cache - -# copy only specifically what we need -COPY .env .env.prod ./ -COPY assets assets/ -COPY bin bin/ -COPY config config/ -COPY public public/ -COPY src src/ -COPY templates templates/ -COPY 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; \ - bin/console sylius:install:assets --no-interaction; \ - bin/console sylius:theme:assets:install public --no-interaction - -VOLUME /srv/sylius/var - -VOLUME /srv/sylius/public/media - -COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint -RUN chmod +x /usr/local/bin/docker-entrypoint - -ENTRYPOINT ["docker-entrypoint"] -CMD ["php-fpm"] - -FROM node:${NODE_VERSION}-alpine${ALPINE_VERSION} AS sylius_node - -WORKDIR /srv/sylius - -RUN set -eux; \ - apk add --no-cache --virtual .build-deps \ - g++ \ - gcc \ - make \ - ; +ENV COMPOSER_MEMORY_LIMIT=-1 +ENV COMPOSER_HOME=/.composer -# prevent the reinstallation of vendors at every changes in the source code -COPY package.json yarn.* ./ -RUN set -eux; \ - yarn install; \ - yarn cache clean +RUN mkdir /.composer -COPY --from=base /srv/sylius/vendor/sylius/sylius/src/Sylius/Bundle/UiBundle/Resources/private vendor/sylius/sylius/src/Sylius/Bundle/UiBundle/Resources/private/ -COPY --from=base /srv/sylius/vendor/sylius/sylius/src/Sylius/Bundle/AdminBundle/Resources/private vendor/sylius/sylius/src/Sylius/Bundle/AdminBundle/Resources/private/ -COPY --from=base /srv/sylius/vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Resources/private vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Resources/private/ -COPY --from=base /srv/sylius/assets ./assets - -COPY webpack.config.js ./ -RUN yarn build:prod - -COPY docker/node/docker-entrypoint.sh /usr/local/bin/docker-entrypoint -RUN chmod +x /usr/local/bin/docker-entrypoint - -ENTRYPOINT ["docker-entrypoint"] -CMD ["yarn", "build:prod"] - -FROM base AS sylius_php_prod - -COPY --from=sylius_node /srv/sylius/public/build public/build - -FROM nginx:${NGINX_VERSION}-alpine AS sylius_nginx - -COPY docker/nginx/conf.d/default.conf /etc/nginx/conf.d/ - -WORKDIR /srv/sylius - -COPY --from=base /srv/sylius/public public/ -COPY --from=sylius_node /srv/sylius/public public/ - -FROM sylius_php_prod AS sylius_php_dev - -COPY docker/php/dev/php.ini $PHP_INI_DIR/php.ini -COPY docker/php/dev/opcache.ini $PHP_INI_DIR/conf.d/opcache.ini - -WORKDIR /srv/sylius - -ENV APP_ENV=dev - -COPY .env.test .env.test_cached ./ - -RUN set -eux; \ - composer install --prefer-dist --no-autoloader --no-interaction --no-scripts --no-progress; \ - composer clear-cache - -FROM sylius_php_prod AS sylius_cron - -RUN set -eux; \ - apk add --no-cache --virtual .build-deps \ - apk-cron \ - ; - -COPY docker/cron/crontab /etc/crontabs/root -COPY docker/cron/docker-entrypoint.sh /usr/local/bin/docker-entrypoint -RUN chmod +x /usr/local/bin/docker-entrypoint - -ENTRYPOINT ["docker-entrypoint"] -CMD ["crond", "-f"] +ENV PATH="${PATH}:/root/.composer/vendor/bin" -FROM sylius_php_prod AS sylius_migrations_prod +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer -COPY docker/migrations/docker-entrypoint.sh /usr/local/bin/docker-entrypoint -RUN chmod +x /usr/local/bin/docker-entrypoint +WORKDIR /app -ENTRYPOINT ["docker-entrypoint"] +FROM node:lts AS frontend -FROM sylius_php_dev AS sylius_migrations_dev +RUN apt update && apt install git -COPY docker/migrations/docker-entrypoint.sh /usr/local/bin/docker-entrypoint -RUN chmod +x /usr/local/bin/docker-entrypoint +RUN mkdir -p /app -RUN composer dump-autoload --classmap-authoritative +WORKDIR /app -ENTRYPOINT ["docker-entrypoint"] +ENTRYPOINT ["tail"] +CMD ["-f","/dev/null"] diff --git a/Makefile b/Makefile index c052062..a7113b4 100644 --- a/Makefile +++ b/Makefile @@ -1,47 +1,33 @@ -.PHONY: run - -DOCKER_COMPOSE ?= docker compose -DOCKER_USER ?= "$(shell id -u):$(shell id -g)" -ENV ?= "dev" - -init: - @make -s docker-compose-check - @if [ ! -e compose.override.yml ]; then \ - cp compose.override.dist.yml compose.override.yml; \ - fi - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) run --rm php composer install --no-interaction --no-scripts - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) run --rm nodejs - @make -s install - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) up -d - -run: - @make -s up - -debug: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) -f compose.yml -f compose.override.yml -f compose.debug.yml up -d - -up: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) up -d - -down: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) down - -install: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) run --rm php bin/console sylius:install -s default -n - -clean: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) down -v - -php-shell: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) exec php sh - -node-shell: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) run --rm -i nodejs sh - -node-watch: - @ENV=$(ENV) DOCKER_USER=$(DOCKER_USER) $(DOCKER_COMPOSE) run --rm -i nodejs "npm run watch" - -docker-compose-check: - @which $(DOCKER_COMPOSE) > /dev/null || (echo "Please install docker compose binary" && exit 1) - @echo "You are using \"$(DOCKER_COMPOSE)\" binary" - @echo "Current version is \"$$($(DOCKER_COMPOSE) version)\"" +setup: + @echo "Setting up project..." + @make docker.up + @make backend.setup + @make frontend.setup + +docker.up: + @echo "Starting docker..." + @docker compose up -d + +docker.stop: + @echo "Stopping docker..." + @docker compose stop + +docker.down: + @echo "Stopping and removing docker..." + @docker compose down + +backend.setup: + @echo "Setting up backend..." + @docker compose exec php composer install + @docker compose exec php php bin/console doctrine:database:create --if-not-exists + @docker compose exec php php bin/console doctrine:migrations:migrate -n + @docker compose exec php php bin/console sylius:fixtures:load -n + +frontend.setup: + @echo "Setting up frontend..." + @docker compose exec frontend npm install + @docker compose exec frontend npm run build + +frontend.build: + @echo "Building frontend..." + @docker compose exec frontend npm run build diff --git a/README.md b/README.md index 73c11aa..3d71e81 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ For more detailed instruction please visit [installation chapter in our docs](ht #### Development -Make sure you have installed [Docker](https://docs.docker.com/get-docker/) on your local machine. -Execute `make init` in your favorite terminal and wait some time until the services will be ready. -Then enter `localhost` in your browser or execute `open localhost` in your terminal. +```bash +$ cp compose.override.dist.yml compose.override.yml +$ make setup +$ open http://localhost:9000/ +``` diff --git a/compose.override.dist.yml b/compose.override.dist.yml index b95849f..4ec027f 100644 --- a/compose.override.dist.yml +++ b/compose.override.dist.yml @@ -1,69 +1,9 @@ services: php: - image: ghcr.io/sylius/sylius-php:8.2-fixuid-xdebug-alpine - user: ${DOCKER_USER:-1000:1000} - depends_on: - mysql: - condition: service_healthy - environment: - # You can move these environment variables to your .env.local file - APP_ENV: ${ENV:-prod} - APP_SECRET: EDITME - DATABASE_URL: "mysql://root@mysql/sylius_%kernel.environment%" - MAILER_DSN: smtp://mailhog:1025 - MESSENGER_TRANSPORT_DSN: doctrine://default - SYLIUS_MESSENGER_TRANSPORT_MAIN_DSN: doctrine://default - SYLIUS_MESSENGER_TRANSPORT_MAIN_FAILED_DSN: doctrine://default?queue_name=main_failed - SYLIUS_MESSENGER_TRANSPORT_CATALOG_PROMOTION_REMOVAL_DSN: doctrine://default?queue_name=catalog_promotion_removal - SYLIUS_MESSENGER_TRANSPORT_CATALOG_PROMOTION_REMOVAL_FAILED_DSN: doctrine://default?queue_name=catalog_promotion_removal_failed - PHP_DATE_TIMEZONE: ${PHP_DATE_TIMEZONE:-UTC} - XDEBUG_MODE: debug - XDEBUG_CONFIG: >- - client_host=host.docker.internal - client_port=9003 - log=/dev/stdout - # This should correspond to the server declared in PHPStorm `Preferences | Languages & Frameworks | PHP | Servers` - # Then PHPStorm will use the corresponding path mappings - PHP_IDE_CONFIG: serverName=sylius - extra_hosts: - - "host.docker.internal:host-gateway" - 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 - mysql: - volumes: - - mysql-data:/var/lib/mysql:rw ports: - - "3306:3306" - nginx: - 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" - nodejs: - image: node:${NODE_VERSION:-18}-alpine - user: ${DOCKER_USER:-1000:1000} - working_dir: /srv/sylius - entrypoint: [ "/bin/sh","-c" ] - command: - - | - npm install - npm run build - volumes: - - .:/srv/sylius:rw,cached - - ./public:/srv/sylius/public:rw,delegated + - "9000:80" mailhog: + image: mailhog/mailhog:latest ports: - "8025:8025" - -volumes: - mysql-data: - public-media: + - "1025:1025" diff --git a/compose.yml b/compose.yml index fbdc6a5..da59246 100644 --- a/compose.yml +++ b/compose.yml @@ -1,20 +1,35 @@ services: php: - image: ghcr.io/sylius/sylius-php:8.2-alpine - mysql: - image: mysql:8.0 - platform: linux/amd64 + build: + context: . + dockerfile: Dockerfile + target: frankenphp + volumes: + - ./:/app:delegated + depends_on: + db: + condition: service_healthy + db: + image: mariadb healthcheck: - test: '/usr/bin/mysql --execute "SHOW databases;"' - timeout: 3s - interval: 1s - retries: 10 + test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + start_period: 10s + interval: 10s + timeout: 5s + retries: 3 + volumes: + - database:/var/lib/mysql environment: - MYSQL_ALLOW_EMPTY_PASSWORD: 1 - cap_add: - - SYS_NICE # prevent "mbind: Operation not permitted" errors - nginx: - image: ghcr.io/sylius/sylius-nginx:latest + MARIADB_ROOT_PASSWORD: root_password + frontend: + build: + context: . + dockerfile: Dockerfile + target: frontend + volumes: + - ./:/app mailhog: - # do not use in production! image: mailhog/mailhog:latest + +volumes: + database: