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

[docker] - Create migration container #859

Merged
merged 6 commits into from
Nov 15, 2022
Merged
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
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = false

[docker-compose.{yaml,yml}]
indent_size = 2

[docker-compose.*.{yaml,yml}]
indent_size = 2

[.babelrc]
indent_style = space
indent_size = 2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
if: matrix.platform == 'linux/amd64'
-
name: Setup Sylius Store
run: docker compose -f ${{ matrix.docker_compose }} up -d
run: LOAD_FIXTURES=1 docker compose -f ${{ matrix.docker_compose }} up -d
if: matrix.platform == 'linux/amd64'
-
name: Health Check Store
Expand Down
22 changes: 19 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ENV PATH="${PATH}:/root/.composer/vendor/bin"
WORKDIR /srv/sylius

# build for production
ARG APP_ENV=prod
ENV APP_ENV=prod

# prevent the reinstallation of vendors at every changes in the source code
COPY composer.* symfony.lock ./
Expand Down Expand Up @@ -132,9 +132,9 @@ COPY docker/php/dev/opcache.ini $PHP_INI_DIR/conf.d/opcache.ini

WORKDIR /srv/sylius

COPY .env.test .env.test_cached ./
ENV APP_ENV=dev

ARG APP_ENV=dev
COPY .env.test .env.test_cached ./

RUN set -eux; \
composer install --prefer-dist --no-autoloader --no-interaction --no-scripts --no-progress; \
Expand All @@ -151,3 +151,19 @@ COPY docker/cron/crontab /etc/crontabs/root

ENTRYPOINT ["crond"]
CMD ["-f"]

FROM sylius_php_prod AS sylius_migrations_prod

COPY docker/migrations/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]

FROM sylius_php_dev AS sylius_migrations_dev

COPY docker/migrations/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

RUN composer dump-autoload --classmap-authoritative

ENTRYPOINT ["docker-entrypoint"]
25 changes: 21 additions & 4 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: .
target: sylius_php_prod
depends_on:
- mysql
- migrations
environment:
APP_DEBUG: 0
APP_ENV: prod
Expand All @@ -28,7 +28,7 @@ services:
context: .
target: sylius_cron
depends_on:
- mysql
- migrations
environment:
APP_ENV: prod
APP_DEBUG: 0
Expand All @@ -37,7 +37,7 @@ services:
PHP_DATE_TIMEZONE: ${PHP_DATE_TIMEZONE:-UTC}
networks:
- sylius

worker:
container_name: worker
command: ["php", "bin/console", "messenger:consume", "main", "catalog_promotion_removal", "--limit=5", "--memory-limit=256M", "--time-limit=600"]
Expand All @@ -46,7 +46,7 @@ services:
context: .
target: sylius_php_prod
depends_on:
- mysql
- migrations
environment:
APP_ENV: prod
APP_DEBUG: 0
Expand All @@ -57,6 +57,23 @@ services:
networks:
- sylius

migrations:
container_name: migrations
build:
context: .
target: sylius_migrations_prod
depends_on:
- mysql
environment:
APP_ENV: prod
APP_DEBUG: 0
APP_SECRET: EDITME
DATABASE_URL: mysql://sylius:${MYSQL_PASSWORD}@mysql/sylius_prod
LOAD_FIXTURES: ${LOAD_FIXTURES:-0}
PHP_DATE_TIMEZONE: ${PHP_DATE_TIMEZONE:-UTC}
networks:
- sylius

mysql:
container_name: mysql
# in production, we may want to use a managed database service
Expand Down
17 changes: 16 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: .
target: sylius_php_dev
depends_on:
- mysql
- migrations
environment:
- APP_ENV=dev
- APP_DEBUG=1
Expand All @@ -22,6 +22,21 @@ services:
# - ./public/media:/srv/sylius/public/media:rw
- public-media:/srv/sylius/public/media:rw

migrations:
container_name: migrations
build:
context: .
target: sylius_migrations_dev
depends_on:
- mysql
environment:
- APP_ENV=dev
- APP_DEBUG=1
- APP_SECRET=EDITME
- DATABASE_URL=mysql://sylius:${MYSQL_PASSWORD:-nopassword}@mysql/sylius
- LOAD_FIXTURES=1
- PHP_DATE_TIMEZONE=${PHP_DATE_TIMEZONE:-UTC}

mysql:
container_name: mysql
image: mysql:5.7 # Sylius is fully working on mysql 8.0 version
Expand Down
25 changes: 25 additions & 0 deletions docker/migrations/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
set -e

attempt_left=20

until php bin/console doctrine:query:sql "select 1" >/dev/null 2>&1;
do
attempt_left=$((attempt_left-1))

if [ "${attempt_left}" -eq "0" ]; then

(>&2 echo "MySQL did not answer. Aborting migrations.")
exit 1
else
(>&2 echo "Waiting for MySQL to be ready...")
fi

sleep 1
done

php bin/console doctrine:migrations:migrate --no-interaction

if [ "$LOAD_FIXTURES" = "1" ]; then
php bin/console sylius:fixtures:load --no-interaction
fi
12 changes: 5 additions & 7 deletions docker/php/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ if [ "$1" = 'php-fpm' ] || [ "$1" = 'bin/console' ]; then
bin/console sylius:theme:assets:install public --no-interaction
fi

until bin/console doctrine:query:sql "select 1" >/dev/null 2>&1; do
(>&2 echo "Waiting for MySQL to be ready...")
sleep 1
done

bin/console doctrine:migrations:migrate --no-interaction
bin/console sylius:fixtures:load --no-interaction
while ping -c1 migrations >/dev/null 2>&1;
do
(>&2 echo "Waiting for Migrations container to finish")
sleep 1;
done;
fi

exec docker-php-entrypoint "$@"