thecrumb.com - Under the hood: Docker
In the past I’ve had Pelican installed locally. This isn’t difficult but when I occasionally reformat my maching setting things up again is sort of a pain. It’s 2019 - how about we use a container!
While there were some Pelican images on Docker hub the install is so simple I just created my own:
FROM bitnami/minideb:latest
# Update & add prerequisites
RUN install_packages \
python \
python-dev \
python-pip \
python-setuptools \
git \
build-essential
ADD requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
WORKDIR /project
CMD ["make", "devserver"]
Lets step through this. First we need an image. I used this ‘minideb’ from Bitnami. It’s not as small as Alpine but it’s a familiar Debian system so I don’t have to worry about weird package managers, etc. And since this is local I really don’t care about image size anyway.
This minideb also has a ‘install_packages’ command built in which does a few nice things for you automatically:
- Install the named packages, skipping prompts etc.
- Clean up the apt metadata afterwards to keep the image small.
- Retrying if apt fails.
Nice!
So we install all the requirements for running Pelican: Python, Git and some build tools.
Then we copy over a pip requirements file from my blog files and use pip to actually install Pelican and a few other things:
beautifulsoup4
cssmin
markdown
pelican
webassets
Next we define a directory in the image and run the Pelican command ‘make devserver’ which will serve my local files and rebuild things if I change a file.
Now we need to run this container.
I can never remember all the port and volume flags so I just created a simple script:
#!/bin/bash
wd=$(pwd)
docker rm -f thecrumb/pelican
docker run \
--rm \
--name pelicanbox \
-p 80:8000 \
-v $wd/git/.gitconfig:/root/.gitconfig \
-v $wd/ssh:/root/.ssh \
-v ~/www/thecrumb:/project \
-it thecrumb/pelican
So this will run Docker, set the ports, copy over some git and ssh things and finally create a volume mapped to my local files where I have my actual blog content - www/thecrumb. Now Pelican can build with my content.
Running that will serve up my blog locally at localhost. And I can edit files and my content will be regenerated.
jim@jim:/mnt/Files/www/thecrumb$ ./start-docker.sh
pelican -lr content -o output -s pelicanconf.py
-> Modified: content, settings, theme, [static]images. re-generating...
So now we have Pelican containerized. I store the dockerfile and start-docker.sh in the root of my blog so it’s under version control.
Now I can craft a blog post and view it locally.
In the next post I’ll go over how I get my files to the server via GitLab CI.