Public Access
1
0

fix: update Dockerfile and docker-compose for improved development setup

This commit is contained in:
2025-08-23 10:13:09 -04:00
parent a002db6285
commit f95d800f6c
6 changed files with 58 additions and 29 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
node_modules node_modules
_site _site
.DS_Store .DS_Store
src/_data/views.json

View File

@@ -1,5 +1,7 @@
# STAGE 1: Build the Eleventy site # STAGE 1: Development
FROM node:18-alpine AS builder # This stage installs all dependencies (including devDependencies) and copies the source code.
# It's used as a base for the 'builder' stage and for the development environment via docker-compose.
FROM node:18-alpine AS development
WORKDIR /app WORKDIR /app
# Copy project files and install dependencies # Copy project files and install dependencies
@@ -7,12 +9,15 @@ COPY package*.json ./
RUN npm install RUN npm install
COPY . . COPY . .
# Build the Eleventy site # STAGE 2: Builder
# This stage builds the Eleventy site for production.
FROM development AS builder
# The output will be in the default `_site` directory # The output will be in the default `_site` directory
RUN npx @11ty/eleventy RUN npx @11ty/eleventy
# STAGE 2: Setup the production server # STAGE 3: Production
FROM node:18-alpine # This stage creates a lean, production-ready image.
FROM node:18-alpine AS production
WORKDIR /app WORKDIR /app
# Copy server dependencies from the builder stage # Copy server dependencies from the builder stage
@@ -21,13 +26,15 @@ COPY --from=builder /app/package*.json ./
# Install only production dependencies for the server # Install only production dependencies for the server
RUN npm install --omit=dev RUN npm install --omit=dev
# Copy the server file and the built Eleventy site # Copy the server, the built Eleventy site, and the data file for the view counter.
COPY --from=builder /app/server.js . # Note: The paths here are relative to the WORKDIR inside the 'builder' stage (/app).
COPY --from=builder /app/src/server.js .
COPY --from=builder /app/_site ./_site COPY --from=builder /app/_site ./_site
COPY --from=builder /app/data ./data # The server expects the _data directory in /app/_data, and your config places it in src/_data.
COPY --from=builder /app/src/_data ./_data
# Expose the port the server runs on # Expose the port the server runs on
EXPOSE 8080 EXPOSE 3000
# The command to start the server # The command to start the server
CMD [ "npm", "start" ] CMD [ "node", "server.js" ]

View File

@@ -1,10 +1,40 @@
# docker-compose.yml for development
# This setup runs two services:
# 1. The Eleventy dev server for live-reloading site changes.
# 2. The Express API server for the view counter.
#
# To start both, run: docker-compose up
#
# Your site will be available at http://localhost:8080
# Your API will be available at http://localhost:3000
services: services:
# This service runs the Eleventy development server.
eleventy: eleventy:
build: . build:
context: .
target: development # Use the 'development' stage from the Dockerfile
container_name: eleventy_dev container_name: eleventy_dev
ports: ports:
- 8080:8080 - "8080:8080" # Eleventy dev server port
- 9229:9229 volumes:
# Mount the project directory for live-reloading.
# The anonymous volume for node_modules prevents the local one from overwriting the container's.
- .:/app
- /app/node_modules
# This command runs Eleventy's dev server with file watching.
command: npm start
# This service runs your Express API server.
api:
build:
context: .
target: development # Use the same 'development' stage
container_name: api_dev
ports:
- "3000:3000" # Express API server port
- "9229:9229" # Node.js debugger port
volumes: volumes:
- .:/app - .:/app
- /app/node_modules - /app/node_modules
# Use nodemon to automatically restart the server on file changes.
command: npx nodemon --inspect=0.0.0.0:9229 src/server.js

13
package-lock.json generated
View File

@@ -1041,19 +1041,6 @@
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw=="
}, },
"node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/function-bind": { "node_modules/function-bind": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",

View File

@@ -13,6 +13,7 @@
"@11ty/eleventy": "^2.0.1", "@11ty/eleventy": "^2.0.1",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.21.2", "express": "^4.21.2",
"fs-extra": "^11.3.1" "fs-extra": "^11.3.1",
"nodemon": "^3.1.0"
} }
} }

View File

@@ -19,6 +19,9 @@ if (initialData.length === 0) {
app.use(cors()); // Enable Cross-Origin Resource Sharing app.use(cors()); // Enable Cross-Origin Resource Sharing
app.use(express.json()); app.use(express.json());
// In production, the server also serves the static site from the `_site` directory.
app.use(express.static('_site'));
// --- API Endpoints --- // --- API Endpoints ---
// GET: Fetch the view count for a specific post slug // GET: Fetch the view count for a specific post slug