To get started let’s abstract our Jekyll build into a Dockerfile, it is a bit more work in the beginning but once we are done our Dockerfile should be re-usable for the rest of our Jekyll projects.

Dockerfile

FROM ruby

WORKDIR /usr/src/app

COPY . /usr/src/app

RUN \
apt-get update && \
apt-get install -y build-essential && \
rm -rf /var/lib/apt/lists/* && \
bundle install && \
jekyll clean && \
jekyll build

EXPOSE  4000

CMD ["jekyll" "--help"]

To build the Docker image the command is quite simple:

docker build -t myblog.com .

This will build a Docker image tagged as myblog.com

To run that image and tell it to run jekyll serve the command looks like this, it is quite long so put it in a bash script:

docker run -it --rm -v $(pwd):/usr/src/app -p 4000:4000 myblog.com ruby -S jekyll serve --host=0.0.0.0 --watch --force_polling

You can run any Jekyll command within the image like this:

Let’s run a normal Jekyll build:

docker run -it --rm -v $(pwd):/usr/src/app -p 4000:4000 myblog.com ruby -S jekyll build

Now a clean:

docker run -it --rm -v $(pwd):/usr/src/app -p 4000:4000 myblog.com ruby -S jekyll clean

 

Ryan is a hacker dad living in Cape Town, South Africa who likes Skate Boarding JavaScript and Functional Programming.

ryanwild @javascript_fn