Skip to content
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

Improve docker compose for development #5

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gitignore
.git/
Dockerfile
docker-compose.yml
docker/secrets
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea/*
.DS_Store
config.php

docker/secrets/
docker/ssl/**/*.pem
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM php:8.3-apache

COPY ./docker/ssl/configure_ssl.sh /usr/local/bin/configure_ssl.sh
RUN chmod +x /usr/local/bin/configure_ssl.sh

COPY ./docker/ssl /docker/ssl

ARG SSL_ENABLED

RUN /usr/local/bin/configure_ssl.sh

EXPOSE 80
EXPOSE 443

RUN apt-get update && apt-get install -y \
libpng-dev \
cron \
&& docker-php-ext-install mysqli gd

# Uncomment the next line when deploying to production
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# This is to prevent removing config.php from gitignore for now. Might be removed when non-docker install gets improved.
COPY ./config.docker.php ./config.php
COPY . /var/www/html

COPY ./docker/run_cron_job.sh /usr/local/bin/run_cron_job.sh
COPY ./docker/cron_jobs /etc/cron.d/cron_jobs

# Ensure the script and cron job files are executable
RUN chmod +x /usr/local/bin/run_cron_job.sh
RUN chmod 0644 /etc/cron.d/cron_jobs

RUN crontab /etc/cron.d/cron_jobs

RUN chmod +x ./docker/start_cron.sh
CMD ./docker/start_cron.sh
71 changes: 61 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ New to MCCodes? Or simply wanting reference on a simple task? That's what this b

