{"id":284,"date":"2021-03-22T21:54:33","date_gmt":"2021-03-22T21:54:33","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=284"},"modified":"2022-04-11T20:24:23","modified_gmt":"2022-04-11T20:24:23","slug":"100-days-of-learning-day-13-14-running-wordpress-locally-using-docker-and-using-highlight-js","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/100-days-challenge\/100-days-of-learning-day-13-14-running-wordpress-locally-using-docker-and-using-highlight-js\/","title":{"rendered":"100 Days of Learning: Day 13 & 14 \u2013 Running WordPress locally using Docker and using highlight.js"},"content":{"rendered":"\n
Here is my Log book<\/a><\/p>\n We interrupt this broadcast (Lego database project) to first take a detour in adding code syntax highlighting to the blog.<\/p>\n One of the biggest reasons I started a blog is to document how I setup and develop things so that the "6 months from now" future me can have a recipe of how stuff works.<\/p>\n Fate, it seems, is not without a sense of irony – Morpheus<\/p>\n<\/blockquote>\n My blog runs on WordPress and I developed my own custom theme back in 2019. However I now find myself in a position where I have no clue how any of this stuff works and I really wish I did write that post about how it works back then.<\/p>\n I will spare you the details of how all this works and maybe one day I will write a post about it. The short summary is this:<\/p>\n Learning action point:<\/strong> I should write a backup script that backs up my blog to S3 and have a script that can restore from S3 to the local Docker blog.<\/p>\n At least I left myself some good instructions on how to setup and run most of this but it could have been better. I guess that is the problem with documentation in general. You really have to dog food it to see if you missed any steps etc. or even better is to get someone else to follow your recipe and see if it just clicks.<\/p>\n I will share the EDIT: I modified my original compose file to be more in line with this tutorial<\/a>.<\/p>\n NOTE:<\/strong> I only run this locally. I am sure there must be better ways to supply secrets than to have it in the docker compose file.<\/p>\n Spin up the containers<\/p>\n A new directory named "wordpress" will be created locally and you will start seeing wordpress files being created there.<\/p>\n To monitor the container logs<\/p>\n Go grab a coffee and wait for the wordpress installation files to be created. Then To stop the containers<\/p>\n Where I have gone wrong in my use of Docker is that I need to learn to better backup and restore "things". In my example above, I am persisting the \/var\/www\/html (where wordpress is installed) out to my local machine … but where I have failed (and only seeing it now) is that I never backed up the MariaDB database that goes with the WordPress setup!<\/p>\n EDIT: I came across Docker Volumes<\/a> as a way to persist container data. The docker compose above ^^ reflects this change.<\/p>\n Learning action point:<\/strong> Backup and restore Docker volumes.<\/p>\n For more information on the Official WordPress Docker image<\/a><\/p>\n I can now access the local running WordPress instance.<\/p>\n <\/p>\n <\/p>\n I can also access the MariaDB database on port 8081 from MySQLWorkbench.<\/p>\n <\/p>\n I like to use Markdown for all of my own documentation as well as writing this blog. At the moment I write the blog posts in Notion and then export the Markdown before adding it to WordPress.<\/p>\n After inspecting the rendered html I noticed that the html produced is like the following.<\/p>\n Turns out that highlight.js<\/a> can easily apply syntax highlighting to these elements.<\/p>\n As I mentioned earlier, my theme uses Node.js, and gulp to monitor for changes to sass (css generation) and javascript files.<\/p>\n I ran into another issue with trying to get my local version of my blog \/ development setup working again (since 2019) and that is to do with gulp 3.x not working with newer node.js versions. Again another reason why I need to learn more about Docker and actually this whole blog system should be "frozen" into a container. See the last section of this post regarding the troubleshooting I went through.<\/p>\n I added the following to my header.php file to fetch highlight.js<\/a> from Cloudflare CDN.<\/p>\n See this link for more options<\/a>.<\/p>\n Refreshed the browser and surprisingly this worked first time.<\/p>\n <\/p>\n Next I need to setup a colour scheme that works with the blog theme. This is surprisingly very easy. Just go to the demo site<\/a> and choose I theme you like. Note the name down (e.g. "Dracula") and then just replace the above link tag (e.g. …\/styles\/dracula.min.css). You can also use this<\/a> to help in finding the correct link.<\/p>\n I liked Night-Owl but did not liked that it used italics for keywords, function names etc. So I ended up cloning and modifying it and placed it under the same sass system currently being used by my theme.<\/p>\n <\/p>\n I just had to make one more modification to the background colour for <pre> tags to match that of Night Owl and I was ready to deploy the new theme to production.<\/p>\n Below is some of the steps I had to follow to just get gulp from my theme working in 2021. I so wish I just containerized the system.<\/p>\n […]<\/p>\nAdding code syntax highlighting (to my blog)<\/h2>\n
\n
\n
Running a local instance of WordPress (Apache, PHP) and MariaDB using Docker<\/h2>\n
docker-compose.yml<\/code> file I use to spin up a WordPress instance. But please be aware that this is based of stuff I did in 2019 and right now I am just focussing on getting this working again so that I can make changes to my WordPress theme and test it before installing it on the live site.<\/p>\n
version: "3.9"\nservices:\n # Create a container for running a MySQL Database (access it from 127.0.0.1:8081)\n wordpress-db:\n # Official mariadb docker image\n image: mariadb\n ports:\n # Map local port 8081 to the MySQL docker's 3306\n - "8081:3306"\n restart: always\n volumes:\n - wordpress-db-data:\/var\/lib\/mysql\n environment:\n MYSQL_ROOT_PASSWORD: INSERT_YOUR_PASSWORD_HERE\n MYSQL_DATABASE: wordpress\n MYSQL_USER: wordpress\n MYSQL_PASSWORD: wordpress \n\n # Create a container for running WordPress (access it from 127.0.0.1:8080)\n wordpress:\n depends_on:\n - wordpress-db\n # Official WordPress docker image\n image: wordpress\n restart: always\n volumes:\n # Map local .\/ directory as \/var\/www\/html (so that the install persists)\n - .\/wordpress:\/var\/www\/html\n ports:\n # Map local 8080 to Apache port 80\n - "8080:80"\n links:\n - wordpress-db:mysql\n environment:\n WORDPRESS_DB_HOST: wordpress-db:3306\n WORDPRESS_DB_USER: wordpress\n WORDPRESS_DB_PASSWORD: wordpress\n WORDPRESS_DB_NAME: wordpress\nvolumes:\n wordpress-db-data: {}\n<\/code><\/pre>\n
$ docker-compose up -d\n...\nCreating wordpress-db_1 ... done\nCreating wordpress_1 ... done\n<\/code><\/pre>\n
$ docker-compose logs -f\n...\nwordpress_1 | WordPress not found in \/var\/www\/html - copying now...\n<\/code><\/pre>\n
$ open http:\/\/127.0.0.1:8080<\/code><\/p>\n
$ docker-compose stop\n<\/code><\/pre>\n
Profit!<\/h3>\n
Adding highlight.js for code syntax highlighting<\/h2>\n
<pre>\n\t<code class="language-python">\n\t\t# Some python code here\n\t<\/code>\n<\/pre>\n<\/code><\/pre>\n
<head><\/h3>\n
<head>\n\t...\n\t<!-- highlight.js -->\n\t<link rel="stylesheet" href="\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/10.7.1\/styles\/default.min.css">\n\t<script src="\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/10.7.1\/highlight.min.js"><\/script>\n\t<script>hljs.highlightAll();<\/script>\n\t...\n<\/head>\n<\/code><\/pre>\n
Node, gulp and trouble<\/h2>\n
# Which node is currently installed\n$ nvm list\n-> v15.10.0\ndefault -> node (-> v15.10.0)\n\n# Which versions of node is available to download and install\n$ nvm ls-remote\n\n#-----------------\n\n# Uninstall the global gulp\n$ sudo npm uninstall -g gulp-cli\n\n# Install 10.15.1 like it is 2019\n$ nvm install 10.15.1\n$ nvm use 10.15.1\nNow using node v10.15.1 (npm v6.4.1)\n\n# Reinstall global gulp\n$ sudo npm install -g gulp-cli\n$ gulp --version\nCLI version: 2.3.0\nLocal version: Unknown\n\n# cd to where the node project is\n$ node install\ninternal\/modules\/cjs\/loader.js:583\n throw err;\n...\n\n# To fix this issue\n$ rm -rf node_modules\n$ rm package-lock.json\n$ npm install\n\n# Next issue about not finding local gulp\n$ npm install gulp\n# Nope\n# Try some more stuff\n# Nope\n\n$ npm install gulp@3.9.1 --save\n$ gulp --version\nCLI version: 2.3.0\nLocal version: 3.9.1\n\n$ gulp watch\n# It works! finally\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"