Introduction

This article covers how one could create new Jekyll projects and/or run them using Docker.

Prerequisites

In order to follow along, you need to have the following installed in your system:

Try to run the following commands:

$ docker run hello-world
$ make --version

If they ran successfully, then you can continue to the next steps.

Create a new Jekyll project

$ mkdir ~/jekyll-site
$ cd jekyll-site
$ docker run -v $(pwd):/srv/jekyll \
    jekyll/jekyll:3.8.6 \
    jekyll new . 

A fresh Jekyll installation is now on the ~/jekyll-site directory.

Run a Jekyll project

Given an existing Jekyll project at ~/jekyll-site:

$ cd ~/jekyll-site
$ docker run -v $(pwd):/srv/jekyll \
    -p 4000:4000 \
    -it \
    jekyll/jekyll:3.8.6 \
    jekyll serve

The project is now served on your host at http://127.0.0.1:4000/. Note that the -v $(pwd):/srv/jekyll volume flag attaches your current directory to the container’s – this effectively enables the jekyll serve hot refresh feature.

Use a Dockerfile and make

Again, given an existing Jekyll project at ~/jekyll-site, we can create a Dockerfile:

# ~/jekyll-site/Dockerfile
FROM jekyll/jekyll:3.8.6

WORKDIR /app

COPY . ./

EXPOSE 4000

CMD ["jekyll", "serve"]

..and a Makefile:

build:
	docker build -t yourname/your-jekyll-project .

serve:
	docker run -v ${PWD}:/app -v ${PWD}/vendor/bundle:/usr/local/bundle -p 4000:4000 -it --rm --name your_jekyll_project yourname/your-jekyll-project

The Makefile should be self-describing. An important detail to understand is the new volume that we attach to the container’s /usr/local/bundle directory. This is the location where Jekyll’s dependencies are installed, and mapping this volume to our filesystem will require the dependencies to be installed only once every time we spin up the container.

You can now commit these two files with your version control system and run your project in any Docker enabled machine with the following commands:

$ make build
$ make serve

In which again, the project is served at http://127.0.0.1:4000/


Got any feedback or suggestions? Feel free to send me an email or a tweet.
Ciao!