Creating Docker container images of Jekyll applications
Jekyll is a simple static website generator made in Ruby, famous for being the preferred tool for publishing GitHub Pages.
Though the Jekyll/GitHub relationship may feel like the natural setup, there are times when one might want to save a compiled Jekyll project into a container for it to be deployed on another platform.
One of the reasons you may want to do that is to circumvent the limitations of the allowed plugins on GitHub pages. Additionally, you may wish to fine-tune how your application is being served by customizing server headers and such.
In my case I am using the configuration described here to publish a container’d versions of my blog on a Dokku installation.
Dockerfile
Start by creating a Dockerfile
at the root of your Jekyll application. The file will be very simple since it only has to define how to build the project files and define a way to serve them.
Here is the Dockerfile
configuration used by this blog :
FROM jekyll/builder AS build
WORKDIR /app
COPY . .
RUN mkdir .jekyll-cache _site \
&& jekyll build --trace
FROM nginx
COPY --from=build /app/_site /usr/share/nginx/html
EXPOSE 80
Notice that the container is exposing port 80
because serving https
(443
) and managing SSL certificates is being handled by the container orchestrator or a reverse proxy installed in it.
In my case, this is being handled by Dokku, but it could be the Kubernetes Ingress Controller, Traefik or another of these types of tools.
From there you may create the container with the docker build
command:
docker build -t my_jekyll_application:latest .
Maintenance & development
While you are at it, you might as well do everything in a container and skip installing Ruby and Jekyll on your machine altogether.
The commands are well documented in the builder image’s documentation, but here are two useful invocations that I use repeatedly:
Update the application’s Gemfile
:
docker run --rm -v ${pwd}:/srv/jekyll -v ${pwd}/vendor/bundle:/usr/local/bundle -it jekyll/builder:3.8 bundle update
Develop applications with live refresh:
docker run --rm -v ${pwd}:/srv/jekyll -v ${pwd}/vendor/bundle:/usr/local/bundle -p 4000:4000 -it jekyll/builder:3.8 jekyll serve