From BITPlan Wiki
Jump to navigation Jump to search


id  pymediawikidocker
state  active
owner  WolfgangFahl
title  pymediawikidocker
version  0.10.7
description  Python controlled mediawiki docker image installation
date  2023-04-22
since  2021-06-08



Official Mediawiki docker images are available for the different Mediawiki versions.

These images need additional infrastructure to create useable Mediawiki environments. The goal of this project is to semi-automatically create such environments and test the functionality. Python was choosen as a development environment for the libraries that are needed and available to perform the tasks:

python on whales was chosen over:

Since it works on the command line interface and not via the SDK and supports docker compose.

How it works

The core concept is a "MediawikiCluster" that is a set of docker applications that run MediaWiki instances with varying versions of MediaWiki and the underlying database. The graphic belows shows the docker containers which are created for different versions of Mediawiki



Docker on Windows, Mac or Linux suppporting "docker compose up" using the Docker composer V2 CLI

Ubuntu 22.04 LTS installation

see on how to setup the docker repositorysudo

sudo apt-get install docker-ce docker-ce-cli docker-compose-plugin
docker --version
Docker version 23.0.2, build 569dd73
docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.


Compatibility test

# test on a Mac OS machine 2022-07-02
docker 2>&1 | grep compose
  compose*    Docker Compose (Docker Inc., v2.4.1)
# test on an Ubuntu machine 2023-04-07
docker 2>&1 | grep compose
 compose*    Docker Compose (Docker Inc., v2.17.2)

Installation of Docker Compose V2 CLI

lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy
docker compose version
Docker Compose version v2.17.2
sudo apt  install docker-compose
docker-compose version
docker-compose version 1.29.2, build unknown
docker-py version: 5.0.3
CPython version: 3.10.6
OpenSSL version: OpenSSL 3.0.2 15 Mar 2022
which docker-compose

pymediawikidocker installation via pip

pip install pymediawikidocker
# alternatively if your pip is not a python3 pip
pip3 install pymediawikidocker


pip install pymediawikidocker -U
# alternatively if your pip is not a python3 pip
pip3 install pymediawikidocker -U

check version

mwcluster -V
mwcluster (v0.10.0,2023-04-08)


mwcluster -h
mwcluster -h    
usage: mwcluster [-h] [-cn CONTAINER_NAME] [-d]
                 [-el [EXTENSIONNAMELIST ...]]
                 [-ej EXTENSIONJSONFILE] [-f] [-fu] [--host HOST]
                 [-dp DOCKER_PATH] [--logo LOGO] [-mv MARIADBVERSION]
                 [--mysqlPassword MYSQLPASSWORD] [-rp] [-p PASSWORD]
                 [-pl PASSWORDLENGTH] [--prefix PREFIX] [--prot PROT]
                 [--script_path SCRIPT_PATH] [--url URL]
                 [-sp SQL_PORT] [-smw SMW_VERSION] [-u USER] [-q]
                 [-bp BASE_PORT] [-vl [VERSIONS ...]] [--about]
                 [--create] [--down] [--check] [--list] [-V]

