Home Running a Jeckyll blog locally with Docker
Post
Cancel

Running a Jeckyll blog locally with Docker

I build my blog with Jekyll and host it on Netlify. Before pushing the site to github for Netlify to build and deploy it I like to build and run the site locally. I do this with Docker by running the following command in the root of my Jekyll site:

1
docker run -i -t --rm -u 1000:1000 -p 4000:4000 -v $(pwd):/opt/app -v $(pwd)/.bundler/:/opt/bundler -e BUNDLE_PATH=/opt/bundler -w /opt/app ruby:2.7 bash -c "bundle install && bundle exec jekyll serve --watch -H 0.0.0.0"

In practice I put this command in a deploy.sh bash file and then I only have to remember to run the bash file.

This command builds the site and then I can view it at localhost:4000 in my browser. It takes a few minutes to build on the first run but eventually I get this message to say the server is running: ` Server address: http://0.0.0.0:4000/ `

To break this command down:

  • docker run runs a new container
  • -i -t attaches the terminal to the container
  • --rm removes the container when it stops
  • -u 1000:1000 sets the user in the container to the same as the host user (to avoid permission issues)
  • -p 4000:4000 maps port 4000 on the host to port 4000 on the container (change the first value to map to a different port address in your browser)
  • -v $(pwd):/opt/app mounts the current directory to /opt/app in the container
  • -v $(pwd)/.bundler/:/opt/bundler mounts the .bundler directory to /opt/bundler in the container (this holds the Ruby gems dependencies)
  • -e BUNDLE_PATH=/opt/bundler sets the BUNDLE_PATH environment variable to /opt/bundler
  • -w /opt/app sets the working directory in the container to /opt/app
  • ruby:2.7 is the Docker image to use
  • bash -c "bundle install && bundle exec jekyll serve --watch -H 00.0.0.0" runs the commands in the container to install the gems and start the Jekyll server

I’ve used this setup for a few years now and it works well for me. I can build and run the site locally without having to install Ruby or Jekyll on my machine.

Next steps

Want to know more about Polars for high performance data science? Then you can:

This post is licensed under CC BY 4.0 by the author.