Skip to content

Commit

Permalink
chore: Dockerize backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmachin committed Jan 12, 2024
1 parent b9b87d9 commit 07cd725
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 41 deletions.
23 changes: 12 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Start with fully-featured Node.js base image
FROM node:20.9.0 AS builder
RUN npm i -g [email protected]
WORKDIR /home/node/app

# Copy dependency information and install production dependencies
COPY *.json ./

RUN npm install --production --pure-lockfile --ignore-scripts
RUN cp -RL node_modules/ /tmp/node_modules/

# Install all dependencies
RUN npm install --pure-lockfile --ignore-scripts
RUN npm ci --ignore-scripts

# Copy source code (and all other relevant files)
COPY . .
Expand All @@ -21,21 +19,24 @@ RUN npm run build

# Run-time stage
FROM node:20.9.0-alpine
RUN npm i -g [email protected]

WORKDIR /home/node/app

# Copy runtime dependencies
COPY --from=builder /tmp/node_modules/ ./node_modules/
COPY --from=builder /home/node/app/node_modules/@prisma ./node_modules/@prisma/
COPY --from=builder /home/node/app/node_modules/.prisma ./node_modules/.prisma/
COPY --from=builder /home/node/app/node_modules/.bin ./node_modules/.bin/
COPY --from=builder /home/node/app/prisma ./prisma

# Copy runtime project
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/src/emails/*.pug ./dist/src/emails/
COPY --from=builder /home/node/app/package*.json ./
COPY --from=builder /home/node/app/tsconfig.json ./

# Install runtime dependencies
RUN npm ci --omit=dev --ignore-scripts

COPY --from=builder /home/node/app/node_modules/@prisma ./node_modules/@prisma/
COPY --from=builder /home/node/app/node_modules/.prisma ./node_modules/.prisma/
COPY --from=builder /home/node/app/node_modules/.bin ./node_modules/.bin/
COPY --from=builder /home/node/app/prisma ./prisma

EXPOSE 8080

CMD [ "npm", "run", "deploy" ]
11 changes: 11 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Start with fully-featured Node.js base image
FROM node:20.9.0 AS builder
RUN npm i -g [email protected]
WORKDIR /home/node/app

# Copy dependency information and install dependencies
COPY *.json ./

RUN npm install --ignore-scripts

CMD [ "npm", "run", "docker-dev-cmd" ]
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ npm test

## Project Setup

There are currently two ways in which we can make the setup of the project, one would be running the whole setup with docker, and the other running everything but the backend project with docker.

### Running the whole setup with docker
You only need to install [Docker](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/). To start the containers, run `docker compose up`, and the `-d` flag if you want to run it in detached mode.
Once the containers are running, you can go to `http://localhost:8080/docs` to see the API documentation.
If you want to interact with the project container terminal you can run the command `docker compose exec backend bash` and then run e.g `npm run test`


### Running only the db and Redis with docker
* Install Node 20.9.0 (as documented on [`.node-version`](./.node-version))
* Install the appropriate version of npm: `npm i -g [email protected]`
* Install packages with `npm install`
* Create a new `.env` file using the `.env.example` as an example. for that run `$ cp .env.example .env`.
* Set the variables in the new environment file `.env` you created above.
* Start the redis container with `docker-compose up redis-server`
* Start the db and redis container with `docker-compose up db redis-server`
* Start the project with `npm start`

## Project structure
Expand All @@ -65,7 +74,7 @@ npm test
[Prisma](https://www.prisma.io/docs/concepts/overview/what-is-prisma) is a next-generation ORM for Node.js.
`prisma client` - Auto-generated and type-safe query builder for Node.js & TypeScript.
`prisma migrate` - Prisma migration system.
Requeriments:
Requirements:
* set a `DATABASE_URL` on .env - The format is specified in .env.example
* Running database: There's a docker-compose file example: `docker-compose up -d`
* Run database migrations: `npx prisma migrate dev`
Expand All @@ -90,7 +99,7 @@ TSOA uses decorators to define the API routes and docs. check out the [TSOA docs

## Docker Configuration
A [`Dockerfile`](./Dockerfile) has been added to this project. The main reason to use Docker is to generate a production build, it is not intended for use for development.
In fact, the Dockerfile has instructions only for generating the production-ready build. This is a multi-stage build that will build the project, run the migrations, and then run the server only with production dependiencies.
In fact, the Dockerfile has instructions only for generating the production-ready build. This is a multi-stage build that will build the project, run the migrations, and then run the server only with production dependencies.

### BullMQ worker
* To add a new worker we just need to create a `new Worker()` object in the worker folder and pass a queue name to pick up tasks from.
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml → docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#This is for dev only

version: '3.8'
services:
db:
Expand Down Expand Up @@ -30,6 +32,18 @@ services:
POSTGRES_DB: 'nodeTemplateDBTesting'
ports:
- '5433:5432'
backend:
build:
dockerfile: Dockerfile.dev
depends_on:
- db
- redis-server
volumes:
- .:/home/node/app
ports:
- '8080:8080'
stdin_open: true
tty: true
volumes:
db:
redis-data:
34 changes: 8 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"deploy": "prisma migrate deploy && concurrently \"npm run deploy-server\" \"npm run deploy-worker\"",
"deploy-server": "TS_NODE_BASEURL=./dist/src node -r dotenv/config -r tsconfig-paths/register dist/src/index.js",
"deploy-worker": "TS_NODE_BASEURL=./dist/src node -r dotenv/config -r tsconfig-paths/register dist/worker/worker.js",
"format": "prettier --write ."
"format": "prettier --write .",
"docker-dev-cmd": "prisma migrate deploy && prisma generate && npm start"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"routes/*": ["routes/*"],
"services/*": ["services/*"],
"tests/*": ["tests/*"],
"queue/*": ["queue/*"],
"types/*": ["types/*"],
"utils/*": ["utils/*"],
"root/*": ["../*"]
Expand Down
1 change: 1 addition & 0 deletions tsoa.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"routes/*": ["routes/*"],
"services/*": ["services/*"],
"tests/*": ["tests/*"],
"queue/*": ["queue/*"],
"types/*": ["types/*"],
"utils/*": ["utils/*"],
"root/*": ["../*"]
Expand Down

0 comments on commit 07cd725

Please sign in to comment.