Craig Huther Wielding magic and tech, often hand in hand

Self Hosting Jekyll with Docker Compose

I used Jekyll in the past and loved how simple it was to add posts to and edit. While you can host Jekyll directly from github pages, I’m a fan of self hosting as much as I can. Originally I installed Jekyll on one of my VM’s, but setting up Ruby was a pain (I’m not a big Ruby user). Docker Compose to the rescue!

You should start on a computer with docker and docker compose installed. We are going to use the theme minima. You may use any theme you want.

Variables

These specific things are encouraged to be changed on your setup

Docker Folder: craighuther.com
Linux User: craig
Port: 4000
VM IP address: 192.168.1.100

Text editor of choice: vi

Setup

First up, I typically setup each docker I’m running in a subfolder in the /srv/ folder. So go ahead and create a folder under there, and give yourself ownership:

sudo mkdir /srv/craighuther.com
sudo chown -R craig:craig /srv/craighuther.com
cd /srv/craighuther.com

Then, clone the theme into a folder called jekyll:

git clone https://github.com/jekyll/minima.git jekyll

Now we create the docker-compose.yml file:

version: '3'

services:
  jekyll:
    image: jekyll/jekyll:latest
    command: jekyll serve --watch --force_polling --verbose
    ports:
      - 4000:4000
    volumes:
      - ./jekyll:/srv/jekyll

Your folder structure should be as follows:

/srv/
|-- craighuther.com/
|   |-- docker-compose.yml
|   |-- jekyll/
|       |-- theme files

Explaining the docker-compose.yml

  • We named the service jekyll
  • The docker image is jekyll/jekyll:latest.
  • jekyll serve --watch --force_polling --verbose servers up the site
    • --watch tells the containter to update if files change.
    • --force_polling makes watch use polling.
    • --verbose outputs logs to std_out so we can view them using docker logs jekyll

Now we bring it up via:

docker-compose up -d

  Creating craighuthercom_jekyll_1 ...
  Creating craighuthercom_jekyll_1 ... done

The -d detaches it, so it runs in the background. It will take a few moments to get it up and going. Type in docker logs craighuthercom_jekyll_1 to see the progress You should see a line in the logs that reads ` Server running… press ctrl-c to stop.` when the site is up and running.

Navigate to the IP address of your VM and put the port in 192.168.1.100:4000. You should see the basic site up and running now: Jekyll up and running

If you do not see the site up yet

You can use docker logs craighuthercom_jekyll_1 to try and diagnose the issues.

Adding your own posts

Now we have the site up and running locally, lets add some posts, but outside of the /srv/craighuther.com/jekyll folder so we can keep pulling releases from git. Lets add a posts folder and change the docker-compose.yml to include it.

mkdir posts

Add a second volume to the docker-compose.yml:

version: '3'

services:
  jekyll:
    image: jekyll/jekyll:latest
    command: jekyll serve --watch --force_polling --verbose
    ports:
      - 4001:4000
    volumes:
      - ./jekyll:/srv/jekyll
      - ./posts:/srv/jekyll/_posts

Inside the posts folder, lets add an example post, 2019-05-23-example.md and put the following inside:

---
layout: post
title: Example Post
---

## Example page!

Examples here!

Now we will have to re-create the docker;

docker-compose up -d --force-recreate

And wait for it to finish. You can use docker logs craighuthercom_jekyll_1 to see the progress. You should see the following: Jekyll custom pages

Now, whenever you edit or add a new blog page to the posts folder, jekyll will automatcally sense this and refresh the site.

If you want to edit anything in the theme you decide to use, make a copy of the folder you want to edit into the /srv/craighuther.com/ folder. Then add a similar volume to the docker-compose.yml. Edit the files in the created directory instead of inside the /srv/craighuther.com/jekyll folder. This will allow you to pull update from the theme easily just by running a git pull in the /srv/craighuther.com/jekyll folder.

Credits

Matthias Lischka

[ tech  selfhosting  docker  jekyll  ]