From de2746f08a34a647aed4bbf98ddf5c2c2c43f05d Mon Sep 17 00:00:00 2001 From: Tobias Batch Date: Sat, 8 Jul 2023 08:39:44 +0100 Subject: [PATCH] feat: prod works, dev doesn't --- docker/development/docker-compose.yml | 75 +++++++++++++++++++ docker/development/env.example | 5 ++ .../Dockerfile.nginx => nginx/Dockerfile} | 2 + .../{production => nginx}/nginx_default.conf | 0 docker/production/docker-compose.yml | 6 +- docker/service/Dockerfile | 13 +++- docker/service/entry-point.sh | 35 +++++++-- docker/service/xdebug.ini | 6 ++ 8 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 docker/development/docker-compose.yml create mode 100644 docker/development/env.example rename docker/{production/Dockerfile.nginx => nginx/Dockerfile} (88%) rename docker/{production => nginx}/nginx_default.conf (100%) create mode 100644 docker/service/xdebug.ini diff --git a/docker/development/docker-compose.yml b/docker/development/docker-compose.yml new file mode 100644 index 0000000..961179c --- /dev/null +++ b/docker/development/docker-compose.yml @@ -0,0 +1,75 @@ +version: '3.5' +services: + + sqldb: + image: mysql:5.7 + environment: + - MYSQL_DATABASE=${DOCKER_DB_DATABASE:-arc} + - MYSQL_USER=${DOCKER_DB_USER:-arc} + - MYSQL_PASSWORD=${DOCKER_DB_PASSWORD:-changem3please} + - MYSQL_ROOT_PASSWORD=${DOCKER_DB_ROOT:-reallychangethisplease} +# ports: # Uncomment this to expose your DB on port 3336 +# - 3336:3306 +# volumes: +# Any sql or sh files in this folder (e.g. db restore) are run on initialisation +# See https://hub.docker.com/_/mysql -> Initializing a fresh instance +# - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + command: --default-storage-engine innodb + restart: unless-stopped + + nginx: + image: arc/nginx + ports: + - 8000:80 + volumes: + - public:/opt/project/public:ro + restart: unless-stopped + depends_on: + - arc + healthcheck: + test: wget --spider http://nginx/health || exit 1 + interval: 20s + start_period: 10s + timeout: 10s + retries: 3 + + arc: + build: + context: ../service + target: dev + image: arc:fpm-dev + environment: + APP_ENV: "local" + RUN_AS: "10001:50" + BRANCH: ${BRANCH:-develop} + DB_DATABASE: ${DOCKER_DB_DATABASE:-arc} + DB_HOST: ${DOCKER_DB_HOST:-sqldb} + DB_PASSWORD: ${DOCKER_DB_PASSWORD:-changem3please} + DB_PORT: ${DOCKER_DB_PORT:-3306} + DB_USERNAME: ${DOCKER_DB_USERNAME:-arc} + volumes: + - public:/opt/project/public + - ${APP_ROOT}/app:/opt/project/app + - ${APP_ROOT}/config:/opt/project/config + - ${APP_ROOT}/database:/opt/project/database + - ${APP_ROOT}/resources:/opt/project/resources + - ${APP_ROOT}/routes:/opt/project/routes + - ${APP_ROOT}/tests:/opt/project/tests + # .sh files in this dir will be executed on startup after migrations are run + - ./docker-entrypoint-init.d:/docker-entrypoint-init.d + # Needed if we want to save passport key + # - ./.env:/opt/arc/ # touch .env before starting + restart: unless-stopped +# entrypoint: bash +# stdin_open: true # docker run -i +# tty: true # docker run -t + + mailer: + image: schickling/mailcatcher + ports: + - "${MAILER_SMTP_PORT:-1025}:1025" + - "${MAILER_ADMIN_PORT:-1080}:1080" + +volumes: + public: + mysql: diff --git a/docker/development/env.example b/docker/development/env.example new file mode 100644 index 0000000..07dbe81 --- /dev/null +++ b/docker/development/env.example @@ -0,0 +1,5 @@ +DOCKER_DB_DATABASE=arc +DOCKER_DB_USER=arc +DOCKER_DB_PASSWORD=changem3please +DOCKER_DB_ROOT=reallychangethisplease +APP_ROOT=/home/tobias/usr/arc/service/app \ No newline at end of file diff --git a/docker/production/Dockerfile.nginx b/docker/nginx/Dockerfile similarity index 88% rename from docker/production/Dockerfile.nginx rename to docker/nginx/Dockerfile index c3da58e..b1901cb 100644 --- a/docker/production/Dockerfile.nginx +++ b/docker/nginx/Dockerfile @@ -4,3 +4,5 @@ LABEL version="1.0" LABEL description="Nginx alpine image built to reverse proxy requests to a PHP FPM server" ADD nginx_default.conf /etc/nginx/conf.d/default.conf + +# docker build -t arc/nginx . \ No newline at end of file diff --git a/docker/production/nginx_default.conf b/docker/nginx/nginx_default.conf similarity index 100% rename from docker/production/nginx_default.conf rename to docker/nginx/nginx_default.conf diff --git a/docker/production/docker-compose.yml b/docker/production/docker-compose.yml index e06d4c5..177c2c4 100644 --- a/docker/production/docker-compose.yml +++ b/docker/production/docker-compose.yml @@ -19,10 +19,7 @@ services: restart: unless-stopped nginx: - build: - context: . - dockerfile: Dockerfile.nginx - image: nginx-fpm-reverse-proxy + image: arc/nginx ports: - 8000:80 volumes: @@ -48,6 +45,7 @@ services: DB_PASSWORD: ${DOCKER_DB_PASSWORD:-changem3please} DB_PORT: ${DOCKER_DB_PORT:-3306} DB_USERNAME: ${DOCKER_DB_USERNAME:-arc} + RUN_AS: "10001:50" volumes: - public:/opt/project/public # .sh files in this dir will be executed on startup after migrations are run diff --git a/docker/service/Dockerfile b/docker/service/Dockerfile index 2d3a78b..0f33e20 100644 --- a/docker/service/Dockerfile +++ b/docker/service/Dockerfile @@ -27,6 +27,7 @@ RUN apk add --no-cache \ libc-dev \ libgomp \ libmagic \ + linux-headers \ m4 \ make \ mpc1 \ @@ -68,6 +69,10 @@ RUN docker-php-ext-install -j$(nproc) xsl FROM fpm-php-ext-base AS php-ext-opcache RUN docker-php-ext-install -j$(nproc) opcache +FROM fpm-php-ext-base AS php-ext-xdebug +RUN apk add --no-cache $PHPIZE_DEPS \ + && pecl install xdebug \ + && docker-php-ext-enable xdebug FROM php:${PHP_VER}-fpm-alpine AS fpm-base ARG BRANCH @@ -165,6 +170,7 @@ ENV MAIL_TO_ADMIN_ADDRESS=to@example.com ENV MAIL_TO_ADMIN_NAME='Admin Name' ENV MAIL_TO_DEVELOPER_TEAM=arc@neontribe.co.uk ENV MAIL_TO_DEVELOPER_NAME='User Support' +ENV RUN_AS="" VOLUME [ "/opt/project/storage" ] ENTRYPOINT /entry-point.sh @@ -172,14 +178,17 @@ ENTRYPOINT /entry-point.sh FROM base AS dev # copy kimai develop source COPY --from=git-dev --chown=www-data:www-data /opt/project /opt/project -RUN echo \$PATH +COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini +COPY --from=php-ext-xdebug /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini +COPY --from=php-ext-xdebug /usr/local/lib/php/extensions/no-debug-non-zts-20210902/xdebug.so /usr/local/lib/php/extensions/no-debug-non-zts-20210902/xdebug.so RUN \ export COMPOSER_HOME=/composer && \ composer --no-ansi install --working-dir=/opt/project --optimize-autoloader && \ composer --no-ansi clearcache && \ cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini && \ chown -R www-data:www-data /opt/project /usr/local/etc/php/php.ini && \ - chown -R www-data:www-data /opt/project /usr/local/etc/php/php.ini + chown -R www-data:www-data /opt/project /usr/local/etc/php/php.ini && \ + echo "error_reporting=E_ALL" > /usr/local/etc/php/conf.d/error_reporting.ini ENV APP_ENV=dev ENV memory_limit=256M diff --git a/docker/service/entry-point.sh b/docker/service/entry-point.sh index 2d6b525..ceae549 100755 --- a/docker/service/entry-point.sh +++ b/docker/service/entry-point.sh @@ -13,14 +13,14 @@ function handleStartup() { # in production we will have a .env mounted into the container, this will have (at least) a # APP_KEY, if we don't have a .env we will create one if [ ! -e /opt/project/.env ]; then - if [ "$APP_ENV" == "prod" ]; then + if [ "$APP_ENV" == "production" ]; then echo "No .env file present." echo "Your are running a prod environment version but there is no .env file present" echo "You need to mount one into this container or the system cannot proceed." exit 1 fi fi - + grep APP_KEY .env # shellcheck disable=SC2181 if [ "$?" != 0 ]; then @@ -28,9 +28,13 @@ function handleStartup() { php /opt/project/artisan key:generate fi - # These are idempotent, run them anyway - php /opt/project/artisan migrate - chmod 600 /opt/project/storage/*.key + if [ "$APP_ENV" == "local" ] || [ "$APP_ENV" == "dev" ] || [ "$APP_ENV" == "development" ] ; then + php /opt/project/artisan migrate:refresh --seed --force + else + # These are idempotent, run them anyway + php /opt/project/artisan migrate + chmod 600 /opt/project/storage/*.key + fi php /passport-install.php @@ -48,5 +52,26 @@ function handleStartup() { checkDatabase handleStartup + +if [ -n "$RUN_AS" ]; then + GROUP_ID=${RUN_AS#*:} + USER_ID=${RUN_AS%:*} # drops substring from last occurrence of `SubStr` to end of string + + GROUP_NAME=$(id -ng "$GROUP_ID") + if [ -z "$GROUP_NAME" ]; then + addgroup --gid "$GROUP_ID" arcuser + GROUP_NAME=arcuser + fi + + USER_NAME=$(id -n "$USER_ID") + if [ -z "$USER_NAME" ]; then + adduser -G "$GROUP_NAME" -u "$USER_ID" arcuser + USER_NAME=arcuser + fi + sed -i "s/user = www-data/user = $USER_NAME/g" /usr/local/etc/php-fpm.d/www.conf + sed -i "s/group = www-data/group = $GROUP_NAME/g" /usr/local/etc/php-fpm.d/www.conf +fi + exec php-fpm + exit diff --git a/docker/service/xdebug.ini b/docker/service/xdebug.ini new file mode 100644 index 0000000..9c37b9c --- /dev/null +++ b/docker/service/xdebug.ini @@ -0,0 +1,6 @@ +zend_extension=xdebug + +[xdebug] +xdebug.mode=develop,debug +xdebug.client_host=host.docker.internal +xdebug.start_with_request=yes