Hikaru's notes

A collection of notes and resources

View the Project on GitHub HikaruMizoe/notes

Docker を使ったローカル検証手順

この手順はローカル環境を汚さずに、Docker コンテナ上で Jekyll ビルドと生成物の確認を行うための手順です。


1. リポジトリを作業ディレクトリにする

cd /path/to/your/notes
# 例: cd ~/repos/notes

2. Jekyll イメージでサイトをビルド(生成物を _site に出力)

# Jekyll イメージを使ってビルド
docker run --rm --platform=linux/amd64 -v "$PWD":/srv/jekyll -w /srv/jekyll jekyll/jekyll:4 sh -lc 'bundle install && bundle exec jekyll build'

3a. Python 簡易サーバーで _site を配信(素早く確認する場合)

docker run --rm --platform=linux/amd64 -v "$PWD/_site":/srv/site -p 4000:8000 python:3.11-slim \
  python3 -m http.server 8000 --directory /srv/site

ブラウザで http://localhost:4000 を開きます。

3b. nginx で本番寄りに配信する場合

docker run --rm --platform=linux/amd64 -v "$PWD/_site":/usr/share/nginx/html:ro -p 4000:80 nginx:alpine

ブラウザで http://localhost:4000 を開きます。

4. 自動チェック(簡易スモークテスト)

python3 - <<'PY'
import urllib.request

with urllib.request.urlopen('http://localhost:4000/') as response:
  print(response.status)
PY
python3 - <<'PY'
import urllib.request

with urllib.request.urlopen('http://localhost:4000/README/') as response:
  print(response.status)
PY

(パスはサイトの実際のURLに合わせて調整してください)

5. よくあるトラブルと対処

6. Docker Compose で簡易化(任意)

docker-compose.yml を作る例:

version: "3.8"
services:
  jekyll:
    image: jekyll/jekyll:4
    platform: linux/amd64
    volumes:
      - ./:/srv/jekyll
    command: jekyll build
  serve:
    image: nginx:alpine
    platform: linux/amd64
    depends_on:
      - jekyll
    volumes:
      - ./_site:/usr/share/nginx/html:ro
    ports:
      - "4000:80"

使い方:

docker compose run --rm jekyll
docker compose up --remove-orphans

必要なら、この手順書をワークフローに組み込むための Makefile や github actions 用のステップ例も追加します。どちらを希望しますか?