Kristi Jorgji
Menu

I moved WordPress inside a docker container

Kristi Jorgji3 min readComments

Docker compose
Contents
  1. Update
  2. How to migrate to docker
  3. What went wrong after the migration ?

Update

Hi all,

I migrated this website from apache barebone installation to encapsulated wordpress inside a docker container with php fpm and nginx.

This is actually a test post I am making to ensure that all is working fine, but also to provide you update about the site and what I changed.

I haven’t been active for several reasons, first of all don’t want to invest time here as at a later time want to move away from WordPress to a custom blog system.

I have a lot of topics and knowledge that can share but will do that at a later point.

For the moment, the only update is that I moved this website from one old apache webserver + wordpress to a new architecture.

My “pet server” where I host this website and some other fun projects without any commercial meaning was old school one. One apache webserver and several websites.

I modernised it last weekend and I started to use docker for splitting my pet projects into several containers, and one container having nginx as reverse proxy.

How to migrate to docker

If you want to do the same, I have some public code I created for you to reuse.

First my docker image is available at

https://github.com/kristijorgji/docker-images/tree/main/wordpress-php-7.1

also with a readme how to use.

After having the wordpress site in a separate docker container, I just configured my nginx configuration in proxy container to redirect traffic to the wordpress container one.

It worked partially fine at the start, beacuse wordpress was trying to load the assets via http instead of https.

The fix for that is to configure the php site to enforce ssl and the domain where the site is served.

Just adjust `wp-config.php` and add these lines on the top and your issue will be fixed

// <K> to allow it to work behind reverse proxy
define('.COOKIE_DOMAIN.', '{{ kj_public_domain }}');
define('.SITECOOKIEPATH.', '.');
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];
}
define( 'WP_HOME', 'https://{{ kj_public_domain }}' );
define( 'WP_SITEURL', 'https://{{ kj_public_domain }}' );
$_SERVER['HTTP_HOST'] = '{{ kj_public_domain }}';
$_SERVER['REMOTE_ADDR'] = 'https://{{ kj_public_domain }}';
$_SERVER[ 'SERVER_ADDR' ] = '{{ kj_public_domain }}';
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS']='on';
}
// </K>

 

That was all. Now I can have several domains on a single server thanks to reverse proxy container (nginx) and having all separate sites in separated docker containers.

Not only that, but also can have sites in different programming languages and environments, as docker containers are partially isolated.

This is my first post after moving site to docker.

What went wrong after the migration ?

The first thing that I noticed was that I could not upload images and some cache errors.

That was because php-fpm runs under www-data user and the mounted volume is mounted by other user and wordpress cannot write files or directories.

One hacky fix is to login into your container and change permissions

docker-compose exec kj_wordpress /bin/bash

afterward

chown -R www-data .

This will work great but the problem is have to do everytime your container changes and it is deployed again.

A proper automated solution would be to mount the volume and specify user and group id in docker-compose.

My question is here

https://stackoverflow.com/questions/72914147/how-to-mount-a-volume-with-docker-compose-with-owner-www-data-user-of-the-contai

That was all for this time.

See you on the next ones, probably after I update to custom blog with pre-rendering. Cheers

CategoriesNews
Tags

Written by Kristi Jorgji

Software engineer writing about programming, architecture, and the books that shape how I build products.

Share

Related posts

Comments

Comments are powered by Disqus, a third-party service.Privacy policy