From 19ba580a62672ede2ef13b0b6fccf0bff3d0a039 Mon Sep 17 00:00:00 2001 From: Nico Wagner Date: Sun, 19 May 2024 12:42:20 +0200 Subject: [PATCH 1/3] build: add Dockerfile with docker compose example fix: installation instructions --- .dockerignore | 6 ++ Dockerfile | 104 ++++++++++++++++++++++++++++++++++ docker/entrypoint.sh | 19 +++++++ documentation/Installation.md | 55 +++++++++++++++++- 4 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker/entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..373d65b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +Dockerfile +docker-compose.yml +node_modules/ +.git/ +.idea/ +.dockerignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8510b35 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,104 @@ +FROM php:8.3-apache-bookworm AS php + +WORKDIR /build + +# install dependencies to build image +RUN \ + apt update && \ + apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev -y && \ + rm -rf /var/lib/apt/lists/* && \ + docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ + docker-php-ext-configure pdo --with-libdir=lib/x86_64-linux-gnu/ && \ + docker-php-ext-install ldap pdo && \ + docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql gd + +RUN pecl install --force redis \ +&& rm -rf /tmp/pear \ +&& docker-php-ext-enable redis + + +# copy files +COPY . . +# get php composer +RUN wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - | php -- +# install composer dependencies +ENV COMPOSER_ALLOW_SUPERUSER=1 +RUN SYMFONY_ENV=prod php composer.phar install -o + + + +FROM node:20-bullseye-slim AS node + +WORKDIR /build +# build frontend files +COPY --from=php /build . +RUN yarn install --frozen-lockfile +RUN yarn build +RUN rm -r node_modules/ + + + +FROM php:8.3-apache-bookworm + +WORKDIR /build + +# add dependencies to final runtime image +RUN \ + apt update && \ + apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev -y && \ + rm -rf /var/lib/apt/lists/* && \ + docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ + docker-php-ext-configure pdo --with-libdir=lib/x86_64-linux-gnu/ && \ + docker-php-ext-install ldap pdo && \ + docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql gd + +RUN pecl install --force redis \ +&& rm -rf /tmp/pear \ +&& docker-php-ext-enable redis + +COPY --from=node /build /var/www/html + +# apply apache2 config for symfony +RUN bash -c 'echo -e "\n\ + ServerAdmin webmaster@localhost\n\ + DocumentRoot /var/www/html/public/\n\ + \n\ + AllowOverride None\n\ + Require all granted\n\ + FallbackResource /index.php\n\ + \n\ +\n\ + ErrorLog \${APACHE_LOG_DIR}/error.log\n\ + CustomLog \${APACHE_LOG_DIR}/access.log combined\n\ +\n\ +\ +" > /etc/apache2/sites-available/000-default.conf' + +EXPOSE 80 + +WORKDIR /var/www/html + +# specify environment variables for .env file + +ENV REDIS_DSN="redis://redis" +ENV APP_ENV="prod" +ENV APP_SECRET="" +ENV DATABASE_URL="mysql://limas:limas@mysql:3306/limas?serverVersion=5.7.9&charset=utf8mb4" +ENV NEXAR_ID="client" +ENV NEXAR_SECRET="secret" +# ISO 3166 (alpha-2) country code +ENV NEXAR_COUNTRY="DE" +# ISO 4217 currency code +ENV NEXAR_CURRENCY="EUR" + +VOLUME ["/var/www/html/data"] + +# run build commands +RUN php bin/console limas:extjs:models +RUN php bin/console cache:warmup + +# overwrite entrypoint for custom startup commands + +RUN chmod +x /var/www/html/docker/entrypoint.sh + +ENTRYPOINT ["/var/www/html/docker/entrypoint.sh"] \ No newline at end of file diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..ca6c0d9 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/env bash + +cd /var/www/html + +# migrate database always to newest state +php bin/console doctrine:migrations:migrate + +# onetime generation +if [[ ! -f /var/www/html/data/.docker_installed ]]; then + php bin/console limas:user:create --role super_admin admin admin@example.com admin + php bin/console lexik:jwt:generate-keypair + php bin/console limas:extjs:models + php bin/console cache:warmup + touch /var/www/html/data/.docker_installed +fi + + +# start apache +docker-php-entrypoint apache2-foreground \ No newline at end of file diff --git a/documentation/Installation.md b/documentation/Installation.md index 2488d97..7ae1616 100644 --- a/documentation/Installation.md +++ b/documentation/Installation.md @@ -9,6 +9,7 @@ Dependencies * php-dom * php-ctype * php-iconv + * php-redis * [Composer](https://getcomposer.org/download/) * MySQL 5.7 server * Nginx or Apache @@ -17,10 +18,10 @@ Dependencies --- 1. Copy or clone this repository into a folder on your server -3. Configure your webserver to serve from the `public/` folder. See [here](https://symfony.com/doc/6.1/setup/web_server_configuration.html) for additional information. +3. Configure your webserver to serve from the `public/` folder. See [here](https://symfony.com/doc/7.0/setup/web_server_configuration.html) for additional information. 4. Copy the global config file `cp .env .env.local` and edit `.env.local`: * Change the line `APP_ENV=dev` to `APP_ENV=prod` -5. Install composer dependencies and generate autoload files: `SYMFONY_ENV=prod composer install --no-dev -o` +5. Install composer dependencies and generate autoload files: `SYMFONY_ENV=prod composer install -o` 6. Install client side dependencies and build it: `yarn install` and `yarn build` 7. Create database: `php bin/console doctrine:migrations:migrate` 8. Create superadmin account: `php bin/console limas:user:create --role super_admin admin admin@example.com admin` @@ -28,3 +29,53 @@ Dependencies 9. Generate models.js asset: `php bin/console limas:extjs:models` 10. _Optional_ generate JWT keypair: `php bin/console lexik:jwt:generate-keypair` 11. _Optional_ (speeds up first load): Warmup cache: `php bin/console cache:warmup` + + +## Docker Usage + +Manually build the docker image with: `docker build -t local-limas-build .` + +Run the image with: `docker run --name limas -p 8080:80 -v "./data:/var/www/html/data" -e "APP_ENV=prod" -e "DATABASE_URL=mysql://username:password@mysql_host:3306/database-name?serverVersion=5.7.9&charset=utf8mb4"` + +On the first start an admin user with `admin@example.com` and `admin` will be created, as well as the database migrations, which run on every start. + +A docker-compose example can be found here: +```yaml +services: + db: + image: mysql + restart: unless-stopped + ports: + - 3306:3306 + environment: + MYSQL_ROOT_PASSWORD: example + MYSQL_DATABASE: limas + MYSQL_USER: limas + MYSQL_PASSWORD: limas + limas: + image: local-limas-build + restart: unless-stopped + ports: + - 8080:80 + volumes: + - ./data:/var/www/html/data + environment: + APP_ENV: prod + DATABASE_URL: mysql://limas:limas@db:3306/limas?serverVersion=5.7.9&charset=utf8mb4 + redis: + image: redis:7-alpine + restart: unless-stopped +``` + +All Enviroment variables with their defaul values: + +| Environment variable name | default value | +|---------------------------|----------------------------------------------------------------------------| +| REDIS_DSN | `redis://redis` | +| APP_ENV | `prod` (can only be set to `dev` or `prod`) | +| APP_SECRET | _empty_ | +| DATABASE_URL | `mysql://limas:limas@mysql:3306/limas?serverVersion=5.7.9&charset=utf8mb4` | +| NEXAR_ID | `client` | +| NEXAR_SECRET | `secret` | +| NEXAR_COUNTRY | `DE` | +| NEXAR_CURRENCY | `EUR` | From 372113c08480f1e448aa782d02c56e8b7249cdaf Mon Sep 17 00:00:00 2001 From: Nico Wagner Date: Sun, 19 May 2024 12:47:51 +0200 Subject: [PATCH 2/3] build: add missing postgresql driver --- Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8510b35..f7a6e1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,12 +5,12 @@ WORKDIR /build # install dependencies to build image RUN \ apt update && \ - apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev -y && \ + apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev libpq-dev -y && \ rm -rf /var/lib/apt/lists/* && \ docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ docker-php-ext-configure pdo --with-libdir=lib/x86_64-linux-gnu/ && \ docker-php-ext-install ldap pdo && \ - docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql gd + docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql pdo_pgsql gd RUN pecl install --force redis \ && rm -rf /tmp/pear \ @@ -45,12 +45,12 @@ WORKDIR /build # add dependencies to final runtime image RUN \ apt update && \ - apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev -y && \ + apt install libldap2-dev wget zlib1g-dev libpng-dev libzip-dev curl libcurl4-gnutls-dev libxml2-dev libpq-dev -y && \ rm -rf /var/lib/apt/lists/* && \ docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ docker-php-ext-configure pdo --with-libdir=lib/x86_64-linux-gnu/ && \ docker-php-ext-install ldap pdo && \ - docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql gd + docker-php-ext-install zip curl bcmath dom ctype iconv pdo_mysql pdo_pgsql gd RUN pecl install --force redis \ && rm -rf /tmp/pear \ From 0c5d8304a8410b7d6582545cb38f3a00c9639ff3 Mon Sep 17 00:00:00 2001 From: Nico Wagner Date: Sun, 19 May 2024 12:51:02 +0200 Subject: [PATCH 3/3] docs: update docker readme file --- documentation/Installation.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/documentation/Installation.md b/documentation/Installation.md index 7ae1616..05ca1ed 100644 --- a/documentation/Installation.md +++ b/documentation/Installation.md @@ -67,7 +67,11 @@ services: restart: unless-stopped ``` -All Enviroment variables with their defaul values: +Connect to the application via `http://localhost:8080` (or the corresponding hostname of your server) + +To add SSL, you need a reverse proxy with SSL configured, like traefik. + +All Environment variables with their default values: | Environment variable name | default value | |---------------------------|----------------------------------------------------------------------------|