Categories: Docker, Linux

WordPress + Docker + Cloudfare

TLDR; Sign-up and host your blog with WordPress

I just took the task of renewing my blog, WordPress is awesome, I found they maitain their own docker image: https://hub.docker.com/_/wordpress

I already had a db server running, and setting their docker container with my existing db was super easy. To get things running I just had to do this:

docker run -d \
  --name=wordpress\
  -p 80:80 \
  -v /some_directory/wp-content/:/var/www/html/wp-content\
  -e WORDPRESS_DB_HOST=mysqlhost.example.com \
  -e WORDPRESS_DB_USER=my_wp_db_user \
  -e WORDPRESS_DB_PASSWORD=my_wp_db_pass \
  -e WORDPRESS_DB_NAME=my_wp_name \
  wordpress

I am also using Cloudflare which is an awesome service, they act as a proxy between the world and my humble box running the wordpress docker container. The WordPress container comes with mod_remote_ip preconfigured

Cloudflare provided the following instructions for setting up mod_remoteip the wordpress container already has remoteip.conf pre-configured with these nice config:

RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 10.0.0.0/8
RemoteIPTrustedProxy 172.16.0.0/12
RemoteIPTrustedProxy 192.168.0.0/16
RemoteIPTrustedProxy 169.254.0.0/16
RemoteIPTrustedProxy 127.0.0.0/8

So I only had to append this section to the existing configuration file: /etc/apache2/conf-available/remoteip.conf

RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/12
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32

I am not yet running in swarm mode, so I am not using the config option.
I am just configuring it before starting the container. i.e:

  1. Create the WP container
docker create \
  --name=wordpress\
  -p 80:80 \
  -v /some_directory/wp-content/:/var/www/html/wp-content\
  -e WORDPRESS_DB_HOST=mysqlhost.example.com \
  -e WORDPRESS_DB_USER=my_wp_db_user \
  -e WORDPRESS_DB_PASSWORD=my_wp_db_pass \
  -e WORDPRESS_DB_NAME=my_wp_name \
  wordpress

2. Copy my modified config file with the original content plus the Cloudflare IPs and header

docker cp /some_directory/remoteip.conf wordpress:/etc/apache2/conf-available/remoteip.conf

3. Start the container

docker start wordpress