Adds Docker setup and basic API endpoints

WHAT: Introduces Dockerfiles for development and production, Docker Compose configurations, a Makefile for common tasks, shell scripts for environment setup/teardown, a basic Express API with SQLite integration.
WHY: Enables easy setup and deployment of the application using Docker. Provides basic API endpoints for managing purchase items.
HOW:
- Creates `docker-compose.dev.yml` and `docker-compose.prod.yml` to define services and volumes.
- Introduces `Dockerfile.dev` and `Dockerfile.prod` to build container images with necessary dependencies.
- Adds `Makefile` with commands for building, running, and managing the application.
- Implements shell scripts for simplified Docker environment management.
- Sets up Express API with endpoints for CRUD operations on purchase items, using SQLite as the database.
- Uses `better-sqlite3` to connect and interact with the SQLite database.
This commit is contained in:
Chenwei Jiang 2025-08-21 14:46:11 +08:00
parent 3b638a9509
commit 4557728932
Signed by: cheverjohn
GPG key ID: ADC4815BFE960182
20 changed files with 1955 additions and 20 deletions

21
docker/Dockerfile.dev Normal file
View file

@ -0,0 +1,21 @@
FROM node:20-bullseye-slim
ENV npm_config_registry=https://registry.npmmirror.com
ENV npm_config_build_from_source=true
# Build prerequisites for native modules (better-sqlite3)
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Use host-built frontend; only run server here
COPY server/package.json server/tsconfig.json ./server/
COPY server/src ./server/src
RUN cd server && npm install --build-from-source && npm run build
EXPOSE 8080
CMD ["node", "server/dist/index.js"]

26
docker/Dockerfile.prod Normal file
View file

@ -0,0 +1,26 @@
FROM node:20-bullseye-slim
ENV npm_config_registry=https://registry.npmmirror.com
ENV npm_config_build_from_source=true
# Build prerequisites for native modules (better-sqlite3)
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Install server deps in container (native modules match linux/amd64)
COPY server/package.json ./server/package.json
RUN cd server && npm install --omit=dev --build-from-source
# Copy server runtime and prebuilt frontend from host
COPY server/dist ./server/dist
COPY dist ./frontend
ENV PORT=8080
ENV DB_PATH=/data/purchase.db
EXPOSE 8080
CMD ["node", "server/dist/index.js"]

View file

@ -0,0 +1,20 @@
services:
api:
build:
context: ..
dockerfile: docker/Dockerfile.dev
image: dm-purchase-api:dev
platform: ${DOCKER_PLATFORM:-linux/amd64}
ports:
- "8080:8080"
environment:
- DB_PATH=/data/purchase.db
volumes:
- dm_db:/data
- ../dist:/app/frontend:ro
restart: unless-stopped
volumes:
dm_db:

View file

@ -0,0 +1,19 @@
services:
api:
build:
context: ..
dockerfile: docker/Dockerfile.prod
image: dm-purchase-api:prod
platform: linux/amd64
ports:
- "8080:8080"
environment:
- DB_PATH=/data/purchase.db
volumes:
- dm_db:/data
restart: unless-stopped
volumes:
dm_db: