Use Docker-Compose To Start A Lightweight Virtual Machine Container of Locally Installed WordPress

I’m not a Docker guy because I don’t use it often. Basic commands such as “docker ps” are probably new to me, meaning I can’t easily remember them. Regardless, there are plenty of online resources for me to tap into regarding Docker, so I’m not too fretted about forgetting what command to use with Docker. Today, I found a fun Docker activity. Yep, you can already see I’m a Docker noob. Regardless, this activity is about creating a docker-compose file, then spinning up Docker to start all services that run a locally installed WordPress. This docker-compose file could be found on the Internet easily! I got a very fast Internet connection too, and so it took Docker about a minute or two, I think, to just pull the latest WordPress and MySQL 8 (or 5.7 if you want to use this version) images down to my local machine – and just a few more seconds to start the whole locally installed WordPress up.

Why is it fun to do this? Well, I’m not a PHP developer! This means I can’t just write any PHP script to take care of whatever itch I have with WordPress. WordPress is based on the PHP programming language. I am though love to code in Python, and so I thought — hmm… what if I want to Djangorize WordPress? This means, I want to export all WordPress blog posts I have on a WordPress website of mine to a Django blog web app that I will code soon – but I have way too many WordPress blog posts! The problem is I’m not also an XML guy, and so WordPress’s export file I downloaded gives me pause. Although XML’s basic rules aren’t hard – because it’s an extensive markup language – meaning tags are extensive (not restrictive like HTML) – meaning you can make up your own tags pretty much. For example, in HTML, you can only use a built-in tag such as <div></div>, but in XML you could just make up a tag like <friend></friend>. Anywho, but the XML file I downloaded from WordPress’s export (exported my WordPress blog posts) is so huge and long – one look at it I got confused. So, my original solution is to use docker to start a local WordPress up, import the export XML file from the WordPress XML file download earlier – and then just export all the blog posts to CSV. See, I can’t export WordPress blog posts directly to CSV earlier because I’m not hosting my own WordPress, but I’m using WordPress.com’s official WordPress hosting service; these guys don’t allow you to export your blog posts to any other format besides XML. Unfortunately, if I want to use local WordPress to import XML and then export to CSV file format, I have to pay for a WordPress plugin – which I don’t want to do! Regardless, it was fun to do docker-compose!

So, what now? I kick myself a bit because now I remember that Python got Pandas that can read XML files. So, I went to all the troubles with docker-compose for naught. On the plus side, I got a local backup of my current running WordPress blog. Hooray! Anyhow, I’ll use Python, Pandas, and BeautifulSoup to organize the XML blog post data to my liking in the future, and in this way, I’ll able to write a Python script that allows me to import WordPress blog posts to Django’s blog web app. For now, I’ll leave you with a fun docker-compose file that allows you to start up a docker container that contains essential running services like MySQL to let you play with a locally installed WordPress.

One more thing, you need to download Docker yourself before you can use this docker-compose file. Once downloaded and install Docker, create a new folder on a Windows machine and name it “wordpress-local”, but you can name this folder with whatever name you like. Copy and paste the docker-compose file below to a file in this “wordpress-local” folder – and save this file as “docker-compose.yml”. Open up Windows PowerShell terminal, change into the “wordpress-local” folder using commands like “cd C:\Users\your-user-name-here\wordpress-local“, make sure the “docker-compose.yml” file is in “wordpress-local” folder, and then you can do “docker-compose up -d” in the terminal. This will tell Docker to start pulling in images for the latest WordPress and whatever version MySQL you had coded in “docker-compose.yml” file, and then Docker will install these images, and start services to allow you to go to 127.0.0.1 to install WordPress locally. The volume Docker creates will be persisted, but you can do “docker-compose down --volume” in the terminal to delete all persisted Docker volumes. If you just do “docker-compose down”, then only Docker containers get deleted but not the volumes. It is fine that Docker containers get deleted because Docker is meant to be used this way – all it takes for you is to do “docker-compose up -d” to recreate and start the container again.

Docker compose file is below:

version: '2.12.2'

services:
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - wordpress_files:/var/www/html
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: abcdefg

   db:
     image: mysql:8
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: root-abcdefg
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: abcdefg
volumes:
    wordpress_files:
    db_data:

The line where the “docker-compose.yml” file said “version: ‘2.12.2’, you can change it to your docker-compose version. In the terminal, do “docker-compose --version” to see your current docker-compose version. The password for WORDPRESS_DB_PASSWORD and MYSQL_PASSWORD needs to be the same – because the former one is for environment settings and the latter one is the actual password that you want to set with MySQL database. You can also change the password for MYSQL_ROOT_PASSWORD to something else if you like. Oh, I almost forgot, when trying to import WordPress blog posts from the download of WordPress’s export XML file, if your file is too big, WordPress may refuse to import the blog posts. This can be fixed easily.

First, you need to install nano or vim for the Docker container. You do not need to stop the running Docker container to do this. In the terminal, do “docker ps” to see the Docker’s CONTAINER ID. In the terminal, do docker exec -it enter-the-container-id-here bash -c "apt-get update && apt-get install -y nano". Once everything is done, you can do “docker exec -it enter-the-container-id-here /bin/sh“. This allows you to mimic how you would ssh into a Linux server, but in our case, we go inside the container. Now, you can do “nano .htaccess“. Go to the very end of the file (.htaccess), and type in two lines:

php_value upload_max_filesize 256M
php_value post_max_size 256M

Save the .htaccess file. You can also create phpinfo.php file by doing “nano phpinfo.php“.

<?php
        phpinfo();
?>

Save the phpinfo.php file. Exit the container’s ssh-like environment by doing Ctrl-d on the keyboard. Now, you can go to 127.0.0.1/phpinfo.php to see if upload_max_filesize and post_max_size are actually 256 Megabytes. You can always increase the Megabytes sizes for these two php_values so WordPress can allow the importation of larger WordPress exported files. That’s it! Go ahead and import your blog posts to a local container and play around with locally installed WordPress using Docker. Have fun!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.