logo
Jiff Slater
🤔 About
✍️ Contact
📚Knowledge
📄Posts
📁Archive
Updated: 04 October 2021

Converting an existing Mediawiki installation to a container

2021-10-04

I use Mediawiki to keep take notes and references things I've learned across the years. I've found it to be a great alternative to OneNote. Traditionally, I configure using the built in package that ships with Debian. Recently, I wanted to make my installation portable so I can use it anywhere and wanted to integrate it into my existing systemd systems. Here's how I converted an installation configured through apt to a systemd-nspawn container.

In a typcial installation you'll installing Mediawiki using the guides available here, and here. We'll replicate that flow inside the container using a mixture of debootstrap and an init script.

Preparation

First shutdown Apache. :

# systemctl stop apache2

Save the configuration files. :

# TEMP=$(mktemp -d)
# tar cf $TEMP/apache2.tar /etc/apache2
# tar cf $TEMP/php.tar /etc/php

Export the databases. Here you can see the database is called mediawikidb. We'll export both the users from the mysql database and the mediawikidb. :

# mysql -u admin -p
MariaDB [(none)]> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mediawikidb        |
  | mysql              |
  | performance_schema |
  +--------------------+
  4 rows in set (0.001 sec)
MariaDB [(none)]> quit;
# mysqldump -u admin -p --create-options --databases mediawikidb > $TEMP/mariadb-mediawikidb-$(date -I).sql
# mysqldump -u admin -p --create-options --system=users > $TEMP/mariadb-systemdb-$(date -I).sql

Export the HTML/PHP files. :

# tar cf $TEMP/transfer.tar /var/www/html/wiki /etc/certs /etc/apache2 /etc/php

Instantiating the new LAMP image

Next, we'll set up the new LAMP container image using btrfs and debootstrap. If you're installing debootstrap on a non-Debian based machine then you'll need to install the Debian keyrings as well. :

# btrfs subvolume create /var/lib/machines/xct-mediawiki
# mv $TEMP/*.tar $TEMP/*.sql /var/lib/machines/xct-mediawiki/
# apt install debootstrap wget debian-archive-keyring systemd-container ncurses-term
# INCLUDELIST="systemd,dbus,php,php-apcu,php-intl,php-mbstring,php-xml,php-mysql,mariadb-server,apache2"
# debootstrap --arch amd64 --include=$INCLUDELIST stable /var/lib/machines/xct-mediawiki https://deb.debian.org/debian
# alias mw="systemd-nspawn -u root -M xct-mediawiki"
# mw systemctl disable apache2
# mw systemctl disable mariadb
# mw systemctl enable systemd-networkd systemd-resolved
# machinectl start xct-mediawiki

Creating the nspawn file

Systemd uses *.nspawn files to specify the parameters of virtual machines and containers managed through systemd-nspawn and machinectl. We'll write a file that handles the port redirections. Note that this file must live in /etc/ other the privileged operation of setting port forwards won't work. :

/etc/systemd/nspawn/xct-mediawiki.nspawn
[Network] 
Port=tcp:80
Port=tcp:443
VirtualEthernet=yes

Importing the old installation

Finally, we'll move the SQL exports and tarballs into the containers root directory. Apache and MariaDB imports shall ensue... :

# machinectl shell xct-mediawiki
# PS1="(x)\w\\$ " # inside the container
(x)# rm -r /etc/php /etc/apache2
(x)# tar xf /transfer.tar
(x)# mkdir -p /var/www/html
(x)# tar xf mariadb-mediawikidb-2021-10-03
(x)# systemctl start mariadb
(x)# mysql -u root 
  MariaDB [(none)]> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  +--------------------+
  3 rows in set (0.001 sec)
  MariaDB [(none)]> create database mediawikidb;
  Query OK, 1 row affected (0.000 sec)
  MariaDB [(none)]> quit;
(x)# mysql -u root < mariadb-mediawikidb-2021-10-03
(x)# mysql -u root < mariadb-systemdb-2021-10-03 # remove the line that references the root user if needed

Starting the container

Now let's check that everything is working as expected. If it is we can start Mariadb. :

# machinectl status xct-mediawiki
# systemctl -M xct-mediawiki start mariadb

If port forwarding isn't working as expected, take a look at a few of the logs. :

# systemctl log-level debug
# systemctl status systemd-nspawn@xct-mediawiki
# journalctl -xeu !$
# iptables -L -t nat
# iptables-save