Skip to content

Dockerize services #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mail/node_modules
app/*/node_modules
web/tmp
web/vendor
.env
89 changes: 89 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
ARG NODE_VERSION=22.2.0
ARG PHP_VERSION=8.3

### Web Service
FROM php:${PHP_VERSION}-alpine AS web-deps
ARG COMPOSER_VERSION="1.8.5"

WORKDIR /app

RUN apk update \
&& apk add git unzip \
&& curl https://getcomposer.org/download/$COMPOSER_VERSION/composer.phar --output /usr/bin/composer \
&& chmod u+x /usr/bin/composer

COPY web/composer.json composer.json
COPY web/composer.lock composer.lock

RUN composer install --no-scripts --no-autoloader

COPY web /app

RUN composer dump-autoload --optimize

FROM php:${PHP_VERSION}-fpm-alpine as web
WORKDIR /var/www
RUN docker-php-ext-install mysqli \
&& docker-php-ext-enable mysqli
COPY --from=web-deps /app /var/www/reon/web
RUN mkdir -p /var/www/reon/web/tmp \
&& chown www-data:www-data /var/www/reon/web/tmp


### Mail Service
FROM node:${NODE_VERSION}-alpine AS mail-deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat jq
WORKDIR /app
COPY mail/package.json mail/package-lock.json* ./
RUN npm ci

FROM node:${NODE_VERSION}-alpine as mail
WORKDIR /app
COPY --from=mail-deps /app/node_modules ./node_modules
COPY mail /app
EXPOSE 25
EXPOSE 110

ENTRYPOINT ["/app/entrypoint.sh"]


### Cron jobs

FROM node:${NODE_VERSION}-alpine as battle-deps
WORKDIR /app
COPY app/pokemon-battle/package.json app/pokemon-battle/package-lock.json* ./
RUN npm ci

FROM node:${NODE_VERSION}-alpine as exchange-deps
WORKDIR /app
COPY app/pokemon-exchange/package.json app/pokemon-exchange/package-lock.json* ./
RUN npm ci

# Based on https://github.com/AnalogJ/docker-cron
FROM node:${NODE_VERSION}-alpine AS cron
COPY app/docker_entry.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

WORKDIR /app

COPY app/pokemon-battle pokemon-battle
COPY --from=battle-deps /app/node_modules ./pokemon-battle/node_modules

COPY app/pokemon-exchange pokemon-exchange
COPY --from=exchange-deps /app/node_modules ./pokemon-exchange/node_modules

COPY app/docker.crontab /etc/crontabs/root

# source: `docker run --rm -it alpine crond -h`
# -f | Foreground
# -l N | Set log level. Most verbose 0, default 8
CMD ["crond", "-f", "-l", "2"]

### DNS server

FROM alpine:3.20 AS dns
RUN apk --no-cache add dnsmasq
COPY docker-dns-entry.sh /entrypoint.sh
EXPOSE 53/udp
ENTRYPOINT ["/entrypoint.sh"]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ This repository holds various folders for the service, and each has its own READ
3. Run tables.sql in MySQL
4. Continue these steps later once the production server is setup.

# Docker Setup

A `docker-compose.yml` is provided which will run all the required services. To get started, be sure to have docker available on your system.

1. Copy `config.example.json` to `config.json`
2. Copy `example.env` to `.env` (note the `.` at the start of the file name!)
3. Update the values in both files as needed, ensuring that the mysql options match
- The included compose file exposes the MySQL port, so make sure you choose secure passwords
- Do NOT change the `mysql_host` option in `config.json` unless you plan to use an external MySQL database
- MYSQL_ROOT_PASSWORD is only needed for the `db` container and is not used by the other services
4. Run `docker compose up -d` to start the services
5. Run `docker compose exec -w /var/www/reon/web/scripts web php add_user.php` to create an account without having to configure email delivery

Your REON server should now be accessible at http://localhost/.

# License

This code is licenced under MIT.
3 changes: 3 additions & 0 deletions app/docker.crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

0 * * * * node /app/pokemon-battle/index.js -c /app/config.json
0 * * * * node /app/pokemon-exchange/index.js -c /app/config.json
7 changes: 7 additions & 0 deletions app/docker_entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

env >> /etc/environment

# execute CMD
echo "$@"
exec "$@"
13 changes: 12 additions & 1 deletion app/pokemon-battle/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const fs = require("fs");
const path = require('path');
const mysql = require("mysql2/promise");
const { Command } = require('commander');
const program = new Command();

const defaultPath = path.resolve(__dirname, "..", "..", "config.json");

program
.option('-c, --config <path>', 'Config file path.', defaultPath)
.parse(process.argv);

const options = program.opts();
const config = JSON.parse(fs.readFileSync(options.config));

const config = JSON.parse(fs.readFileSync("../../config.json"));
const mysqlConfig = {
host: config["mysql_host"],
user: config["mysql_user"],
Expand Down
85 changes: 6 additions & 79 deletions app/pokemon-battle/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/pokemon-battle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"commander": "^12.1.0",
"mysql2": "^3.5.2"
}
}
12 changes: 11 additions & 1 deletion app/pokemon-exchange/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const fs = require("fs");
const path = require('path');
const mysql = require("mysql2/promise");
const { Command } = require('commander');
const program = new Command();

const defaultPath = path.resolve(__dirname, "..", "..", "config.json");

program
.option('-c, --config <path>', 'Config file path.', defaultPath)
.parse(process.argv);

const options = program.opts();
const config = JSON.parse(fs.readFileSync(options.config));

const config = JSON.parse(fs.readFileSync(path.resolve(__dirname, "..", "..", "config.json")));
const mysqlConfig = {
host: config["mysql_host"],
user: config["mysql_user"],
Expand Down
9 changes: 9 additions & 0 deletions app/pokemon-exchange/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/pokemon-exchange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"commander": "^12.1.0",
"mysql2": "^3.5.2"
}
}
6 changes: 3 additions & 3 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"hostname": "example.net",
"email_domain": "example.net",
"email_domain_dion": "xxxx.dion.ne.jp",
"mysql_host": "localhost",
"mysql_user": "USER",
"mysql_password": "PASS",
"mysql_host": "db",
"mysql_user": "reon",
"mysql_password": "password",
"mysql_database": "db",
"amoj_regist": "h200"
}
Loading