-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Dockerize backend #48
Open
gabrielmachin
wants to merge
2
commits into
main
Choose a base branch
from
chore/Dockerize-backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . . | ||
|
@@ -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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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` | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So changing a package means you will need to rebuild the image, right? and manually because docker won't notice that change, is this right?
Something like https://docs.docker.com/compose/file-watch/ would fix this and it may be "needed" (or a volume and check always update the packages), right? Otherwise changing a package will be a "painful" task making hard for someone to adopt this feature.