From 7e32718ca18820ba2d1ea827fd774dd3394611dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Larsson=20H=C3=B6gfeldt?= Date: Sun, 21 Dec 2025 13:35:34 +0100 Subject: [PATCH] Simpler Docker setup --- docker-for-development/docker-compose.yaml | 66 +++++++++++++++++-- .../migration-runner/Dockerfile | 15 +++++ .../migration-runner/run-migrations.sh | 11 ++++ docker-for-development/node/Dockerfile | 3 - docker-for-development/php-apache/Dockerfile | 23 +------ .../php-apache/entrypoint.sh | 23 ------- .../php-apache/twhl-install.sh | 20 ------ .../php-apache/wait-for-mysql-to-start.php | 28 -------- docker-for-development/readme.md | 44 ++++++------- readme.md | 4 +- 10 files changed, 111 insertions(+), 126 deletions(-) create mode 100644 docker-for-development/migration-runner/Dockerfile create mode 100644 docker-for-development/migration-runner/run-migrations.sh delete mode 100644 docker-for-development/node/Dockerfile delete mode 100644 docker-for-development/php-apache/entrypoint.sh delete mode 100644 docker-for-development/php-apache/twhl-install.sh delete mode 100644 docker-for-development/php-apache/wait-for-mysql-to-start.php diff --git a/docker-for-development/docker-compose.yaml b/docker-for-development/docker-compose.yaml index 4c2ebcc..4f75925 100644 --- a/docker-for-development/docker-compose.yaml +++ b/docker-for-development/docker-compose.yaml @@ -1,5 +1,14 @@ name: twhl-dev services: + composer: + container_name: twhl-dev-composer + command: composer install + environment: + - COMPOSER_IGNORE_PLATFORM_REQS=1 + image: composer:latest + volumes: + - ../:/var/twhl + working_dir: /var/twhl db: command: --collation-server=utf8mb4_unicode_ci @@ -8,27 +17,73 @@ services: - MYSQL_ALLOW_EMPTY_PASSWORD=yes - MYSQL_DATABASE=twhl - MYSQL_ROOT_PASSWORD=cs_canbunk2 + healthcheck: + test: "/usr/bin/mysql --user=root --password=cs_canbunk2 --execute \"SELECT 1;\"" + interval: 5s + timeout: 5s + retries: 10 image: mysql:8.0 # ports: # - "3306:3306" volumes: - ./mysql-data:/var/lib/mysql + migration-runner: + build: + context: ./migration-runner + container_name: twhl-dev-migration-runner + depends_on: + composer: + condition: service_completed_successfully + db: + condition: service_healthy + env_file: + - path: ../.env.example + - path: ../.env + required: false + environment: + - APP_KEY=base64:PB2xpacpFD+vjAcrcj/tBEQ7RzAtvUyufGd4KznJ81k= + - DB_DATABASE=twhl + - DB_HOST=twhl-mysql-db + - DB_PASSWORD=cs_canbunk2 + - DB_USERNAME=root + image: twhl-dev/migration-runner + links: + - "db:twhl-mysql-db" + volumes: + - ../:/var/twhl node: container_name: twhl-dev-node - build: - context: ./node - image: twhl-dev/node + command: npm install + image: node:24 + secrets: + - source: npmrc + target: /root/.npmrc volumes: - ../:/var/twhl + working_dir: /var/twhl php-apache: build: context: ./php-apache container_name: twhl-dev-php-apache + depends_on: + composer: + condition: service_completed_successfully + db: + condition: service_healthy + migration-runner: + condition: service_completed_successfully + node: + condition: service_completed_successfully + env_file: + - path: ../.env.example + - path: ../.env + required: false environment: + - APP_KEY=base64:PB2xpacpFD+vjAcrcj/tBEQ7RzAtvUyufGd4KznJ81k= - DB_DATABASE=twhl - DB_HOST=twhl-mysql-db - DB_PASSWORD=cs_canbunk2 - - DB_USER=root + - DB_USERNAME=root image: twhl-dev/php-apache links: - "db:twhl-mysql-db" @@ -37,3 +92,6 @@ services: volumes: - ../:/var/www/html/twhl - ./php-apache/twhl-dev-php-conf.d:/usr/local/etc/php/conf.d +secrets: + npmrc: + file: ~/.npmrc diff --git a/docker-for-development/migration-runner/Dockerfile b/docker-for-development/migration-runner/Dockerfile new file mode 100644 index 0000000..d6b4e8b --- /dev/null +++ b/docker-for-development/migration-runner/Dockerfile @@ -0,0 +1,15 @@ +FROM php:8.4 + +# Use the PHP development config +RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" + +# Install PHP extensions +COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ +RUN install-php-extensions pdo_mysql + +WORKDIR /var/twhl/ + +# Migration script +COPY ./run-migrations.sh ../run-migrations.sh +RUN chmod +x ../run-migrations.sh +ENTRYPOINT ["../run-migrations.sh"] diff --git a/docker-for-development/migration-runner/run-migrations.sh b/docker-for-development/migration-runner/run-migrations.sh new file mode 100644 index 0000000..eea7bf7 --- /dev/null +++ b/docker-for-development/migration-runner/run-migrations.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +if php artisan migrate:status | grep -q "Migration table not found" +then + echo "Did not find a migration table in the database." + echo "Running 'php artisan migrate --seed'." + echo "If it fails, you may have to rerun it manually." + php artisan migrate --seed +fi +php artisan migrate diff --git a/docker-for-development/node/Dockerfile b/docker-for-development/node/Dockerfile deleted file mode 100644 index 5ebc7ff..0000000 --- a/docker-for-development/node/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM node:22 - -WORKDIR /var/twhl diff --git a/docker-for-development/php-apache/Dockerfile b/docker-for-development/php-apache/Dockerfile index 927fbac..0fdb4b5 100644 --- a/docker-for-development/php-apache/Dockerfile +++ b/docker-for-development/php-apache/Dockerfile @@ -5,10 +5,10 @@ RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" # Install PHP extensions COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ -RUN install-php-extensions gd pdo_mysql @composer +RUN install-php-extensions gd pdo_mysql # Set the document root -ENV APACHE_DOCUMENT_ROOT /var/www/html/twhl/public +ENV APACHE_DOCUMENT_ROOT=/var/www/html/twhl/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 @@ -21,24 +21,5 @@ EXPOSE 80/tcp # If we wanted to store the PHP files etc. in a volume: # VOLUME /var/www/html/twhl -# Environment variables -ENV DB_DATABASE=twhl -ENV DB_HOST=twhl-mysql-db -ENV DB_PASSWORD=cs_canbunk2 -ENV DB_USER=root - -# Script for waiting for the MySQL server to start -COPY ./wait-for-mysql-to-start.php /usr/local/bin/wait-for-mysql-to-start -RUN chmod +x /usr/local/bin/wait-for-mysql-to-start - -# Script for creating a .env file and running Composer -COPY ./twhl-install.sh /usr/local/bin/twhl-install -RUN chmod +x /usr/local/bin/twhl-install - # This is where the good stuff goes, so make that the working directory WORKDIR /var/www/html/twhl/ - -# Startup script -COPY ./entrypoint.sh ../entrypoint.sh -RUN chmod +x ../entrypoint.sh -ENTRYPOINT "../entrypoint.sh" diff --git a/docker-for-development/php-apache/entrypoint.sh b/docker-for-development/php-apache/entrypoint.sh deleted file mode 100644 index 126eb20..0000000 --- a/docker-for-development/php-apache/entrypoint.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -if [ ! -d ./vendor ] || [ ! -f ./.env ] -then - echo "Running 'twhl-install'." - twhl-install -else - echo "Remember to run 'composer install' or 'twhl-install' to install dependencies after pulling or changing branch." -fi - - -wait-for-mysql-to-start -php artisan migrate:status | grep -q "Migration table not found" -if [ $? == 0 ] -then - echo "Did not find a migration table in the database." - echo "Running 'php artisan migrate --seed'." - echo "If it fails, you may have to rerun it manually." - php artisan migrate --seed -fi - -echo "Running 'docker-php-entrypoint'" -docker-php-entrypoint "apache2-foreground" diff --git a/docker-for-development/php-apache/twhl-install.sh b/docker-for-development/php-apache/twhl-install.sh deleted file mode 100644 index f6d5a92..0000000 --- a/docker-for-development/php-apache/twhl-install.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -echo "Running 'composer install'" -composer install - -if [ ! -f ./.env ] -then - echo "You do not have an '.env'. file yet." - echo "Copying '.env.example' to '.env'." - cp ./.env.example ./.env - echo "Replacing database connection details in .env." - sed -i "s/DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" ./.env - sed -i "s/DB_HOST=.*/DB_HOST=${DB_HOST}/g" ./.env - sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=${DB_PASSWORD}/g" ./.env - sed -i "s/DB_USER=.*/DB_USER=${DB_USER}/g" ./.env - echo "Running 'php artisan key:generate'." - php artisan key:generate -else - echo "You have an .env file. Good." -fi diff --git a/docker-for-development/php-apache/wait-for-mysql-to-start.php b/docker-for-development/php-apache/wait-for-mysql-to-start.php deleted file mode 100644 index e34e718..0000000 --- a/docker-for-development/php-apache/wait-for-mysql-to-start.php +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/local/bin/php - 30) { - echo "Failed to connect to the MySQL server. Giving up.\n"; - var_dump($error); - exit(1); - } - } - $delay = ceil($connectionAttempts * 0.5); - echo "Failed to connect to the MySQL server. Retrying in " . $delay . " seconds. The server is probably still starting up.\n"; - sleep($delay); -} - -?> diff --git a/docker-for-development/readme.md b/docker-for-development/readme.md index f1c8746..e0551f2 100644 --- a/docker-for-development/readme.md +++ b/docker-for-development/readme.md @@ -1,30 +1,24 @@ -Delete your `.env` file if you have one and run this in Bash or PowerShell to build the Docker images and start the containers: -``` -docker compose up -``` -The web server can then be reached at http://localhost:82 . +# Dockerized local development -To install the Composer dependencies and create the `.env` file (if it does not exist), run: -``` -docker compose exec php-apache twhl-install -``` +## Starting a local development instance of TWHL +1. Create a [GitHub Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token) with the `read:packages` scope and add it to the file `~/.npmrc` like this: + ``` + //npm.pkg.github.com/:_authToken=ghp_1234example567 + ``` + This step is necessary for the successful installation of the `@logicandtrick/twhl-wikicode-parser` NPM package. +2. Run `docker compose up` to build the Docker images and start the containers. Your local development instance of TWHL can now be reached at http://localhost:82 . +3. When making changes to `*.css`, `*.scss` and `*.js` files, you need to run `docker compose run --rm node npm run development` to bundle those changes into the compiled CSS and JS files. The easiest way to do this is to run `docker compose run --rm node npm run watch`, which will watch the files for changes and auto-build when needed. -To run `npm install` inside the Node container: -``` -docker compose run node npm install -``` +## Updating NPM dependencies +1. Make your changes to package.json +2. Run `docker compose run --rm node npm install` -To start a Bash instance inside the PHP & Apache container for executing commands, run this: -``` -docker compose exec php-apache bash -``` +## Updating Composer PHP dependencies +1. Make your changes to composer.json +2. Run `docker compose run --rm composer composer install` -To see the Apache log, run this: -``` -docker compose logs php-apache -``` +## Running Bash inside the PHP & Apache container for executing commands +Run `docker compose exec php-apache bash` -To rebuild the images: -``` -docker compose build -``` +## Inspecting the Apache logs +Run `docker compose logs php-apache` diff --git a/readme.md b/readme.md index d5508b1..0dc034b 100644 --- a/readme.md +++ b/readme.md @@ -2,11 +2,11 @@ TWHL is a mapping and modding resource website for Half-Life 1 and related games. -- [Visit twhl.info](http://twhl.info) +- [Visit twhl.info](https://twhl.info) ### Getting a dev environment set up -TWHL4 uses the [Laravel](http://laravel.com/) framework. Detailed instructions can be found +TWHL4 uses the [Laravel](https://laravel.com/) framework. Detailed instructions can be found in the Laravel docs, but here's the basic steps: 1. If you know how to use Docker, see the [docker-for-development](docker-for-development/readme.md) folder and skip many of the following steps.