Move Wordpress site into Docker and publish it on remote site using Nginx.

Borama Apps
3 min readJun 18, 2019

After finishing our family blog from our journey through Sulawesi, we still haven’t decided where we will host it. I decided using docker to contain it and make it easy to switch hosts in the future.

Backup both content and database.

Connect to your host (for example via ssh).

Zip the folder containing wordpress wp-content directory. For example /var/www/html/wp-content:

sudo tar -czf wp-content.tar.gz wp-content

Export database, where wordpress is the name of wordpress database “add-drop-table” will drop tables when importing this backed up database into existing one:

mysqldump wordpress --add-drop-table -uuser -ppassword | gzip  > wordpress.sql.gz

Recreate Wordpress on the local machine.

I created directory sulawesi, copied both wp-content.tar.gz and wordpress.sql.gz into that directory. For example from within this directory you can run scp:

scp user@server:/home/user/wp-content.tar.gz .
scp user@server:/home/user/wordpress.sql.gz .

Create wp-content and extract wp-content.tar.gz into that directory. Also extract wordpress.sql.gz

Create docker-compose.yml:

I’m using .env file for storing mysql credentials (use user / password from your original/old wordpress instance):

I’ve added

DOCKER_COMPOSE_YML_LOCATION: ${PWD}

because later when debuggin my containers I can see which docker-compose.yml was used for particular conainer. For example:

docker inspect wordpress

Will show the docker-compose.yml location under Config / Env / DOCKER_COMPOSE_YML_LOCATION

In db container volumes,

- ./wordpress.sql:/docker-entrypoint-initdb.d/init.sql

loads backed up database content from original site, theoretically we could use ./:docker-entrypoint-initdb.d/ mapping directory containing .sql or even wp-content.tar.gz but I didn’t work for me. Only explicit mapping sql to init.sql worked.

- db_data:/var/lib/mysql

this will ensure database persistance, this is named volume and the location is somewhere inside docker system.

In wordpress conainer:

volumes:- ./wp-content:/var/www/html/wp-content

maps extracted wp-content to conainer’s wp-content.

I like to run:

docker-compose config

To see if my paths and environment variables are set, this is the ouput of current docker-compose.yml:

Let’s run our containers, first not in detached mode:

docker-compose up

Go to localhost:8000, in terminal we should see we are hitting wordpress conainer, but there’s error: This site can’t be reached, localhost refused to connect.

We need to fix base url inside database.

Go to localhost:8080 (port configured for phpmyadmin), login using wordpress/wordpress or what ever put inside .evn for WORDPRESS_DB_USER / WORDPRESS_DB_PASSWORD

Open wp_options and change siteurl and homeurl to http://0.0.0.0:8000

Go to localhost:8000 again, if you still can’t connect, restart docker client.

Move the setup to remote server.

Compress the whole directory containing docker-compose.yml, .env, wordpress.sql and wp-content subdirectory. Copy it to remote server (using scp or ftp), extract it, and move to that directory.

Run docker-compose up -d, set up Nginx by adding server block with locations for phpmyadmin and for wordpress:

Connect to phpmyadmin and update siteurl and home inside wp_options table to point to actual domain (in my case nasulawesi.me).

In order to be able to update wordpress content in host’s directory, we need to run docker as root:

sudo docker-compose up -d

Just as a side note, during the whole process I had to restart docker services both on my destkop and on my remote computers, because they sometimes didn’t catch the containers updates, even when using:

docker-compose down --volumes

That’s it!

Please check my IOS and Android apps!

Follow me on twitter :)

--

--