From fc12d092efe1e752072ea7626829675276b282e6 Mon Sep 17 00:00:00 2001 From: Joel Fernandes <150249488+jfernsio@users.noreply.github.com> Date: Mon, 30 Mar 2026 09:49:43 +0000 Subject: [PATCH] feat: add Dockerfile for Node.js backend --- README.md | 21 +++++++++++++++++++++ backend/.dockerignore | 8 ++++++++ backend/Dockerfile | 17 +++++++++++++++-- backend/README.md | 14 ++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 backend/.dockerignore diff --git a/README.md b/README.md index 8322d11..5bc2014 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,27 @@ npm run dev:backend API: http://localhost:3001 (health: http://localhost:3001/health, v1: http://localhost:3001/api/v1). +#### Run backend with Docker + +```bash +# from repository root +docker build -f backend/Dockerfile -t trivela-backend . + +docker run --rm -p 3001:3001 \ + -e PORT=3001 \ + -e STELLAR_NETWORK=testnet \ + -e SOROBAN_RPC_URL=https://soroban-testnet.stellar.org \ + -e CORS_ALLOWED_ORIGINS=http://localhost:5173 \ + -e TRIVELA_API_KEY=dev-secret \ + trivela-backend +``` + +or using an env file: + +```bash +docker run --rm -p 3001:3001 --env-file backend/.env trivela-backend +``` + ### 4. Run frontend ```bash diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..487a36f --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,8 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore +.git +.gitignore +*.md +.vscode diff --git a/backend/Dockerfile b/backend/Dockerfile index d7b2647..bdd2ecf 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,11 +1,23 @@ -FROM node:20-alpine AS base +# Backend Dockerfile +# Build and run the Node.js Express API in a small, production-ready image +FROM node:20-alpine AS builder WORKDIR /app +# Copy package files and install dependencies (production only) COPY backend/package*.json ./ RUN npm ci --omit=dev -COPY backend/src ./src +# Copy source and non-code assets +COPY backend/ ./ + +# Optional step if your code pipeline has a build step +# RUN npm run build + +FROM node:20-alpine AS runtime +WORKDIR /app + +COPY --from=builder /app ./ ENV NODE_ENV=production ENV PORT=3001 @@ -13,3 +25,4 @@ ENV PORT=3001 EXPOSE 3001 CMD ["node", "src/server.js"] + diff --git a/backend/README.md b/backend/README.md index c6f07f5..59c3cf4 100644 --- a/backend/README.md +++ b/backend/README.md @@ -130,6 +130,12 @@ Build image from repo root: docker build -f backend/Dockerfile -t trivela-backend . ``` +Or build using backend as context (recommended for isolation): + +```bash +docker build -f backend/Dockerfile -t trivela-backend backend/ +``` + Run container (example): ```bash @@ -141,3 +147,11 @@ docker run --rm -p 3001:3001 \ -e TRIVELA_API_KEY=dev-secret \ trivela-backend ``` + +You can also use an env file: + +```bash +docker run --rm -p 3001:3001 --env-file backend/.env \ + trivela-backend +``` +