This is how I use, build, and test my jekyll site locally without polluting my system with npm or ruby installations. Instead, I rely on a docker installation to run the tooling.
The key to this working is to put the following shell script jekyll.sh
in my jekyll site repository. This script allows me to run commands that you would normally be able to run if you had jekyll installed natively.
1
2
3
4
5
6
7
export JEKYLL_VERSION=3.8
docker run --rm \
-p 127.0.0.1:4000:4000/tcp \
--volume="$PWD:/srv/jekyll" \
--volume="$PWD/vendor/bundle:/usr/local/bundle" \
-it jekyll/jekyll:$JEKYLL_VERSION \
$@
Let’s go through the script line by line.
export JEKYLL_VERSION=3.8
JEKYLL_VERSION
to 3.8
. This will be used when pulling the docker image in the next commanddocker run --rm \
-p 127.0.0.1:4000:4000/tcp \
--volume="$PWD:/src/jekyll" \
/src/jekyll
on the container. This is where the the container expects to find the jekyll site source code--volume="$PWD/vendor/bundle:/usr/local/bundle" \
/vendor/bundle
folder inside the present working directory to the container’s /usr/local/bundle
folder. This is where the jekyll commands will cache ruby packages. These packages will be persisted on the host machine inside the mapped folder which allows caching to happen between commands even though the container is removed after each command-it jekyll/jekyll:$JEKYLL_VERSION \
-it
flags specifies that we will be interacting with the container’s shell so it will map “standard in” (stdin) to the host’s shell to allow user inputjekyll/jekyll
docker image with the specified version set in step #1$@
is a shell script feature that maps the user arguments to the shell’s command. If you run the script like ./myscript.sh firstArg secondArg
, those arguments would replace the $@
in the shell script. This allows the person running the script to pass in any command they want and it will attempt to run the command in the docker containerFinally, you can set this script (we’ll call it jekyll.sh
) to be executable and then use it to run jekyll commands on your host machine.