13 Apr 2023
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
FROM sets the minimal Alpine Linux version 3.17 from the DockerHub registry as the base image (~15 MB).Jekyll is a native Ruby extension and thus requires installing GCC, G++ and Make which 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 including Ruby, Gem and Bundle resulting in a much smaller image (~46 MB).srv (server) following Linux system directories naming conventions.srv (except files & folders as per .dockerignore)-c flag.--trace Show the full backtrace when an error occurs (stdout to Docker container logs).--livereload to reload a page automatically on the browser when its content is edited.--host to listen at the given hostname 0.0.0.0 which means all available interfaces including Docker container created one.Dockerfile at the current directorydocker build --no-cache --tag my-jekyll-image .
--no-cache to avoid any dangling corrupted build cache.-t to tag the image by the given name.--rm option 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-image
You might need to wait a couple of seconds for the server to start
that’s it
Get in touch_