Skip to content

Commit

Permalink
[docker] - Create migration container
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Oct 31, 2022
1 parent f2cdf22 commit 57a9232
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
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
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 @@ -129,7 +129,7 @@ COPY docker/php/dev/opcache.ini $PHP_INI_DIR/conf.d/opcache.ini

WORKDIR /srv/sylius

ARG APP_ENV=dev
ENV APP_ENV=dev

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

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

FROM sylius_php_prod AS sylius_migrations

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

ENTRYPOINT ["docker-entrypoint"]
19 changes: 18 additions & 1 deletion 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:
DATABASE_URL: mysql://sylius:${MYSQL_PASSWORD}@mysql/sylius_prod
MAILER_URL: smtp://localhost
Expand All @@ -26,6 +26,22 @@ services:
build:
context: .
target: sylius_cron
depends_on:
- migrations
environment:
DATABASE_URL: mysql://sylius:${MYSQL_PASSWORD}@mysql/sylius_prod
PHP_DATE_TIMEZONE: ${PHP_DATE_TIMEZONE:-UTC}
APP_ENV: prod
APP_DEBUG: 0
APP_SECRET: EDITME
networks:
- sylius

migrations:
container_name: migrations
build:
context: .
target: sylius_migrations
depends_on:
- mysql
environment:
Expand All @@ -34,6 +50,7 @@ services:
APP_ENV: prod
APP_DEBUG: 0
APP_SECRET: EDITME
LOAD_FIXTURES: 1
networks:
- sylius

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
depends_on:
- mysql
environment:
- APP_ENV=prod
- APP_DEBUG=0
- 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 "$@"

0 comments on commit 57a9232

Please sign in to comment.