Skip to content

Commit

Permalink
Merge pull request #2 from jakubtobiasz/docker
Browse files Browse the repository at this point in the history
Add a simple Docker configuration
  • Loading branch information
lchrusciel authored Oct 21, 2024
2 parents 3c9fa40 + 27b4b2f commit 340bf7b
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 310 deletions.
13 changes: 13 additions & 0 deletions .docker/frankenphp/Caddyfile
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions .docker/frankenphp/php.ini
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .docker/frankenphp/xdebug.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[xdebug]
xdebug.mode=debug
xdebug.client_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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/sylius_%kernel.environment%?serverVersion=15&charset=utf8
###< doctrine/doctrine-bundle ###

Expand Down
175 changes: 17 additions & 158 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
80 changes: 33 additions & 47 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ $ open http://localhost:8000/
For more detailed instruction please visit [installation chapter in our docs](https://docs.sylius.com/en/latest/book/installation/installation.html).

### Docker

#### 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/
```
Loading

0 comments on commit 340bf7b

Please sign in to comment.