From f95d800f6c6336386192bc7fd1c416072abe0913 Mon Sep 17 00:00:00 2001 From: giteaadmin Date: Sat, 23 Aug 2025 10:13:09 -0400 Subject: [PATCH] fix: update Dockerfile and docker-compose for improved development setup --- .gitignore | 3 ++- Dockerfile | 27 +++++++++++++++++---------- docker-compose.yml | 38 ++++++++++++++++++++++++++++++++++---- package-lock.json | 13 ------------- package.json | 3 ++- src/server.js | 3 +++ 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 72fdab9..bc2cdd3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules _site -.DS_Store \ No newline at end of file +.DS_Store +src/_data/views.json diff --git a/Dockerfile b/Dockerfile index a94b67d..0451828 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ -# STAGE 1: Build the Eleventy site -FROM node:18-alpine AS builder +# STAGE 1: Development +# 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 # Copy project files and install dependencies @@ -7,12 +9,15 @@ COPY package*.json ./ RUN npm install 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 RUN npx @11ty/eleventy -# STAGE 2: Setup the production server -FROM node:18-alpine +# STAGE 3: Production +# This stage creates a lean, production-ready image. +FROM node:18-alpine AS production WORKDIR /app # Copy server dependencies from the builder stage @@ -21,13 +26,15 @@ COPY --from=builder /app/package*.json ./ # Install only production dependencies for the server RUN npm install --omit=dev -# Copy the server file and the built Eleventy site -COPY --from=builder /app/server.js . +# Copy the server, the built Eleventy site, and the data file for the view counter. +# 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/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 8080 +EXPOSE 3000 # The command to start the server -CMD [ "npm", "start" ] +CMD [ "node", "server.js" ] diff --git a/docker-compose.yml b/docker-compose.yml index 8b10783..e3ef89d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: + # This service runs the Eleventy development server. eleventy: - build: . + build: + context: . + target: development # Use the 'development' stage from the Dockerfile container_name: eleventy_dev ports: - - 8080:8080 - - 9229:9229 + - "8080:8080" # Eleventy dev server port + 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: - .:/app - - /app/node_modules \ No newline at end of file + - /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 diff --git a/package-lock.json b/package-lock.json index c2779d3..e151a58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1041,19 +1041,6 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "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": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package.json b/package.json index a28060a..4e8b1ba 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@11ty/eleventy": "^2.0.1", "cors": "^2.8.5", "express": "^4.21.2", - "fs-extra": "^11.3.1" + "fs-extra": "^11.3.1", + "nodemon": "^3.1.0" } } diff --git a/src/server.js b/src/server.js index 118b481..5085f88 100644 --- a/src/server.js +++ b/src/server.js @@ -19,6 +19,9 @@ if (initialData.length === 0) { app.use(cors()); // Enable Cross-Origin Resource Sharing app.use(express.json()); +// In production, the server also serves the static site from the `_site` directory. +app.use(express.static('_site')); + // --- API Endpoints --- // GET: Fetch the view count for a specific post slug