|
1 | 1 | # ===================================================================================== |
2 | 2 | # STAGE 1: The "Builder" |
3 | | -# This stage uses Node.js to download the source files and build the static site. |
| 3 | +# This stage uses Node.js and your local source files to build the static site. |
4 | 4 | # ===================================================================================== |
5 | 5 | FROM node:18-alpine AS builder |
6 | 6 |
|
7 | | -# Install git, which is required by some tools. |
8 | | -RUN apk add --no-cache git |
9 | | - |
10 | | -# Install the necessary command-line tools globally. |
11 | | -# gitbook-exporter: Downloads the Markdown source from a live GitBook site. |
12 | | -# gitbook-cli: The official tool to build a static site from Markdown source. |
13 | | -RUN npm install -g gitbook-exporter gitbook-cli |
| 7 | +# Install gitbook-cli, the official tool to build a static site from Markdown source. |
| 8 | +RUN npm install -g gitbook-cli |
14 | 9 |
|
15 | 10 | # Set a working directory inside the container. |
16 | 11 | WORKDIR /app |
17 | 12 |
|
18 | | -# Run the exporter to download the source files from the live GitBook site. |
19 | | -# This creates a directory named after the book, e.g., "nebula". |
20 | | -RUN gitbook-exporter https://nebulaclient.gitbook.io/nebula/ |
21 | | - |
22 | | -# Navigate into the newly created source directory. |
23 | | -# The directory name is based on the book's title, so we use a wildcard. |
24 | | -WORKDIR /app/nebula |
| 13 | +# Copy ALL your local files (Markdown, book.json, images, etc.) into the container. |
| 14 | +# This assumes the Dockerfile is at the root of your documentation source. |
| 15 | +COPY . . |
25 | 16 |
|
26 | | -# Install the required plugins for the book (defined in its book.json). |
| 17 | +# Run "gitbook install" to download any plugins defined in your book.json. |
27 | 18 | RUN gitbook install |
28 | 19 |
|
29 | | -# Build the final, static HTML site. |
30 | | -# This creates a "_book" directory containing all the files. |
| 20 | +# Run "gitbook build" to generate the final static HTML site. |
| 21 | +# This creates a "_book" directory containing all the files needed for hosting. |
31 | 22 | RUN gitbook build |
32 | 23 |
|
33 | 24 | # ===================================================================================== |
34 | 25 | # STAGE 2: The "Runner" |
35 | | -# This stage uses a minimal NGINX server to host the static files. |
| 26 | +# This stage uses a minimal NGINX server to host the static files. It is the final image. |
36 | 27 | # ===================================================================================== |
37 | 28 | FROM nginx:1.25-alpine |
38 | 29 |
|
39 | | -# Set the port you want to expose. |
| 30 | +# Set the port to be used inside the container. |
40 | 31 | ARG PORT=3587 |
41 | 32 | EXPOSE ${PORT} |
42 | 33 |
|
43 | | -# Remove the default NGINX configuration file. |
| 34 | +# Remove the default NGINX configuration to avoid conflicts. |
44 | 35 | RUN rm /etc/nginx/conf.d/default.conf |
45 | 36 |
|
46 | | -# Copy the static HTML files generated in the "builder" stage |
47 | | -# into the default NGINX web root directory. |
48 | | -COPY --from=builder /app/nebula/_book /usr/share/nginx/html |
| 37 | +# Copy ONLY the built static HTML files from the "builder" stage |
| 38 | +# into the NGINX web root directory. This keeps the final image small. |
| 39 | +COPY --from=builder /app/_book /usr/share/nginx/html |
49 | 40 |
|
50 | | -# Copy our custom NGINX configuration. |
51 | | -# This config tells NGINX to listen on our chosen port and serve the static files. |
| 41 | +# Create our simple NGINX configuration to serve the static site. |
| 42 | +# This is placed directly into the configuration directory. |
52 | 43 | COPY <<EOF /etc/nginx/conf.d/gitbook.conf |
53 | 44 | server { |
| 45 | + # Listen on the port specified during the build. |
54 | 46 | listen ${PORT}; |
55 | 47 | server_name _; |
56 | 48 |
|
57 | 49 | # The root directory where our static files are located. |
58 | 50 | root /usr/share/nginx/html; |
59 | 51 | index index.html index.htm; |
60 | 52 |
|
61 | | - # Standard configuration for serving static files. |
62 | | - # It tries to find a file matching the URI, then a directory, |
63 | | - # and finally falls back to the index.html for clean URLs. |
| 53 | + # This is the crucial part for GitBook's "clean URLs". |
| 54 | + # It allows you to visit /my-page instead of /my-page.html. |
64 | 55 | location / { |
65 | 56 | try_files \$uri \$uri/ /index.html; |
66 | 57 | } |
67 | 58 |
|
68 | | - # Optional: Add headers for security and caching. |
| 59 | + # Standard security headers. |
69 | 60 | add_header X-Frame-Options "SAMEORIGIN"; |
70 | 61 | add_header X-Content-Type-Options "nosniff"; |
71 | 62 | add_header X-XSS-Protection "1; mode=block"; |
72 | 63 | } |
73 | 64 | EOF |
74 | 65 |
|
75 | | -# Command to run NGINX in the foreground. |
| 66 | +# The command to start the NGINX server when the container runs. |
76 | 67 | CMD ["nginx", "-g", "daemon off;"] |
0 commit comments