Create Dockerfile to deploy NestJS API to fly.io

627 Views Asked by At

I created a REST API using NestJS and I want to deploy it to fly.io. fly.io requires me to create a Dockerfile to do so. I don't know much about Dockerfiles but fly.io has a cli tool that creates one for you.

I am not able to deploy my API, however, and my suspicion is that there's an issue with the Dockerfile. Specifically, that the generated Dockerfile isn't tailored specifically to a NESTJS app.

I am linking my repo below so you can view both the existing Dockerfile and the structure of my API. Can someone please suggest how I can modify the Dockerfile to work for a NESTJS app?

Thanks

https://github.com/AhmedAbbasDeveloper/noteify-server/tree/nestjs-migration

2

There are 2 best solutions below

0
On

I think you need to expose a port that your nestjs server runs on. For your docker image you can set an env variable by adding ENV PORT=8080 to your Dockerfile and also need to expose it with EXPOSE 8080. You can use any port, but since you've configured 8080 in fly, that makes more sense.

...
ENV PORT=8080
EXPOSE 8080
CMD ["node", "dist/main.js"]
0
On

This dockerfile worked for me.

FROM node:16-alpine as builder

ENV NODE_ENV build

USER node
WORKDIR /home/node

COPY package*.json ./
RUN npm ci

COPY --chown=node:node . .
RUN npm run build \
    && npm prune --production

# ---

FROM node:16-alpine

ENV NODE_ENV production

USER node
WORKDIR /home/node

COPY --from=builder --chown=node:node /home/node/package*.json ./
COPY --from=builder --chown=node:node /home/node/node_modules/ ./node_modules/
COPY --from=builder --chown=node:node /home/node/dist/ ./dist/

CMD ["node", "dist/server.js"]