Skip to content

Commit add367b

Browse files
authored
Fix running docs site with docker (kubernetes-sigs#5512)
* Update docs site local build Clean up makefile Ignore container-image.sentinel Fix hugo server errors Ignore files Add makefile credit * Indentation per feedback * Address PR feedback. Remove sentinel file * Remove change to .gitignore
1 parent b1b61ad commit add367b

File tree

6 files changed

+264
-64
lines changed

6 files changed

+264
-64
lines changed

site/Dockerfile

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1-
FROM klakegg/hugo:ext-alpine
1+
# Credit to Julien Guyomard (https://github.com/jguyomard)
2+
# Credit to the Kubernetes Website team.
3+
# This Dockerfile is based on:
4+
# (https://github.com/kubernetes/website/blob/main/Dockerfile)
25

3-
RUN apk add git
6+
FROM docker.io/library/golang:1.20-alpine
7+
8+
RUN apk add --no-cache \
9+
curl \
10+
gcc \
11+
g++ \
12+
musl-dev \
13+
build-base \
14+
libc6-compat
15+
16+
ARG HUGO_VERSION
17+
18+
RUN mkdir $HOME/src && \
19+
cd $HOME/src && \
20+
curl -L https://github.com/gohugoio/hugo/archive/refs/tags/v${HUGO_VERSION}.tar.gz | tar -xz && \
21+
cd "hugo-${HUGO_VERSION}" && \
22+
go install --tags extended
23+
24+
FROM docker.io/library/golang:1.20-alpine
25+
26+
RUN apk add --no-cache \
27+
runuser \
28+
git \
29+
openssh-client \
30+
rsync \
31+
npm && \
32+
npm install -D autoprefixer postcss-cli
33+
34+
RUN mkdir -p /var/hugo && \
35+
addgroup -Sg 1000 hugo && \
36+
adduser -Sg hugo -u 1000 -h /var/hugo hugo && \
37+
chown -R hugo: /var/hugo && \
38+
runuser -u hugo -- git config --global --add safe.directory /src
39+
40+
COPY --from=0 /go/bin/hugo /usr/local/bin/hugo
41+
42+
WORKDIR /src
43+
44+
USER hugo:hugo
45+
46+
EXPOSE 1313

site/Makefile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Credit to the Kubernetes Website team. (https://github.com/kubernetes/website/blob/main/Makefile)
2+
HUGO_VERSION = $(shell grep ^HUGO_VERSION netlify.toml | tail -n 1 | cut -d '=' -f 2 | tr -d " \"\n")
3+
4+
# The CONTAINER_ENGINE variable is used for specifying the container engine. By default 'docker' is used
5+
# but this can be overridden when calling make, e.g.
6+
# CONTAINER_ENGINE=podman make container-image
7+
CONTAINER_ENGINE ?= docker
8+
IMAGE_REGISTRY ?= registry.local:5000
9+
IMAGE_VERSION=$(shell scripts/hash-files.sh Dockerfile Makefile | cut -c 1-12)
10+
CONTAINER_IMAGE = $(IMAGE_REGISTRY)/kustomize-website-hugo:v$(HUGO_VERSION)-$(IMAGE_VERSION)
11+
# Mount read-only to allow use with tools like Podman in SELinux mode
12+
# Container targets don't need to write into /src
13+
CONTAINER_RUN = "$(CONTAINER_ENGINE)" run --rm --interactive --tty --volume "$(abspath $(CURDIR)/..):/src:ro,Z"
14+
15+
CCRED=\033[0;31m
16+
CCEND=\033[0m
17+
18+
.PHONY: help
19+
help: ## Show this help.
20+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
21+
22+
.PHONY: module-check
23+
module-check: ## Check if all of the required submodules are correctly initialized.
24+
@git submodule status --recursive | awk '/^[+-]/ {err = 1; printf "\033[31mWARNING\033[0m Submodule not initialized: \033[34m%s\033[0m\n",$$2} END { if (err != 0) print "You need to run \033[32mmake module-init\033[0m to initialize missing modules first"; exit err }' 1>&2
25+
26+
.PHONY: module-init
27+
module-init: ## Initialize required submodules.
28+
@echo "Initializing submodules..." 1>&2
29+
@git submodule update --init --recursive --depth 1
30+
31+
.PHONY: all
32+
all: build ## Build site with production settings and put deliverables in ./public
33+
34+
.PHONY: build
35+
build: module-check ## Build site with non-production settings and put deliverables in ./public
36+
hugo --cleanDestinationDir --minify --environment development
37+
38+
.PHONY: build-preview
39+
build-preview: module-check ## Build site with drafts and future posts enabled
40+
hugo --cleanDestinationDir --buildDrafts --buildFuture --environment preview
41+
42+
.PHONY: serve
43+
serve: module-check ## Boot the development server.
44+
hugo server --buildFuture --environment development
45+
46+
## Build a container image for the preview of the website
47+
.PHONY: container-image
48+
container-image: netlify.toml Dockerfile hugo.toml
49+
$(CONTAINER_ENGINE) build . \
50+
--network=host \
51+
--tag $(CONTAINER_IMAGE) \
52+
--build-arg HUGO_VERSION=$(HUGO_VERSION)
53+
54+
# no build lock to allow for read-only mounts
55+
## Boot the development server using container.
56+
.PHONY: container-serve
57+
container-serve: module-check
58+
$(CONTAINER_RUN) \
59+
--cap-drop=ALL \
60+
--cap-add=AUDIT_WRITE \
61+
--read-only \
62+
--mount type=tmpfs,destination=/tmp,tmpfs-mode=01777 \
63+
-p 1313:1313 $(CONTAINER_IMAGE) \
64+
hugo server \
65+
--source site \
66+
--buildFuture \
67+
--environment development \
68+
--bind 0.0.0.0 \
69+
--destination /tmp/hugo \
70+
--cleanDestinationDir \
71+
--noBuildLock

site/README.md

Lines changed: 106 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,114 @@ I put the most effort into the `Documentation` section. The left-menu bar has th
99

1010
The top bar is customized with the sections I think make sense to split. However, I have customized nothing else inside the `Community`, `Contribute` and `Blog` sections.
1111

12-
## Building
12+
## Running the website locally
13+
You can run the website locally using [Hugo (Extended version)](https://gohugo.io/), or you can run it in a container runtime. We strongly recommend using the container runtime, as it gives deployment consistency with the live website.
1314

14-
Build and run using Docker or Hugo, then access the site at `http://localhost:1313`.
15+
## Prerequisites
16+
17+
To use this repository, you need the following installed locally:
18+
19+
- [npm](https://www.npmjs.com/)
20+
- [Go](https://go.dev/)
21+
- [Hugo (Extended version)](https://gohugo.io/)
22+
- A container runtime, like [Docker](https://www.docker.com/).
23+
24+
Before you start, install the dependencies. Clone the repository and navigate to the site directory:
25+
26+
```bash
27+
# Clone your repository fork from the previous step
28+
git clone --recurse-submodules [email protected]:<your github username>/kustomize.git
29+
cd kustomize/site
30+
```
31+
32+
The Kustomize website uses the [Docsy Hugo theme](https://github.com/google/docsy#readme). Even if you plan to run the website in a container, we strongly recommend pulling in the submodule and other development dependencies by running the following:
33+
34+
### Windows
35+
```powershell
36+
# fetch submodule dependencies
37+
git submodule update --init --recursive --depth 1
38+
```
39+
40+
### Linux / other Unix
41+
```bash
42+
# fetch submodule dependencies
43+
make module-init
44+
```
45+
46+
## Running the website using a container
47+
48+
To build the site in a container, run the following:
49+
50+
```bash
51+
# You can set $CONTAINER_ENGINE to the name of any Docker-like container tool
52+
# Build the image
53+
make container-image
54+
55+
# Run the container
56+
make container-serve
57+
```
58+
59+
If you see errors, it probably means that the hugo container did not have enough computing resources available. To solve it, increase the amount of allowed CPU and memory usage for Docker on your machine ([MacOS](https://docs.docker.com/desktop/settings/mac/) and [Windows](https://docs.docker.com/desktop/settings/windows/)).
60+
61+
Open up your browser to <http://localhost:1313> to view the website. As you make changes to the source files, Hugo updates the website and forces a browser refresh.
62+
63+
## Running the website locally using Hugo
64+
65+
Make sure to install the Hugo extended version specified by the `HUGO_VERSION` environment variable in the [`netlify.toml`](netlify.toml#L7) file.
66+
67+
To install dependencies, deploy and test the site locally, run:
68+
69+
- For macOS and Linux
70+
```bash
71+
npm ci
72+
make serve
73+
```
74+
- For Windows (PowerShell)
75+
```powershell
76+
npm ci
77+
hugo.exe server --buildFuture --environment development
78+
```
79+
80+
This will start the local Hugo server on port 1313. Open up your browser to <http://localhost:1313> to view the website. As you make changes to the source files, Hugo updates the website and forces a browser refresh.
81+
82+
## Troubleshooting
83+
84+
### error: failed to transform resource: TOCSS: failed to transform "scss/main.scss" (text/x-scss): this feature is not available in your current Hugo version
85+
86+
Hugo is shipped in two set of binaries for technical reasons. The current website runs based on the **Hugo Extended** version only. In the [release page](https://github.com/gohugoio/hugo/releases) look for archives with `extended` in the name. To confirm, run `hugo version` and look for the word `extended`.
87+
88+
### Troubleshooting macOS for too many open files
89+
90+
If you run `make serve` on macOS and receive the following error:
1591

16-
### Docker
17-
Dependencies:
18-
* [docker](https://docs.docker.com/engine/install/)
19-
* [docker-compose](https://docs.docker.com/compose/install/)
2092
```bash
21-
docker-compose build
22-
docker-compomse up -d
93+
ERROR 2020/08/01 19:09:18 Error: listen tcp 127.0.0.1:1313: socket: too many open files
94+
make: *** [serve] Error 1
95+
```
96+
97+
Try checking the current limit for open files:
98+
99+
`launchctl limit maxfiles`
100+
101+
Then run the following commands (adapted from <https://gist.github.com/tombigel/d503800a282fcadbee14b537735d202c>):
102+
103+
```shell
104+
#!/bin/sh
105+
106+
# These are the original gist links, linking to my gists now.
107+
# curl -O https://gist.githubusercontent.com/a2ikm/761c2ab02b7b3935679e55af5d81786a/raw/ab644cb92f216c019a2f032bbf25e258b01d87f9/limit.maxfiles.plist
108+
# curl -O https://gist.githubusercontent.com/a2ikm/761c2ab02b7b3935679e55af5d81786a/raw/ab644cb92f216c019a2f032bbf25e258b01d87f9/limit.maxproc.plist
109+
110+
curl -O https://gist.githubusercontent.com/tombigel/d503800a282fcadbee14b537735d202c/raw/ed73cacf82906fdde59976a0c8248cce8b44f906/limit.maxfiles.plist
111+
curl -O https://gist.githubusercontent.com/tombigel/d503800a282fcadbee14b537735d202c/raw/ed73cacf82906fdde59976a0c8248cce8b44f906/limit.maxproc.plist
112+
113+
sudo mv limit.maxfiles.plist /Library/LaunchDaemons
114+
sudo mv limit.maxproc.plist /Library/LaunchDaemons
115+
116+
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
117+
sudo chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
118+
119+
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
23120
```
24121

25-
### hugo
26-
1. Building using the `hugo` command requires the following dependencies:
27-
* [hugo CLI](https://gohugo.io/getting-started/installing/)
28-
* [Go](https://go.dev/learn/)
29-
* [Node.js](https://nodejs.org/en/)
30-
* npm dependencies
31-
```bash
32-
npm install -D autoprefixer
33-
npm install -D postcss-cli
34-
npm install -D postcss
35-
```
36-
1. Initialize [Docsy](https://www.docsy.dev/docs/) and nested [submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules)
37-
```bash
38-
# In Kustomize repository root directory, fetch docsy submodule at site/themes/docsy.
39-
# See alternative submodule cloning options in the submodule documentation linked above.
40-
git submodule init
41-
git submodule update
42-
# Fetch submodules nested in docsy.
43-
cd site/themes/docsy
44-
git submodule init
45-
git submodule update
46-
```
47-
1. Start in development mode:
48-
```bash
49-
hugo serve -D
50-
```
122+
This works for Catalina as well as Mojave macOS.

site/config.toml renamed to site/hugo.toml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ theme = ["docsy"]
2020
enableGitInfo = true
2121

2222
# Comment out to enable taxonomies in Docsy
23-
# disableKinds = ["taxonomy", "taxonomyTerm"]
23+
disableKinds = ["taxonomy"]
24+
25+
ignoreFiles = [ "README[-]+[a-z]*\\.md", "^node_modules$" ]
2426

2527
# Highlighting config
2628
pygmentsCodeFences = true
@@ -58,26 +60,29 @@ id = "UA-00000000-0"
5860
[languages]
5961
[languages.en]
6062
title = "Kustomize"
61-
description = "Documentation for Kustomize"
6263
languageName ="English"
6364
# Weight used for sorting.
6465
weight = 1
65-
66-
# [languages.no]
67-
# title = "Kustomize"
68-
# description = "Docsy er operativsystem for skyen"
69-
# languageName ="Norsk"
70-
# contentDir = "content/no"
71-
# time_format_default = "02.01.2006"
72-
# time_format_blog = "02.01.2006"
73-
74-
# [languages.fa]
75-
# title = "اسناد گلدی"
76-
# description = "یک نمونه برای پوسته داکسی"
77-
# languageName ="فارسی"
78-
# contentDir = "content/fa"
79-
# time_format_default = "2006.01.02"
80-
# time_format_blog = "2006.01.02"
66+
languagedirection = "ltr"
67+
i18nDir = "./data/i18n"
68+
69+
70+
[caches]
71+
[caches.assets]
72+
dir = ":cacheDir/_gen"
73+
maxAge = -1
74+
[caches.getcsv]
75+
dir = ":cacheDir/:project"
76+
maxAge = "60s"
77+
[caches.getjson]
78+
dir = ":cacheDir/:project"
79+
maxAge = "60s"
80+
[caches.images]
81+
dir = ":cacheDir/_images"
82+
maxAge = -1
83+
[caches.modules]
84+
dir = ":cacheDir/modules"
85+
maxAge = -1
8186

8287
[markup]
8388
[markup.goldmark]

site/netlify.toml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
[build]
2-
base = "site/"
3-
command = "git submodule update --init --recursive --depth 1 && hugo"
4-
publish = "publishedSite/"
2+
base = "site/"
3+
command = "git submodule update --init --recursive --depth 1 && hugo"
4+
publish = "publishedSite/"
55

66
[build.environment]
7-
HUGO_VERSION = "0.92.2"
8-
NODE_ENV = "development"
9-
NETLIFY_BUILD_DEBUG = "true"
7+
HUGO_VERSION = "0.120.3"
8+
NODE_ENV = "development"
9+
NETLIFY_BUILD_DEBUG = "true"
1010

1111
[context.deploy-preview]
12-
command = "git submodule update --init --recursive --depth 1 && hugo --enableGitInfo --buildFuture -b $DEPLOY_PRIME_URL"
12+
command = "git submodule update --init --recursive --depth 1 && hugo --enableGitInfo --buildFuture -b $DEPLOY_PRIME_URL"
1313

1414
[context.branch-deploy]
15-
ignore = "exit 0" # build PRs (deploy preview env) only, not all branches
15+
ignore = "exit 0" # build PRs (deploy preview env) only, not all branches
1616

1717
[context.production]
18-
ignore = "exit 0" # never deploy production until we're ready to launch
19-
18+
ignore = "exit 0" # never deploy production until we're ready to launch

site/scripts/hash-files.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
# this script emits as hash for the files listed in $@
3+
if command -v shasum >/dev/null 2>&1; then
4+
cat "$@" | shasum -a 256 | cut -d' ' -f1
5+
elif command -v sha256sum >/dev/null 2>&1; then
6+
cat "$@" | sha256sum | cut -d' ' -f1
7+
else
8+
echo "missing shasum tool" 1>&2
9+
exit 1
10+
fi

0 commit comments

Comments
 (0)