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
_site
.DS_Store
src/_data/views.json

View File

@@ -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" ]

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:
# 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
# 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",
"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",

View File

@@ -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"
}
}

View File

@@ -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