Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created August 26, 2024 01:47
Show Gist options
  • Save jpalala/da28a2deba3cdd4dc8b09ac21ef8d8af to your computer and use it in GitHub Desktop.
Save jpalala/da28a2deba3cdd4dc8b09ac21ef8d8af to your computer and use it in GitHub Desktop.
Docker run jekyll

Setting up a Jekyll environment with Docker can streamline your development process, especially if you're looking to maintain consistency across different environments. Below, I'll guide you through creating a working Jekyll Docker environment using the official Jekyll Docker image.

Step-by-Step Guide

1. Create a Dockerfile

First, create a Dockerfile to define your Jekyll environment. Create a file named Dockerfile in your project directory with the following content:

# Use the official Jekyll image
FROM jekyll/jekyll:latest

# Set the working directory
WORKDIR /srv/jekyll

# Copy the Gemfile and Gemfile.lock
COPY Gemfile* ./

# Install dependencies
RUN bundle install

# Copy the Jekyll site
COPY . .

# Expose port 4000
EXPOSE 4000

# Build and serve the site
CMD ["jekyll", "serve", "--watch", "--force_polling", "--host", "0.0.0.0"]

2. Create a Docker Compose File

To make managing your Docker environment easier, you can use Docker Compose. Create a docker-compose.yml file in your project directory with the following content:

version: '3.8'

services:
  jekyll:
    build: .
    volumes:
      - .:/srv/jekyll
    ports:
      - "4000:4000"
    command: jekyll serve --watch --force_polling --host 0.0.0.0

3. Create a Gemfile

The Gemfile is used to specify the Ruby gems required for your Jekyll site. Create a Gemfile in your project directory with the following content:

source "https://rubygems.org"
gem "jekyll"
gem "webrick"  # Required for Jekyll 4.x

4. Create a Gemfile.lock

The Gemfile.lock file ensures that the exact versions of the gems used in your project are maintained. You can generate it by running:

docker run --rm -v "$(pwd)":/srv/jekyll -w /srv/jekyll jekyll/jekyll:latest bundle install

5. Build and Run Your Docker Container

Now you’re ready to build and run your Docker container. In your project directory, run:

docker-compose up

This command will:

  1. Build the Docker image using the Dockerfile.
  2. Start a container with Jekyll installed.
  3. Bind port 4000 on your local machine to port 4000 in the container.
  4. Serve your Jekyll site at http://localhost:4000.

6. Verify Your Setup

Open a web browser and navigate to http://localhost:4000. You should see your Jekyll site running.

Additional Tips

  • Volume Mounting: The volumes directive in the docker-compose.yml file mounts your local directory into the container, allowing you to make changes to your Jekyll site locally and see the updates in real-time.

  • Rebuilding the Container: If you make changes to the Dockerfile or Gemfile, you can rebuild the container using:

    docker-compose build
  • Stopping the Container: To stop the Jekyll server and the Docker container, use:

    docker-compose down
  • Debugging: If you encounter issues, you can get a shell into the running container with:

    docker-compose exec jekyll /bin/bash

This setup uses the official Jekyll Docker image, which ensures compatibility with the Jekyll version available on Docker Hub. If you need specific Jekyll versions or additional configuration, you can modify the Dockerfile accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment