Setup Jekyll runtime with Docker
To get a minimal Docker image (~43 MB) for your Jekyll site, write the following Dockerfile:
Make sure to remove
jekyllor any other native Ruby extensions from yourGemfile
# syntax=docker/dockerfile:1.2
FROM alpine:3.17
RUN apk add --no-cache jekyll
WORKDIR /srv
COPY . .
CMD [ "sh", "-c", "jekyll serve --trace --livereload --host 0.0.0.0" ]
EXPOSE 4000
- The syntax directive tells BuildKit backend to use the Official releases stable channel with patch updates.
FROMsets the minimalAlpine Linuxversion 3.17 from theDockerHubregistry as the base image (~15 MB).- Now,
Jekyllis a native Ruby extension and thus requires installingGCC,G++andMakewhich increases the build time and the image size significantly (+340 MB) and so the trick is to install a statically linked compiled binary from the Alpine repository which in turns downloads all the dependencies includingRuby,GemandBundleresulting in a much smaller image (~46 MB). - Create and set the working directory to
srv(server) following Linux system directories naming conventions. - Copy everything from the host working directory to
srv(except files & folders as per.dockerignore) - Declare a command to run when spawning a container out of this image
- Execute Bourne Shell passing the command to run as a string using the
-cflag. - Run the Jekyll build command with the option:
--traceShow the full backtrace when an error occurs (stdout to Docker container logs).
- Run the serve sub-command by Jekyll binary passing options
--livereloadto reload a page automatically on the browser when its content is edited.--hostto listen at the given hostname0.0.0.0which means all available interfaces including Docker container created one.
- Execute Bourne Shell passing the command to run as a string using the
Build the image from the Dockerfile at the current directory
docker build --no-cache --tag my-jekyll-image .
--no-cacheto avoid any dangling corrupted build cache.-tto tag the image by the given name.
Start a container
- Start a CLI detached container with a name mapping Jekyll’s serving and live reloading ports to auto refresh the page on file changes bind mounting the current directory on your host system to the container’s working directory volume to persist changes.
--rmoption removes the container when it stops (avoids name collision on next start).docker run --name my-jekyll-container -dp 4000:4000 -p 35729:35729 -v $(pwd):/srv --rm my-jekyll-imageYou might need to wait a couple of seconds for the server to start
that’s it