Contents
--------
* [1\. Installation](#1p0)
* [1.1 Basic Installation](#1p1)
* [1.2 Cronjobs](#1p2)
* [1.3 PayPal Configuration](#1p3)
* [2\. Basic Usage & Tips](#2p0)
* [2.1 Setting up Will Potions](#2p1)
* [2.2 Tips for running a game](#2p2)
- [MCCodes 2.0](#mccodes-20)
- [Introduction](#introduction)
- [Contents](#contents)
- [1. Installation](#1-installation)
- [ 1.1 Basic Installation](#-11-basic-installation)
- [ 1.2 Docker Installation](#-12-docker-installation)
- [Enabling SSL (Optional)](#enabling-ssl-optional)
- [ 1.3 Cronjobs](#-13-cronjobs)
- [ 1.4 PayPal Configuration.](#-14-paypal-configuration)
- [2. Basic Usage \& Tips](#2-basic-usage--tips)
- [ 2.1 Setting up Will Potions](#-21-setting-up-will-potions)
- [ 2.2 Tips for running a game](#-22-tips-for-running-a-game)

<a id="1p0"></a>1\. Installation
----------------
Expand All @@ -31,8 +36,54 @@ Got all the above? Good. Upload all the files and directories in the upload fold
webserver. Normally, you will be uploading to either the public\_html folder, or the root level folder. A good way to check this is to upload one file then try to access it through your domain and see if it works. If it doesn't, you've got the path wrong. Once done, run install.php. First, your server diagnostics will be checked. If one of these tests fails, you will need to correct it before moving on. Next, you need to fill in your database info, and a few basic
settings. Make sure the PayPal email you specify is the main email of the PayPal account, and that the account is at least Premier. Otherwise, the basic settings are up to you. After you submit this form, the installer should hopefully run, and insert all the tables and entries into SQL you need. If not, go back and check your config. Now the basic installation is complete, and you're ready to move on to setting up the Cron Jobs.

* * *
### <a id="1p2"></a> 1.2 Cronjobs
* * *
### <a id="ip2"></a> 1.2 Docker Installation

This mode of installation is currently intended for development but might be suitable for production with the appropriate adjustments.

First, ensure you have [Docker](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/) installed.

Create the files `db_password.txt` and `app_key.txt` in the `docker/secrets/` folder within your project. The `db_password.txt` file stores the root user password for the database, and the `app_key.txt` file stores the code (usually randomly generated in `installer.php`) used to secure certain scripts, such as cron scripts.

> [!NOTE]
> Before running for the first time, you can modify the `db/initial_data.sql` file, where the admin user and some settings are created. After the initial run, this file is ignored, and changes must be made directly in the database.

Once everything is set up, run the following command to deploy the containers and synchronize all changes to the source files after saving:

```shell
docker compose watch
```

Alternatively, the containers can be run without syncing by using:

```shell
docker compose up -d
```

#### Enabling SSL (Optional)

A self signed certificate can be created with `mkcert`. Follow the [installation instructions](https://github.com/FiloSottile/mkcert?tab=readme-ov-file#installation) and run:

```shell
mkcert -cert-file docker/ssl/cert.pem -key-file docker/ssl/cert-key.pem localhost 127.0.0.1 ::1
```

With the certificate files created in the `docker/ssl/` folder, add the SSL_ENABLED=true argument to your docker-compose:

```YAML
services:
web:
build:
context: .
args:
SSL_ENABLED: true
```

* * *
### <a id="1p3"></a> 1.3 Cronjobs

> [!NOTE]
> If you are using Docker for the build process, you can skip this step. The cron jobs are already included in the Docker image.

The Cronjobs are the thing most people are confused on, or they simply do not work for them. However, if you follow the below instructions correctly, your crons should work fine, unless your server is configured differently to a normal cPanel server. If you are not using cPanel, you will need Shell access to your server (through SSH or SFTP).

Expand Down Expand Up @@ -67,7 +118,7 @@ The Cronjobs are the thing most people are confused on, or they simply do not wo
* If everything goes to plan, you should now have working crons.

* * *
### <a id="1p3"></a> 1.3 PayPal Configuration.
### <a id="1p4"></a> 1.4 PayPal Configuration.

MCC2 uses several PayPal devices to ensure that donations are credited securely and accurately. However, some of these devices must be enabled in your PayPal account before MCC2 can use them. If these steps are not followed, your donation system will not work properly. Follow these steps to get it up and working:

Expand Down
22 changes: 22 additions & 0 deletions config.docker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
function read_secret($name) {
if ($val = getenv($name)) {
return $val;
}

if ($envFile = getenv($name . '_FILE')) {
return trim(file_get_contents($envFile));
}

trigger_error("Missing '$name' configuration.", E_USER_ERROR);
}

$_CONFIG = array(
'hostname' => getenv('DB_HOST'),
'username' => getenv('DB_USER'),
'password' => read_secret('DB_PASS'),
'database' => getenv('DB_NAME'),
'persistent' => 0,
'driver' => 'mysqli',
'code' => read_secret('APP_KEY'),
);
18 changes: 18 additions & 0 deletions db/initial_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
INSERT INTO `users` (
`username`, `login_name`, `userpass`, `level`, `money`, `crystals`, `donatordays`, `user_level`, `energy`,
`maxenergy`, `will`, `maxwill`, `brave`, `maxbrave`, `hp`, `maxhp`, `location`, `gender`, `signedup`, `email`,
`bankmoney`, `lastip`, `lastip_signup`, `pass_salt`, `display_pic`, `staffnotes`, `voted`, `user_notepad`
) VALUES (
'admin', 'admin', '862b159a2262556d8c077fbf4353e32a', 1, 100, 0, 0, 2, 12, 12, 100, 100, 5, 5, 100, 100, 1, 'Male',
'1719158844', '[email protected]', -1, '127.0.0.1', '127.0.0.1', 'a7487647', '', '', '', ''
);

INSERT INTO `userstats` VALUES (LAST_INSERT_ID(), 10, 10, 10, 10, 10);

INSERT INTO `settings`
(conf_name, conf_value, data_type)
VALUES
('game_name', 'MCCodes v2', 'string'),
('game_owner', 'Owner Name', 'string'),
('paypal', NULL, 'string'),
('game_description', 'Game made on the MCCodes engine', 'string');
Loading