Run your jekyll blog in docker
Docker… can’t live with it… can’t maintain the Ruby runtime on my machine without it…
I had designated my old laptop the “ruby machine” but I’m away from home and I wanted to design something on my blog, which is tricky if you can’t run it. So here we are, grudgingly using docker again.
Docker is supposed to make things easy but it is its own world of complexity hell in case you hadn’t noticed.
There’s an official jekyll image and it doesn’t work
It’s called jekyll/jekyll
and it supports Jekyll 3.8 and 4.2 but not 3.9, which is the one everyone uses.
Btw. you can download a docker image locally with
docker pull suchandsuch
but you don’t have to;docker run
is an all-in-one command which downloads an image and dependencies and runs it up into a container.
Just run your blog they say
If your blog was supported by jekyll/jekyll
, you could simply open your blog root on disk, open a git bash or something and type:
export JEKYLL_VERSION=3.8
docker run --volume="$PWD:/srv/jekyll" -p 4000:4000 -it jekyll/jekyll:$JEKYLL_VERSION jekyll serve --watch
That said, I had trouble with the $PWD
syntax on windows, which did something funky like trying to create a C:\Program Files\git\srv
folder.
I later tried:
docker run --rm \
--volume="//d/git/stegriff:/srv/jekyll" \
--publish [::1]:4000:4000 \
jekyll/jekyll:$JEKYLL_VERSION \
jekyll serve
And that would work, except that it doesn’t align with my ruby and jekyll versions! Yay, dependency management is so fun and motivating!
The Internet Of Frustrated Bloggers
A few people already walked this path and posted it (this is why I blog too!)
My .ruby-version
file says 3.2.3
I can check Ruby on Docker Hub and see that they have a tag for 3.2.7-slim-bullseye
which seems right.
My dockerfile
Credit for most of this goes to flyingpreacher. When it wouldn’t work, I opened my blog repo in VS Code and asked GH CoPilot what was wrong. It pointed out that I needed to add the EXPOSE 4000
docker command and the --host 0.0.0.0
argument to the jekyll command.
# Create a Jekyll container from a Debian image
# With thanks to https://flyingpreacher.github.io/
# My ruby-version is 3.2.3 so let's use
FROM ruby:3.2.7-slim-bullseye
# Add Jekyll dependencies to Debian
RUN apt-get update && apt-get upgrade -y
RUN apt-get install build-essential git -y
# Update the Ruby bundler and install Jekyll
RUN gem update bundler && gem install bundler jekyll
# When you launch this, mount your blog into /blog dir like
# docker run ... --mount type=bind,src=D:/git/stegriff,target=/blog sg-blog
RUN mkdir blog
RUN cd blog
EXPOSE 4000
CMD cd blog && bundle install && bundle exec jekyll serve --host 0.0.0.0 --force_polling
To build:
docker build . -t sg-blog
To run:
docker run --rm -it -p 4000:4000 --mount type=bind,src=D:/git/stegriff,target=/blog sg-blog
You could even take a command like that and move it to a run.sh
file or something.
Does it work?
I can now build and preview my blog this way. Initially, when I changed a file in my repo, the blog didn’t rebuild! Asked GHCP again and it said to add the --force_polling
bit, which worked.
Was it worth it
No, I don’t think so.
The time it took to get this working would have been better spent downloading and installing the appropriate Ruby version and stuff.
The docker image necessarily contains not just ruby but also a whole Debian distro. The image is 526 MB (seems high). Running it takes 92 MB of RAM (seems ok). But as it’s dog slow, took ages to set up, and doesn’t save me any space in practical terms, I don’t like this solution.
I do savour all successes with the whale though 🐋🐋
See you next time 🐳👀