Skip to content
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

Optimize dockerfiles #113

Closed
rajdip-b opened this issue Feb 6, 2024 · 20 comments · Fixed by #173
Closed

Optimize dockerfiles #113

rajdip-b opened this issue Feb 6, 2024 · 20 comments · Fixed by #173
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed scope: api Everything related to the API scope: web Everything related to the webiste type: enhancement New feature or request

Comments

@rajdip-b
Copy link
Member

rajdip-b commented Feb 6, 2024

Description

Currently, the Dockerfiles for web and api are crossing 1GB size. We would like to reduce the final build size so that we get a significant lower build time in out CIs.

The files are:

  • apps/api/Dockerfile
  • apps/web/Dockerfile

Things you can do:

  • Use a multi-staged build
  • Purge the development dependencies
@rajdip-b rajdip-b added type: enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed scope: web Everything related to the webiste scope: api Everything related to the API labels Feb 6, 2024
@rajdip-b rajdip-b moved this to Todo in keyshade-api Feb 6, 2024
@rajdip-b
Copy link
Member Author

Hey bro @Abbhiishek mind giving this a shot?

@Abbhiishek
Copy link
Contributor

sure

@rajdip-b
Copy link
Member Author

Awesome bro! @Abbhiishek assigning this to you

@rajdip-b rajdip-b moved this from Todo to In progress in keyshade-api Feb 16, 2024
@Abbhiishek
Copy link
Contributor

Abbhiishek commented Feb 17, 2024

I have created a draft for web:

# Alpine image
FROM node:20-alpine AS alpine
RUN apk update
RUN apk add --no-cache libc6-compat


# Setup pnpm and turbo on the alpine base
FROM alpine as base
RUN npm install pnpm turbo --global
RUN pnpm config set store-dir ~/.pnpm-store



# Prune projects
FROM base AS pruner
ARG PROJECT


WORKDIR /app
COPY . .
RUN turbo prune --scope=${PROJECT} --docker

# Build the project
FROM base AS builder
ARG PROJECT



WORKDIR /app

# Copy lockfile and package.json's of isolated subworkspace
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=pruner /app/out/json/ .


# First install the dependencies (as they change less often)
RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --frozen-lockfile


# Copy source code of isolated subworkspace
COPY --from=pruner /app/out/full/ .

RUN turbo build --filter=${PROJECT}
RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm prune --prod --no-optional
RUN rm -rf ./**/*/src



# Final image
FROM alpine AS runner
ARG PROJECT

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs

WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app .
WORKDIR /app/apps/${PROJECT}

ARG PORT=3000
ENV PORT=${PORT}
ENV NODE_ENV=production
EXPOSE ${PORT}

CMD node dist/main

I am passing project as web for building the web micro however :( the below mentioned error occurred

=> ERROR [web pruner 3/3] RUN turbo prune --scope=web --docker                                                                                           0.4s
------
 > [web pruner 3/3] RUN turbo prune --scope=web --docker:
0.382
0.382 Attention:
0.382 Turborepo now collects completely anonymous telemetry regarding usage.
0.382 This information is used to shape the Turborepo roadmap and prioritize features.
0.382 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
0.382 https://turbo.build/repo/docs/telemetry
0.382
0.424   x invalid scope: package web not found
0.424
------
failed to solve: process "/bin/sh -c turbo prune --scope=${PROJECT} --docker" did not complete successfully: exit code: 1

@rajdip-b
Copy link
Member Author

rajdip-b commented Feb 17, 2024

@Abbhiishek Yeah actually you got a base very wrong :) You are using turbo but this repository is using nx

I think you can just build the project, and then copy over the dist to the main image. Without any deps or anything. Building and copying happens in the docker image not to mention

@Abbhiishek
Copy link
Contributor

ohhh my bad 🥲! i tried doing it traditionally but there was some path issues i will give it try once again

@rajdip-b
Copy link
Member Author

Not an issue my friend!

@rajdip-b
Copy link
Member Author

Hey bro @Abbhiishek made any further progress on this?

@Abbhiishek
Copy link
Contributor

hey i was able to dockerise but my application is not building correctly. Files are not imported to the container. Still working on this issue. Will keep you updated :)

@rajdip-b
Copy link
Member Author

hey i was able to dockerise but my application is not building correctly. Files are not imported to the container. Still working on this issue. Will keep you updated :)

Wow that's strange, do drop in a thread on discord if you wanna discuss this!

@Abbhiishek
Copy link
Contributor

i am giving a try to use https://www.npmjs.com/package/@nx-tools/nx-container plugin.

@rajdip-b
Copy link
Member Author

Hey bro, just hold up for a day or 2. We are migrating our project to Turbo. I believe it will be much more easier for you to do so once the migration is done!

@Abbhiishek
Copy link
Contributor

turbo 😀

@Abbhiishek
Copy link
Contributor

sure

@rajdip-b
Copy link
Member Author

@Abbhiishek hey bro! The migration is complete. You can start your work on this.

@Abbhiishek
Copy link
Contributor

hey @rajdip-b ,

a general question about the web app microservice. I am referring to this page here . According to this each app should also contain a package.json file.

├── apps
│   ├── docs
│   │   ├── server.js
│   │   └── package.json
│   └── web
│       └── package.json
├── package.json
└── package-lock.json

@rajdip-b
Copy link
Member Author

@Abbhiishek yes since we now have turbo, every microservice now has its own package.json that has deps specific to that package.

@Abbhiishek
Copy link
Contributor

hey @rajdip-b i was able to contain web.
The new build size is 169mb

image

@rajdip-b
Copy link
Member Author

rajdip-b commented Apr 5, 2024

Awesome bro!!!

@github-project-automation github-project-automation bot moved this from In progress to Done in keyshade-api Apr 5, 2024
@rajdip-b
Copy link
Member Author

🎉 This issue has been resolved in version 1.3.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed scope: api Everything related to the API scope: web Everything related to the webiste type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants