Docker Compose and Jekyll

X @urre

Docker Compose and Jekyll

I still use Jekyll for this site. It’s still great, write in Markdown, build a static site, host everywhere.

But, my plan is to migrate to Gatsby and MDX pretty soon. But in the meantime, it does the job. Recently i switched to a new computer, and this time I didn’t want to install Ruby since I don’t use it for anything else nowadays. In the past I’ve used Rbenv for managing a local Ruby installation which is great, but i wanted to use Docker Compose this time. I am fine installing Ruby if it’s isolated in a container.

So, lets use Docker Compose to handle the container.

Option 1: Simple and easy

The following docker-compose.yml file will do the job.

version: '3'
services:
  jekyll:
      image: jekyll/jekyll:3.8
      environment:
        - JEKYLL_ENV=development
      command: bundle exec jekyll serve --watch --incremental --config _config-dev.yml --host 0.0.0.0
      ports:
          - 4000:4000
      volumes:
          - .:/srv/jekyll:cached
          - ./vendor/bundle:/usr/local/bundle:cached

The notes

  • Using the default Jekyll Docker image
  • Using Bundler to start Jekyll.
  • Specify a separate config file for development
  • Use cached volumes for performance
  • To make Jekyll available outside localhost, use --host=0.0.0.0

✅ Now all we have to do is run docker-compose up -d and head over to http://localhost:4000

Option 2: Custom nginx service and HTTPS

The notes

The docker-compose.yml

version: '3'

services:
  jekyllbuild:
    build:
      context: .
      args:
        build_command: "bundle exec jekyll serve --watch --drafts --incremental --config _config-dev.yml"
    volumes:
      - ".:/srv/jekyll"
    ports:
      - 4000:4000
  nginx:
    image: nginx
    volumes:
      - ".:/var/www/public"
      - "./nginx/nginx.conf:/etc/nginx/nginx.conf"
      - "./nginx/html/404.html:/usr/share/nginx/html/404.html"
      - ./certs:/etc/certs
    ports:
      - 80:80
      - 443:443

In this example I use a separate nginx service and exposing ports 80 and 443. As a bonus we can now use HTTPS by creating a cert for localhost:

brew install mkcert
mkcert -install
mkcert localhost --ca-key --ca-cert (generates SSL certs, move them to ./certs)

The Dockerfile

FROM jekyll/jekyll:latest

ARG build_command
ENV BUILD_COMMAND ${build_command}

RUN bundle install

CMD ${BUILD_COMMAND}

✅ Again, just run docker-compose up -d and head over to https://localhost