Copyright 2020-2023 contributors. All rights reserved. Licensed under
the Apache License 2.0
Distributed on an "AS IS" basis without warranties or conditions of
any kind, either express or implied.

  -h, --help            show this help message and exit
  -cn CONTAINER_NAME, --container_name CONTAINER_NAME
                        set container name (only valid and
                        recommended for single version call)
                        (default: mw-139)
  -d, --debug           enable debug mode [default: False]
  -el [EXTENSIONNAMELIST ...], --extensionList [EXTENSIONNAMELIST ...]
                        list of extensions to be installed [default:
                        ['Admin Links', 'Header Tabs',
                        'SyntaxHighlight', 'Variables']]
                        additional extension descriptions default:
                        [default: None]
  -f, --forceRebuild    force rebuilding [default: False]
  -fu, --forceUser      force overwrite of wikiuser (default: False)
  --host HOST           the host to serve / listen from [default:
  -dp DOCKER_PATH, --docker_path DOCKER_PATH
                        the base directory to store docker and jinja
                        template files [default:
  --logo LOGO           set Logo [default: $wgResourceBasePath/resour
                        mariaDB Version to be installed [default:
  --mysqlPassword MYSQLPASSWORD
                        set sqlRootPassword [default: None] - random
                        password if None
  -rp, --randomPassword
                        create random password and create wikiuser
                        while at it (default: False)
  -p PASSWORD, --password PASSWORD
                        set password for initial user [default:
                        set the password length for random
                        passwords[default: 15]
  --prefix PREFIX       the container name prefix to use [default:
  --prot PROT           change to https in case [default: http]
  --script_path SCRIPT_PATH
                        change to any script_path you might need to
                        set [default: ]
  --url URL             will set prot host,script_path, and
                        optionally port based on the url given
                        [default: None]
  -sp SQL_PORT, --sql_base_port SQL_PORT
                        set base mySql port 3306 to be exposed -
                        incrementing by one for each version
                        [default: 9306]
  -smw SMW_VERSION, --smw_version SMW_VERSION
                        set SemanticMediaWiki Version to be installed
                        default is None - no installation of SMW
                        (default: None)
  -u USER, --user USER  set username of initial user with sysop
                        rights [default: Sysop]
  -q, --quiet           not verbose [default: False]
  -bp BASE_PORT, --base_port BASE_PORT
                        set how base html port 80 to be exposed -
                        incrementing by one for each version
                        [default: 9080]
  -vl [VERSIONS ...], --version_list [VERSIONS ...]
                        mediawiki versions to create docker
                        applications for [default: ['1.35.10',
                        '1.38.6', '1.39.3']]
  --about               show about info [default: False]
  --create              create wikis [default: False]
  --down                shutdown wikis [default: False]
  --check               check the wikis [default: False]
  --list                list the wikis [default: False]
  -V, --version         show program's version number and exit



create with default settings

Starts a cluster with versions 1.35.10,1.38.6 and 1.39.3 using MariaDB 10.11. Essentially this is automatic generation of the configuration with a docker compose up for all versions asked for.

mwcluster --create -f
creating docker compose applications for mediawiki versions ['1.35.10', '1.38.6', '1.39.3']
starting mw-135 1.35.10 docker application ...
Initializing MediaWiki SQL tables
Executing docker command /root/
Executing docker command /root/
Executing docker command /root/
Executing docker command /root/
Executing docker command /root/

Mediawiki applications will be available at

http://localhost:9080 http://localhost:9080/index.php/Special:Version

http://localhost:9081 http://localhost:9081/index.php/Special:Version

http://localhost:9082 http://localhost:9082/index.php/Special:Version


mwcluster --check -f 
checking docker access for mediawiki versions ['1.35.10', '1.38.6', '1.39.3']
1:checking 1.35.10 ...
mediawiki webserver container mw-135-mw:✅
mediawiki database container mw-135-db:✅
port binding 9080= expected  port 9080?:✅
Checking ...
Special Version accessible ...:✅
Mediawiki Version 1.35.10= expected  1.35.10?:✅
Maria DB Version 10.11 fitting expected 10.11?:✅
2:checking 1.38.6 ...
mediawiki webserver container mw-138-mw:✅
mediawiki database container mw-138-db:✅
port binding 9081= expected  port 9081?:✅
Checking ...
Special Version accessible ...:✅
Mediawiki Version 1.38.6= expected  1.38.6?:✅
Maria DB Version 10.11 fitting expected 10.11?:✅
3:checking 1.39.3 ...
mediawiki webserver container mw-139-mw:✅
mediawiki database container mw-139-db:✅
port binding 9082= expected  port 9082?:✅
Checking ...
Special Version accessible ...:✅
Mediawiki Version 1.39.3= expected  1.39.3?:✅
Maria DB Version 10.11 fitting expected 10.11?:✅


mwcluster --down -f
running docker compose down for mediawiki versions ['1.35.10', '1.38.6', '1.39.3']
running docker compose down for mw-135 1.35.10 docker application ...
[+] Running 6/6
 ⠿ Container mw-135-mw       Removed                              1.3s
 ⠿ Container mw-135-db       Removed                              0.7s
 ⠿ Volume mw-135_wiki-etc    Removed                              0.1s
 ⠿ Volume mw-135_mysql-data  Remove...                            0.2s
 ⠿ Network mw-135_default    Removed                              0.1s
 ⠿ Volume mw-135_wiki-www    Removed                              1.6s
running docker compose down for mw-138 1.38.6 docker application ...
[+] Running 6/6
 ⠿ Container mw-138-mw       Removed                              1.3s
 ⠿ Container mw-138-db       Removed                              0.5s
 ⠿ Volume mw-138_mysql-data  Remove...                            1.2s
 ⠿ Volume mw-138_wiki-www    Removed                              1.1s
 ⠿ Volume mw-138_wiki-etc    Removed                              1.2s
 ⠿ Network mw-138_default    Removed                              0.1s
running docker compose down for mw-139 1.39.3 docker application ...
[+] Running 6/6
 ⠿ Container mw-139-mw       Removed                              1.3s
 ⠿ Container mw-139-db       Removed                              0.5s
 ⠿ Volume mw-139_mysql-data  Remove...                            0.0s
 ⠿ Volume mw-139_wiki-etc    Removed                              0.0s
 ⠿ Network mw-139_default    Removed                              0.1s
 ⠿ Volume mw-139_wiki-www    Removed                              0.7s

Single 1.35.10 LTS

# python mwdocker/  can be used instead of mwcluster if you test from your locally cloned environment
mwcluster \
  --create \
  --version_list 1.35.10\
  --base_port 8480\
  --sql_base_port 10306\
  --mariaDBVersion 10.11\
  --container_name mw135lts\
creating docker compose applications for mediawiki versions ['1.35.10']
starting mw135lts 1.35.10 docker application ...

Mediawiki application will be available at

http://localhost:8480 http://localhost:8480/index.php/Special:Version PyMediaWikiDocker-ExampleMW135WithLogo.png

Single 1.35 with Semantic Mediawiki

There might be more current versions of SMW available see and adapt the example below if you see the need to do so.

Version 4.1.1

mwcluster \
  --create \
  --container_name smw4\
  --version_list 1.35.10\
  --base_port 8481\
  --sql_base_port 10307\
  --mariaDBVersion 10.11\
  --smw_version 4.1.1

debug docker compose results - ports and other configuration parts

cd ~/.pymediawikidocker/smw4
docker-compose up
# in another terminal session:
docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED              STATUS         PORTS                     NAMES
87a795d444eb   smw4_mw                 "docker-php-entrypoi…"   About a minute ago   Up 7 seconds>80/tcp      smw4-mw
7ca3a0434d31   mariadb:10.11           "docker-entrypoint.s…"   About a minute ago   Up 7 seconds>3306/tcp   smw4-db

open bash in docker container

docker exec -it smw4-mw /bin/bash

check script

cat /root/ 
# install the required extensions
# WF 2021-06-23
cd /var/www/html/extensions

# Admin Links
git clone --single-branch --branch REL1_35 AdminLinks

# Header Tabs
git clone --single-branch --branch REL1_35 HeaderTabs

# SyntaxHighlight
# no installation script command specified

# Variables
git clone --single-branch --branch REL1_35 Variables

check Semantic MediaWiki settings in LocalSettings.php

grep -i semantic LocalSettings.php 
# enable Support for Semantic MediaWiki
# see
# Version of SemanticMediaWiki at install time: 4.1.1
wfLoadExtension( 'SemanticMediaWiki' );

SMW Version 3.2.3


mwcluster \
  --create \
  --container_name smw3\
  --version_list 1.35.10\
  --base_port 8482\
  --sql_base_port 10308\
  --mariaDBVersion 10.11\
  --smw_version 3.2.3


mwcluster --check --container_name smw3 --version_list 1.35.10


mwcluster --down --container_name smw3 --version_list 1.35.10 -f
running docker compose down for mediawiki versions ['1.35.10']
running docker compose down for smw3 1.35.10 docker application ...
[+] Running 3/3
 ⠿ Volume smw3_wiki-etc    Removed                                0.0s
 ⠿ Volume smw3_mysql-data  Removed                                0.1s
 ⠿ Volume smw3_wiki-www    Removed                                1.2s


see Pymediawikidocker/Extensions


Docker files

You'll find the files created by pymediawikidocker in ".pymediawikidocker" in your homedirectory.

ls -l
total 176
-rw-r--r--  1 wf  staff   1057 22 Jun 07:03 Dockerfile
-rw-r--r--  1 wf  staff   4580 22 Jun 07:03 LocalSettings.php
-rw-r--r--  1 wf  staff   1161 22 Jun 07:03 docker-compose.yml
-rw-r--r--  1 wf  staff    317 22 Jun 07:03
-rw-r--r--  1 wf  staff    125 22 Jun 07:03 phpinfo.php
-rw-r--r--  1 wf  staff  64515 22 Jun 07:03 wiki.sql

Check installed docker images

docker images | grep mw
mw1_38_4_mw    latest    c647ad44a344   About a minute ago   996MB
mw1_37_6_mw    latest    af9de508138c   About a minute ago   980MB
mw1_35_8_mw    latest    f41c84ab16c3   2 minutes ago        988MB
mw1_31_16_mw   latest    9a8a26949b58   2 minutes ago        897MB
mw1_27_7_mw    latest    1fa159362ec9   2 minutes ago        898MB

Bash into docker containers

# Mediawiki container
docker exec -it mw1_38_4-mw /bin/bash
docker exec -it mw1_37_6-mw /bin/bash
docker exec -it mw1_35_8-mw /bin/bash
docker exec -it mw1_31_16-mw /bin/bash
docker exec -it mw1_27_7-mw /bin/bash

# MariaDB container
docker exec -it mw1_38_4-db /bin/bash
docker exec -it mw1_37_6-db /bin/bash
docker exec -it mw1_35_8-db /bin/bash
docker exec -it mw1_31_16-db /bin/bash
docker exec -it mw1_27_7-db /bin/bash

Monkey patching

# Enable raw html in for the wiki
docker exec  mw1_35_8-mw sh -c "echo '\$wgRawHtml = true;\n' >> /var/www/html/LocalSettings.php"

No space left on device issue

e.g. when starting mariadb container see

Wiki Backup

The passwords are available from the docker files e.g.

grep PASS docker-compose.yml 
      MYSQL_PASSWORD: "TnbKf2zGRmZPjUvz7B"

In a docker bash a dump can be created

mysqldump wiki -u wikiuser --password=TnbKf2zGRmZPjUvz7B > /tmp/wiki.sql

and then copied to your local machine

docker cp mw1_36_0_db_1:/tmp/wiki.sql /tmp

Docker cleanup procedure

# WF 2023-04-08
# ⚠️ ⚠️ ⚠️ 
#  kids  don't do this at home ...
# ⚠️ These commands will stop and remove all running docker containers (not only the ones created pymediawikidocker/the mwcluster command) 
# kill/stop all running containers
docker kill $(docker ps -q)
#docker stop $(docker ps -aq)
# remove all docker containers
docker rm $(docker ps -aq)
# remove all images
# ⚠️ This command will remove all your images not only the ones created by pymediawikidocker (docker will hopefully refuse to do this without the -f option) 
docker rmi $(docker images -q)
# ⚠️ these commands will remove all your volumes - you might loose valuable data if you don't check what your volumes contain before starting this command 
# see
docker volume rm $(docker volume ls -qf dangling=true | xargs)
#⚠️ WARNING! This will remove:
#  - all stopped containers
#  - all networks not used by at least one container
#  - all dangling images
#  - all dangling build cache
docker system prune --force