Running Jekyll locally with Docker
This website is built with static site generator Jekyll and hosted on GitHub pages.
There are a number of reasons why this combination is awesome:
- I can write everything in Markdown
- The workflow has source control baked in
- The website is automatically updated by pushing to master
But using Jekyll has its own downsides, for example you have to make sure you have the correct version of Ruby and some gems installed. Setting it up takes a couple of steps and installations. This is where Docker comes into play.
I have been using docker since one year now and I am absolutely in love with it. You can run Jekyll from inside a Docker container, you won’t have to bother with installing a specific version of Ruby or with different versions of gems you already had installed potentially clashing. You could of course use the Ruby Version Manager, but working with Docker really is a breeze.
There is a Docker image available on Docker Hub. Let’s take advantage of that! Suppose you have downloaded or cloned the jekyll repository into
~/jekyll-site/. To use the Jekyll image without having to pass in the required options every time we would create a container and will make use of Docker Compose. Create a file called
~/jekyll-site with these contents:
jekyll: image: jekyll/jekyll command: jekyll serve --watch --drafts --incremental ports: - 4000:4000 volumes: - .:/srv/jekyll
image: jekyll/jekyll:pagesyou indicate you want to use Jekyll’s Jekyll image tagged with “pages”. This is a specific image suited for GitHub pages.
- The part after
command:is the command to execute in the container. The
jekyll servecommand starts Jekyll’s builtin development web server. The options
--incrementalinstruct Jekyll to automatically regenerate the HTML when a file is changed.
--draftslets you view posts those are in the
4000:4000forwards port 4000 of the container to your local port 4000.
- Finally, on the last line you map the current directory (
/srv/jekyll/. That’s where the images is configured to go looking for a Jekyll site.
That’s all! You can start your Jekyll site by browsing to your site’s directory using a terminal and doing
If you go to http://0.0.0.0:4000 (or the IP of your VM if you’re running Docker on OS X or Windows) in a browser you’ll see your Jekyll site. Easy, right?
When you want to shut-down the site, use
Ctrl+C then run