Skip to content

Commit

Permalink
feat(docker): docker-compose for local development
Browse files Browse the repository at this point in the history
a docker compose file containing the app and a postgres database have been added to provide a local
dev stack to spin up. Therefore, the docker file multi stage build got updated. The prisma schema
got updated as well to be able to run with certain types of Linux builds and also be compatible with
Apple M1 systems
  • Loading branch information
noctua84 committed Jan 13, 2024
1 parent 2b0c4ca commit 62c488c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docker
.dockerignore
node_modules
npm-debug.log
dist
50 changes: 36 additions & 14 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
# Stage 1: Build the app
FROM node:18 AS builder
# Stage 1: development
FROM node:20 AS development

RUN apt-get update && apt-get install -y openssl

# create app directory
WORKDIR /app

# copy package.json and package-lock.json
COPY package*.json ./
COPY --chown=node:node package*.json ./

# install dependencies
RUN npm install
RUN npm ci

# copy source code
COPY . .
COPY --chown=node:node . .

# build the app
RUN npm run build
# Generate Prisma client
RUN npm run prisma:generate

# Use the node user instead of root
USER node

#------------------------------------------------------------
# Stage 2: Run the app
FROM node:18-slim
# Stage 2: build for production
FROM node:20-alpine AS builder

WORKDIR /app

# copy package.json and package-lock.json
COPY package*.json ./
COPY --chown=node:node package*.json ./

# The build process needs access to NestJS CLI, which is a dev dependency.
COPY --chown=node:node --from=development /app/node_modules ./node_modules

COPY --chown=node:node . .

RUN npm run build

ENV NODE_ENV=production

# Install production dependencies overriding the previously copied node_modules:
RUN npm ci --only=production && \
npm cache clean --force

USER node

RUN npm install --only=production
# -----------------------------------------------------------
# Stage 3: production
FROM node:20-alpine AS production

# copy build from stage 1
COPY --from=builder /app/dist ./dist
COPY --chown=node:node --from=builder /app/node_modules ./node_modules
COPY --chown=node:node --from=builder /app/dist ./dist

# expose port 3000
EXPOSE 3000
Expand Down
33 changes: 33 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
api:
build:
dockerfile: Dockerfile
context: .
target: development
volumes:
- .:/app
env_file:
- .env
environment:
BASE_URL: https://${HOSTNAME}:${PORT}
DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:${DB_PORT}/${DB_NAME}?schema=public
command: npm run start:dev
ports:
- 3000:3000
depends_on:
- postgres

postgres:
image: postgres:16
restart: always
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data

volumes:
postgres:
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["metrics", "postgresqlExtensions"]
// binary targets are optional, omitting them will default to ["native"]
// the targets next to native, are for alpine linux and arm64 (Apple M1) respectively
binaryTargets = ["native", "linux-musl", "linux-arm64-openssl-3.0.x"]
}

generator erd {
Expand Down

0 comments on commit 62c488c

Please sign in to comment.