From ea769c3069bccfc0eb0e50a492e1bf537d220778 Mon Sep 17 00:00:00 2001 From: Roomcays Date: Fri, 26 Nov 2021 01:40:40 +0100 Subject: [PATCH] Docker(-Compose) setup to aid local development. --- .docker/README.md | 40 +++++++++++++++++++++ .docker/apache-php/Dockerfile | 18 ++++++++++ .docker/composer.sh | 9 +++++ .docker/example.docker-compose.override.yml | 6 ++++ .docker/example.env | 2 ++ .docker/php.sh | 9 +++++ .editorconfig | 4 +++ .gitignore | 5 ++- docker-compose.yml | 14 ++++++++ 9 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 .docker/README.md create mode 100644 .docker/apache-php/Dockerfile create mode 100755 .docker/composer.sh create mode 100644 .docker/example.docker-compose.override.yml create mode 100644 .docker/example.env create mode 100755 .docker/php.sh create mode 100644 docker-compose.yml diff --git a/.docker/README.md b/.docker/README.md new file mode 100644 index 00000000..31789d06 --- /dev/null +++ b/.docker/README.md @@ -0,0 +1,40 @@ +# Docker to aid local development + +1. Copy `.docker/example.env` to `.env` (in project's root directory), change its contents to reflect your user's UID + and GID respectively. This will make try to force container to not create any files as root. +2. Copy `.docker/example.docker-compose.override.yml` to `docker-compose.override.yml` in application's root directory. + You may alter the HOST port (first one, as they're defined as `HOST:CONTAINER`) leaving container's port as `80`. +3. Run `docker-compose up` (or `start`) and wait for fetching, extracting and building process to complete (this will + happen only once or every time you'd like to rebuild the image) +4. Open separate terminal (or not, if you used `docker-compose start` before) and install project's dependencies like + this: + + ```shell + $ .docker/composer.sh install + ``` +5. Visit `http://localhost` (or `http://localhost:8080` if you have altered `HOST`'s port to `8080` in step 2) to see + Grocy ready for your hacking. +6. Every time you need Composer for something use: + + ```shell + $ .docker/composer.sh [require|update|etc] [other arguments] + ``` +7. You may also use PHP the same way: + + ```shell + $ .docker/php.sh -v + ``` + +Enjoy! + +## BONUS + +Would you like to see what happens to the app when launched on PHP 7.4 or 8.1? Edit `.docker/apache-php/Dockerfile` and +change image tag in the first line from `php:8.0-apache` to `php:7.4-apache` or `php:8.1-apache`. Then make +`docker-compose` rebuild the container for you: + +```shell +$ docker-compose build +``` + +And voila! After `docker-compose up` (or `start`) check what works and what breaks! diff --git a/.docker/apache-php/Dockerfile b/.docker/apache-php/Dockerfile new file mode 100644 index 00000000..9a38f9ea --- /dev/null +++ b/.docker/apache-php/Dockerfile @@ -0,0 +1,18 @@ +# Application's image will be based on... +FROM php:8.0-apache + +# Since web application should be served from `/public` subdirectory change the document root in Apache configuration +ENV APACHE_DOCUMENT_ROOT /var/www/html/public +RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf +RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf + +# Make Rewrite engine enabled in Apache +RUN ln -s -t /etc/apache2/mods-enabled ../mods-available/rewrite.load + +# Install Composer into the image +COPY --from=composer /usr/bin/composer /usr/bin/composer + +# Install PHP dependencies using helper from https://github.com/mlocati/docker-php-extension-installer +COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ +RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" +RUN install-php-extensions gd intl xdebug zip diff --git a/.docker/composer.sh b/.docker/composer.sh new file mode 100755 index 00000000..04c61622 --- /dev/null +++ b/.docker/composer.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +pushd "$(dirname "$0")" > /dev/null +pushd .. > /dev/null + +docker-compose exec apache-php composer "$@" + +popd > /dev/null +popd > /dev/null diff --git a/.docker/example.docker-compose.override.yml b/.docker/example.docker-compose.override.yml new file mode 100644 index 00000000..f9c45f65 --- /dev/null +++ b/.docker/example.docker-compose.override.yml @@ -0,0 +1,6 @@ +version: '3' + +services: + apache-php: + ports: + - 80:80 diff --git a/.docker/example.env b/.docker/example.env new file mode 100644 index 00000000..1a63eeff --- /dev/null +++ b/.docker/example.env @@ -0,0 +1,2 @@ +USER_ID=1000 +GROUP_ID=1000 diff --git a/.docker/php.sh b/.docker/php.sh new file mode 100755 index 00000000..8f888d78 --- /dev/null +++ b/.docker/php.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +pushd "$(dirname "$0")" > /dev/null +pushd .. > /dev/null + +docker-compose exec apache-php php "$@" + +popd > /dev/null +popd > /dev/null diff --git a/.editorconfig b/.editorconfig index f8c5d3d2..4843a5e1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,3 +6,7 @@ indent_size = 4 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore index 9946de1e..1d7c9dde 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ /public/node_modules /vendor /.release -embedded.txt \ No newline at end of file +embedded.txt +/.idea +/.env +/docker-compose.override.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..8232b925 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3' + +services: + apache-php: + build: .docker/apache-php + environment: + DOCKER: "true" + COMPOSER_HOME: /tmp + XDEBUG_CONFIG: "client_host=host.docker.internal" + user: ${USER_ID:-1000}:${GROUP_ID:-1000} + volumes: + - .:/var/www/html + extra_hosts: + - host.docker.internal:host-gateway