Creating Docker container images of Jekyll applications
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.
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 the reverse proxy serving
https and managing its SSL certificate is being handled by the container orchestrator. In my case, it is handled by Dokku, but it could be the Kubernetes Ingress Controller, Traefik or another of these 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 the 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
docker run --rm -v "$PWD:/srv/jekyll" -it jekyll/builder:latest bundle update
Develop applications with live refresh:
docker run --rm -v "$PWD:/srv/jekyll" -p 4000:4000 -it jekyll/builder:latest jekyll serve --force_polling --drafts