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.
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"]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.0The 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.xThe 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 installNow you’re ready to build and run your Docker container. In your project directory, run:
docker-compose upThis command will:
- Build the Docker image using the
Dockerfile. - Start a container with Jekyll installed.
- Bind port 4000 on your local machine to port 4000 in the container.
- Serve your Jekyll site at
http://localhost:4000.
Open a web browser and navigate to http://localhost:4000. You should see your Jekyll site running.
-
Volume Mounting: The
volumesdirective in thedocker-compose.ymlfile 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
DockerfileorGemfile, 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.