If you want to create a simple website using Jekyll or GitHub Pages, the typical route you take is to install Ruby and Jekyll on your computer. There are so many variables in getting it right, that many poeple give up in frustration. Using a Docker container is such an easier way to go.
This repo supports my YouTube video that shows how to set up a Docker container on your computer so you do not have to install Ruby and Jekyll locally on your own computer.
- How to use Develop Jekyll (and GitHub Pages) locally using Docker containers
- Benefits of using Docker with Visual Studio Code remote containers
- Prepare your desktop
- Reference links
- To create a new GitHub Pages website from scratch
- STEP 1: Create a GitHub Pages repo
- STEP 2: Enable GitHub Pages
- STEP 3: Install Visual Studio Code extensions
- STEP 4: Clone the GitHub repo to your local computer
- STEP 5: Determine Jekyll dependencies
- STEP 6: Determine your docker image
- STEP 6: Create a dockerfile and a Docker container
- STEP 7: Build the Jekyll website
- STEP 8: Run the Jekyll website
- STEP 9: Prepare your code for GitHub Pages
- STEP 10: Update the .gitignore file (optional)
- Closing the container
- Opening the container
- FAQs
- Your code lives on your computer, but the website you are testing is built inside a container
- You can test your work locally using any browser on your computer
- No need to install Ruby on your desktop
- No need to install Jekyll on your desktop
- Switch versions of Ruby or Jekyll at any time
You will need the following accounts or apps on your computer before getting started
STEP 1: Sign up for Docker and download Docker Desktop
STEP 2: Sign up for GitHub for free
Download Visual Studio Code for free
If you ever have questions about using Jekyll or finding out dependencies, I provide these helpful links here.
Bill’s Jekyll and GitHub Pages YouTube playlist
GitHub Pages dependencies
Jekyll dependencies
Docker Hub
devcontainer.json reference documentation
Code.visualstudio.com remote container docs
- Create a new GitHub repo with the following options:
option | Setting |
---|---|
Name | my-jekyll-docker-website (or whatever you would like to name it) |
Description | Testing Jekyll development using Docker |
Add a README file | Yes |
Add a .gitignore file | Yes. Also, select the Jekyll template |
- Go to your new repo and go to settings
- Select the Pages menu
option | Setting |
---|---|
Source | main (branch) |
Theme | none |
- You will be provided with a GitHub Pages url link. Copy that to the clipboard
- Return to your repo, click the gear icon and then paste the url link into the Website field
- Add the Docker extension from Microsoft
- Add the Remote - Container extension from Microsoft
- Restart Visual Studio Code
- In Visual Studio Code, type COMMAND+SHIFT+P, then git clone
- Type or paste the url for the GitHub pages repo you created
- Select a folder
- Open the cloned repo
Option 1 (latest version of Jekyll): If you want to use Jekyll and do not care about GitHub Pages, go to the Jekyll Docs website and find the minimum version of Ruby. The latest version of Jekyll is always on the top-right corner of the site. Here are my recommended settings for the latest version of Jekyll:
Option | Setting |
---|---|
Jekyll 4.x.x | Ruby 3.0.3 (maybe greater) |
Note | Use this option if you plan to build your site with a third-party host or use GitHub Actions to build your site |
Option 2 (GitHub Pages): If you want to create a Jekyll site using GitHub Pages, GitHub only supports certain versions. To learn what version GitHub supports and the minimum Ruby version, check out the GitHub Pages dependency versions page. As of this writing (April 1, 2022), I recommend the following:
option | Setting |
---|---|
Jekyll 3.9.x | Ruby 2.7.x or greater, but not 3.x |
Most Jekyll docker images are designed for x86-64 chipsets. That means if you are using an ARM chipset (like Apple Silicon or Windows on ARM), it will (a) not run or (b) run very slowly. Also, many of these images may contain dependencies you do not want or too many dependencies.
I recommend you start with a small and portable Ruby image based on Alpine. If you decide to use something else, then my instructions may not work, so you might want to try my recommendation first and then go on your own after that.
- Login to Docker Hub
- Locate the official Ruby Docker image
- Determine the image you want to use. As of this writing, I recommend:
option | Setting |
---|---|
Jekyll 3.9.x / GitHub Pages | 2.7.5-alpine3.15 (or just 2.7-alpine) |
Jekyll 4.x.x | 3.0.3-alpine3.15 (or just 3.0.3-alpine) |
- Add the following fcode to your
dockerfile
, being sure to uncomment one of theFROM
lines:
# Create a Jekyll container from a Ruby Alpine image
# At a minimum, use Ruby 2.5 or later
# Uncomment the following line if you want to use GitHub Pages (Jekyll 3.9.x):
# FROM ruby:2.7-alpine3.15
# Uncomment the following line if you want to use the latest version of Jekyll:
# FROM ruby:3.0.3-alpine3.15
# Add Jekyll dependencies to Alpine
RUN apk update
RUN apk add --no-cache build-base gcc cmake git
# Update the Ruby bundler and install Jekyll
RUN gem update bundler && gem install bundler jekyll
- Save the dockerfile
- Type
SHIFT+COMMAND+P
, typecommit all
- Type
Create dockerfile
and pressreturn
- Type
SHIFT+COMMAND+P
, typeopen folder in container
and pressreturn
- Select the
folder
containing your cloned repo - Select the
From dockerfile
option - VSC will build the container. This may take a long time for the first time
- Optionally, you can update the
devcontainer.json
file, but I do not cover those instructions here. To learn more, go to the Remote container docs website
- In Visual Studio Code, open the terminal window and type the following commands:
bundle init
# --- Decision time!
# Either install Jekyll 3.9.x for for official GitHub Pages support
bundle add jekyll --version "~>3.9.0"
# Or install the latest version of Jekyll 4.x
bundle add jekyll --version "~>4"
# ---
bundle install
bundle update
bundle exec jekyll new --force --skip-bundle .
bundle add webrick
bundle install
bundle update
- In Visual Studio Code, open the terminal window and type:
bundle exec jekyll serve --livereload
- Select the
Open in Browser
option and verify the site is running (note: Visual Studio Code has a built-in browser and it may or may not work, so use your desktop browser for best results) - After you verify Jekyll is running, go to the terminal in Visual Studio Code and type
CONTROL+C
to stop Jekyll
Note: Even if you are not using GitHub Pages, you still need to set the url
and baseurl
. I only cover how to do that for GitHub Pages here
- Go to your GitHub Pages repo and copy the link for your GitHub Pages website (not the repo, but the website link)
- In Visual Studio Code, with the repo open and edit the _config.yml file and modify the
baseurl
andurl
fields.
In this example, I will use the GitHub Pages url from this repo, which looks like this:
https://billraymond.github.io/my-jeckyll-docker-website/
baseurl example: “/my-jeckyll-docker-website”
url: “https://billraymond.github.io”
Notice that I removed the trailing /
at the end of the original url as that is not required
- Save the
_config.yml
file and close it - Type
COMMAND+SHIFT+P
, typecommit all
, and pressreturn
- Type
Create Jekyll site
, and pressreturn
- Type
COMMAND+SHIFT+P
, typeGit Push
, and pressreturn
- Go back to your GitHub Pages repo and refresh the page to make sure the files on your desktop now exist on GitHub
- After 30 seconds to 5 minutes, go to the GitHub Pages website url and verify the new Jekyll website is running
I did not cover this in the original video, but you may want to update your .gitignore
file with more options.
Note: You probably have some of these entries already. Also, the .DS_Store
option is for macOS, but it will not matter if you include it or not on another OS.
- In Visual Studio Code, select the current
.gitignore
file and paste in the following code:
_site/
.sass-cache/
.jekyll-cache/
.jekyll-metadata
**/.DS_Store
.bundle/
vendor/
Technically, you are done now. However, I will share you some additional steps to learn how to work with the container.
It is a good practice to close the container when you are done coding as it will free up memomry and resources on your base operating system. You can do that directly inside Visual Studio Code.
- To close and exit the container, in Visual Studio Code, close it by typing
COMMAND+SHIFT+P
, then typeClose remote connection
, and pressreturn
If you are familiar with Visual Studio Code, you probably go to the file menu and open a folder. With containers, it is almost the same, but with slightly different steps.
- To open and start the container, run Visual Studio Code, type
COMMAND+SHIFT+P
, typeOpen Folder in container…
, and then pressreturn
- Select the folder containing your Jekyll/GitHub Pages code that contains a
dockerfile
and pressreturn
- If the container already exists on your computer, it should start immediately. If it does not exist, Visual Studio Code will create a new Docker image and container (and possibly a Docker volume)
- Begin developing!
- Don't forget you can see your changes in real time by running Jekyll with the following command in the Visual Studio Code terminal window:
bundle exec jekyll serve --livereload
A: Good news! The code you work with is always on your local computer, not the container. Follow the steps in this page under the section titled Opening the container. However, for the first time, before you can run jekyll serve
, you must run the following commands:
bundle install
bundle update
Not a problem. Clone the code to your local computer, then follow the same steps I share in the FAQ titled What if I delete the container
Important: You will have to modify the _config.yml
file and update it with your new baseurl
and url
, as mentioned in STEP 9
earlier in this README
A: So as mentioned in the FAQ titled What if I delete the container, the bundle install
and bundle update
commands need to be run each time after the container has been created. But what if we would like to automate this step? Well lucky for us, there is a way!
We just need to make use of the onCreateCommand
command property.
If we open up the .devcontainer\devcontainer.json
file, it should look like this:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../dockerfile"
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
We need to do the following:
- Uncomment
// "postCreateCommand": "apt-get update && apt-get install -y curl",
and remove the comma,
at the end. - Replace
"apt-get update && apt-get install -y curl"
with"bundle install && bundle update"
(we use the&&
to be able to chain multiple commands together). - Add a comma
,
to the end of"dockerFile": "../dockerfile"
.
So the file should look like this:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../dockerfile",
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
"postCreateCommand": "bundle install && bundle update"
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
Now save the file, and try deleting and recreating the container, after the container has been created it should automatically start running the commands we just added. They should be visible within the terminal.
More information about this and other command properties can be found here.