Moving on to MediaWiki – Part 2 – Docker Install

In the previous post on this topic, I’ve had a look back at which Wiki’s I’ve used over the decades to store my personal tech information and why I decided to move on to MediaWiki as a platform. In the past, getting a new web service up and running required the setup of a web server, a database and some other things and was a bit of a hassle. Fortunately, there is an official containerized version of MediaWiki these days and I found a good description of how to get it up and running with docker-compose in no time. As the description is in German and because I have changed a few things, here’s my version of how to get up and running:

Based on the docker-compose script linked to above, I decided that I didn’t require memcached and a separate network connection, it’s only a personal Wiki after all. This makes the docker-compose.yml file and environment variables in the .env file somewhat simpler for the initial bring-up:

####### .env


# Config File for MediaWiki Application

# Docker Compose Project Name
# max length 11 characters
PROJECT_NAME=mediawiki

# Maria DB Configuration
DB_ROOT_PASS=XXXXXXXXXXXXX
DB_NAME=wiki
DB_USER=wiki
DB_PASS=YYYYYYYYYY

# MediaWiki Port Configuration
WIKI_HTTP_PORT=9755
# Wiki Version
# for latest:
WIKI_VERSION=latest

# Timezone
TZ=Europe/Berlin



####### docker-compose.yml


version: '3.8'

services:
  martin-wiki-db: 
    image: mariadb:latest
    volumes:
      - ./mysql:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      TZ: ${TZ}

  martin-mediawiki:
    image: mediawiki:${WIKI_VERSION}
    depends_on:
      - martin-wiki-db
    restart: always
    environment:
      TZ: ${TZ}
    ports:
      - ${WIKI_HTTP_PORT}:80
    volumes:
      - ./images:/var/www/html/images
      - ./data/sitemap:/var/www/html/sitemap
      # After initial setup, download LocalSettings.php to data/conf directory 
      # and uncomment the following line and use compose to restart
      # the mediawiki service
      #- ./data/conf/LocalSettings.php:/var/www/html/LocalSettings.php:ro
      - ./data/conf/.htaccess:/var/www/html/.htaccess:ro

With these two configuration files, the Wiki initially becomes available on port 9755 and is open to the world for HTTP access. Be a bit careful with this, you might want to limit this to localhost (127.0.0.1:9755) and use an ssh tunnel and proxying to access the proto-installation. More about how to open the Wiki to the world and add TLS certificates for production use will follow below. At this point, the installation bring up continues in the web browser as follows:

  • The Wiki needs to know the IP address of the database server. As the IP address can change, use the container alias defined the yml file above. In my example, this is ‘martin-wiki-db‘.
  • Once all questions have been answered in the web interface, ‘LocalSettings.php‘ is generated, which has to be downloaded.
  • Stop the Wiki
  • Move the php settings file to the ./data/conf directory and map it into the container. See the comment in docker-compose.yml for how that is done.
  • Start the Wiki again.
  • Done!

Reverse Proxy For HTTPS and Letsencrypt

For HTTPS and automatic generation and renewal of the TLS certificate with Letsencrypt, a reverse proxy is your best friend. Fortunately, I already had one in place for my other projects, so I only had to stop the Wiki, remove the local port binding in the docker-compose.yml file, add a few lines for the reverse proxy network configuration and make the domain name known to the Wiki:


###### docker-compose.yml

version: '3.8'

services:
  martin-wiki-db: 
    image: mariadb:latest
    volumes:
      - ./mysql:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      TZ: ${TZ}

  martin-mediawiki:
    image: mediawiki:${WIKI_VERSION}
    depends_on:
      - martin-wiki-db
    restart: always
    environment:
      TZ: ${TZ}
      VIRTUAL_HOST: MY_DOMAIN_NAME
      LETSENCRYPT_HOST: MY_DOMAIN_NAME
      LETSENCRYPT_EMAIL: noreply@test.com
    #ports:
    #  - ${WIKI_HTTP_PORT}:80
    volumes:
      - ./images:/var/www/html/images
      - ./data/sitemap:/var/www/html/sitemap
      # After initial setup, download LocalSettings.php to data/conf directory 
      # and uncomment the following line and use compose to restart
      # the mediawiki service
      - ./data/conf/LocalSettings.php:/var/www/html/LocalSettings.php:ro
      - ./data/conf/.htaccess:/var/www/html/.htaccess:ro

networks:
    default:
      external:
        name: proxy


###### LocalSettings.php

## The protocol and server name to use in fully-qualified URLs
#$wgServer = "http://x.x.x.x:9755";
$wgServer = "https://MY_DOMAIN_NAME";

And that’s it!

P.S. You might want to think about using ‘WIKI_VERSION=latest’ for a moment and perhaps decide to use the long term version instead. Have a look here for details.