Skip to main content

Dockerizing Jekyll

·2 mins

Let’s take the last two posts about Jekyll and Docker and create a Docker image as a basis for containers serving a Jekyll blog.

Whenever the image gets built and the container is being run the following will happen:

  • Code for running git and Jekyll will be installed.
  • A git repo containing the Jekyll blog will be downloaded.
  • Jekyll will be run, building and serving the static site.

The Dockerfile looks like this:

{% gist 41c14bedd5fc05cc2ab8 Dockerfile.jekyll_serve %}

It is being built with

docker build —tag=“your_tag/jekyll_serve” —rm=true .

After that its run with

docker run -d -p 4000:4000 —name jekyll_serve your_tag/jekyll_serve:latest

In this case, port 4000 of the docker container is being mapped to port 4000 on the docker host.

In order to test if our blog is being served on that port, just do a curl on the docker host to localhost:4000. You should retrieve the index of the newly created blog.

In case you work with a Vagrant box as your docker host, you can also add the following line to your Vagrantfile to enable port forwarding for Jekyll to your physical host.

config.vm.network “forwarded_port”, guest: 4000, host: 4000

Now, when I open my browser on my Mac and type localhost:4000, I will be forwarded into my Vagrantbox on port 4000 and that will forward me to the containers port 4000.

Another option is to manually add the port forwarding from physical host to Vagrantbox under VirtualBox in Settings->Network->Advanced->Port Forwarding.

Virtual Box Port Forwarding

In this post you have learned how to create your own Docker image that is building and serving a Jekyll blog. If its your first Dockerfile, then congrats, it won’t get much more complicated when creating Dockerfiles, you have covered the most important steps already.

Done for today!