Setup MediaWiki and Postgres with Docker

Introduction

MediaWiki may not be the most difficult to setup, but docker can make that process even easier. By having MediaWiki and Postgres setup in docker containers, you can also switch servers without worrying about reinstalling.

Step 0: Setup docker

I suggest following Digital Ocean's How to Install and User Docker guide.

Step 1: Download docker images

For this, I am using the official postgres image, but because wikimedia/mediawiki had not be updated, I decided to use simplyintricate/mediawiki.

docker pull postgres  
docker pull simplyintricate/mediawiki  

Step 2: Create wiki directories

We need to create directories for the database data, images, and mediawiki extensions.

mkdir -p <wiki_root>/{db/data,extensions,images}  

Step 3: Setup Postgres docker container

We're going to use a host volume (directory on the machine running docker) to store the database data outside of the docker postgres container. This way, if we need to, we can restart the postgres container without losing any of our data.

docker run --name mediawiki-db -v <wiki_root>/db/data:/var/lib/postgresql/data -d postgres  

Now you need to setup the postgres user and database for mediawiki.

Execute psql -h localhost -U postgres to connect to postgres and create your wiki account and database with:

CREATE USER <user> PASSWORD '<password>';  
CREATE DATABASE <db>;  
GRANT ALL PRIVILEGES ON DATABASE <db> TO <user>;  

Step 4: Setup MediaWiki docker container

For this, we're going to link a mediawiki container to our newly created postgres container.

Step 4a: Create LocalSettings.php

If you don't already have a LocalSettings.php created from a previous install, create a new one by running the mediawiki container:

docker run --name <your_wiki_name>  --link mediawiki-db:db  -p 8080:80 -d simplyintricate/mediawiki  

Take note: simplyintricate/mediawiki has you link your database to :db not :postgres like wikimedia/mediawiki.

Now log onto your wiki instance <server_ip>:8080 with a browser, and initiate the setup.

Make sure to select 'PostgreSQL' as your Database type.

To find what IP the link thinks your db is on, run:

docker exec <your_wiki_name> printenv | grep 'MYSQL\|DB\|POSTGRES'  

Look for DB_PORT_5432_TCP_ADDR - You'll want to use this for the 'Database host'.

Once you've completed setup, keep track of where the generated LocalSettings.php file is downloaded, you'll need that in a moment.

After finishing setup, stop and remove your container (don't worry, the configuration you setup is stored in LocalSettings.php, and the database configuration is saved in your database volume).

docker rm -f <your_wiki_name>  
Step 4b: Run MediaWiki container

First, scp your generated LocalSettings.php to your server, preferably to your the wiki root directory.

Now is the moment our hard work finally pays off. Recreate your wiki container, this time with all the data volumes mounted:

docker run --name <your_wiki_name>  --link mediawiki-db:postgres  -p 8080:80 \  
-v <wiki_root>/data/rw:/data/rw \
-v <wiki_root>/images:/usr/share/nginx/html/images \
-v <wiki_root>/extensions:/tmp/extensions \
-v <wiki_root>/LocalSettings.php:/usr/share/nginx/html/LocalSettings.php \
-d simplyintricate/mediawiki

If you cannot connect to your wiki, try running docker logs <your_wiki_name> to debug.

Conclusion

Now you should have a working wiki of your own!