Cookie Consent by Free Privacy Policy Generator 📌 How to deploy a nestjs back-end from a mono repo on fly.io


✅ How to deploy a nestjs back-end from a mono repo on fly.io


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

I recently had to deploy an API backend from a monorepo on fly.io and this is a documentation of the steps.
The project uses turborepo with npm managed workspace. The monorepo had the below structure
Monorepo Structure

Fly.io has a generous free tier that is perfect for small projects like MVPs. New users get a $5 credit which gives up to 3 shared-cpu-1x 256mb VMs and 3GB persistent volume storage. This served my purposes especially during development. Let's dive in.

Step 1: Create an Account on fly.io & Install Fly CLI

To begin visit fly.io to create an account. Next install flyctl a command line tool for creating and deploying fly apps.
macOS

brew install flyctl

Windows

pwsh -Command "iwr https://fly.io/install.ps1 -useb | iex"

You may have to run the command on windows with powershell instead of pwsh if you run into errors. Full instructions can be found here. After installation authenticate the CLI using:

fly auth login

Step 2: Create A Dockerfile

Flyctl automatically detects a Dockerfile in the app root directory, It builds an image from it and deploys it.
Create a Dockerfile in the nestjs app's root directory and paste in the following content.

# Line 1
FROM node:20-alpine as builder
# Line 2
ENV NODE_ENV build
# Line 3
WORKDIR /api
# Line 4
COPY package*.json ./
# Line 5
RUN npm install
# Line 6
COPY . .
# Line 7
RUN npm run build \
    && npm prune --production

# Line 8
FROM node:20-alpine
# Line 9
ENV NODE_ENV production
# Line 10
WORKDIR /api
# Line 11
COPY --from=builder  /api/package*.json ./
COPY --from=builder  /api/node_modules/ ./node_modules/
COPY --from=builder  /api/dist/ ./dist/
# Line 12
CMD ["node", "dist/src/main.js"]

This dockerfile uses a multi stage build, which is a docker build process that lets you build an image in different steps. Each build stage has output(files) from which one can copy only what is needed in the next stage, this removes unnecessary files like development dependencies from the final output which helps to reduce the overall size of the resulting image.

In the builder stage(Line 1), from Lines 4-7, we copy package.json file to the image, run the install script, copy over the generated files and build the nest app.

In the next stage of the build beginning in line 8, On Line 11 we copy over the only files we need(package.json and lock files, node modules folder and the dist folder) and on line 12 we finally specify the start command.

Step 3: Create & Configure a fly app

In this step we create and configure a fly app from our nestjs application. Run the command below from inside your project source directory to create, configure, and (for most apps) deploy a new fly application. In my case the root was monorepo_root/apps/api.

fly launch

This command generates a fly.toml file with default configuration that can be edited using a web interface.

Note: Fly expects the app to listen on port 8080 by default, so tweak the default configuration when prompted to do so and set the listening port to 8080.
fly launch screenshot
Note: Also change the nest app to listen on the same port(8080) from main.ts/js as shown below
Change default port in nestjs

Step 4: Deploying the app

To deploy the app run the command below (from the root of your nest app).

fly deploy

This command detects our dockerfile, builds an image and deploys a container from it. If the build is successful, a url will be generated for you to access the app.

Step 5: Setting environment variables

To set runtime environment variables, use fly secrets set KEY=VALUE syntax to set sensitive variables that should not be included in the built docker image.
Example:
setting environment variables
You can also import bulk values from your .env file by doing:
fly secrets import < .env

This then updates each Machine belonging to that Fly App with the new environment variables. This involves a restart of the Machine(s).

Congratulations🎊

...

✅ Monolithic, Microservices and Mono Repo Architecture


📈 34.01 Punkte

✅ Pipeline strategies for a mono-repo – experiences with our Football Match Center projects in CodeCatalyst


📈 34.01 Punkte

✅ Pipeline strategies for a mono-repo – experiences with our Football Match Center projects in CodeCatalyst


📈 34.01 Punkte

✅ How to Implement Data Validation in NestJS Using nestjs-joi and joi-class-decorators


📈 33.97 Punkte

✅ NestJS tip: how to use Node.js built-in .env file reader with NestJS CLI


📈 33.97 Punkte

✅ Queuing jobs in NestJS using @nestjs/bullmq package


📈 33.97 Punkte

✅ yum repo mirror address - how to determine all the specific repo fqdns or IPs in a mirror list


📈 29.94 Punkte

✅ Mucky Duck Repo & Add-ons Not Working: Best Alternatives to Mucky Duck’s Repo


📈 29.94 Punkte

✅ Sync GitHub repo and Hugging Face Space Repo with GitHub Actions


📈 29.94 Punkte

✅ Monorepo, Poly-repo, or No Repo at all?


📈 29.94 Punkte

✅ Programmatically deploy your GitHub Repo on Netlify


📈 25.12 Punkte

✅ Create and Deploy a Discord Bot for Free with Fly.io


📈 24.61 Punkte

✅ How to Deploy a Next.js App Using Fly.io and Tigris


📈 24.61 Punkte

✅ How to use fly.io and Tigris to deploy a Next.js app


📈 24.61 Punkte

✅ Why a multi-layered technological approach can help the airline industry get back on board and safe to fly


📈 21.32 Punkte

✅ Why a multi-layered technological approach can help the airline industry get back on board and safe to fly


📈 21.32 Punkte

✅ CVE-2024-22359 | IBM UrbanCode Deploy/DevOps Deploy Web UI cross site scripting (XFDB-280897)


📈 20.31 Punkte

✅ CVE-2024-22339 | IBM UrbanCode Deploy/DevOps Deploy log file (XFDB-279979)


📈 20.31 Punkte

✅ CVE-2024-22358 | IBM UrbanCode Deploy/DevOps Deploy session expiration (XFDB-280896)


📈 20.31 Punkte

✅ CVE-2024-22334 | IBM UrbanCode Deploy/DevOps Deploy permission assignment (XFDB-279974)


📈 20.31 Punkte

✅ Running Windows Services on Linux with Mono


📈 19.04 Punkte

✅ Mono 1.2.5.1 on Windows staticfilehandler.cs information disclosure


📈 19.04 Punkte

✅ Is a There a Way to Make Apps Running via Mono Accept Files From Nautilus?


📈 19.04 Punkte

✅ How to enable Mono audio on Windows 10


📈 19.04 Punkte

✅ Carbon 1 MK II: Das Karbonfaser-Mono­coque-Smartphone startet doch noch


📈 19.04 Punkte

✅ Medium CVE-2020-12471: MONO Monox


📈 19.04 Punkte

✅ Compiling a Windows Service With Mono on Kali


📈 19.04 Punkte

✅ Mono up to 1.2.5.1 memory corruption [CVE-2007-5197]


📈 19.04 Punkte

✅ Why is Java considered more of a "first class citizen" in the Linux world compared to .NET/Mono?


📈 19.04 Punkte

✅ How to enable Mono audio on Windows 10


📈 19.04 Punkte

✅ If you make jewelry you will love the Anycubic Photon Mono SE 3D printer


📈 19.04 Punkte











matomo

Datei nicht gefunden!