with-docker and shared dependancies - how to cache the non-shared dependancies? #7457
Unanswered
yuval-illumex
asked this question in
Help
Replies: 1 comment
-
I do two separate npm installs: one so that I can build and a second one for The thing that is regrettably missing from the turbo docs is that once you build, you only want to keep the FROM base AS build-server
COPY tsconfig.json tsconfig.json
COPY --from=builder-server /app/out/json/ .
COPY --from=builder-server /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
# do a full install - required for build
RUN pnpm install --frozen-lockfile --filter=@myOrg/server...
COPY --from=builder-server /app/out/full/ .
# this calls `turbo build` eventually
RUN pnpm build:server
# Now, we only want to keep the `dist` folders, package.json files, and whatever else necessary for production
# This is sort of like a "production prune", and this is the part that is woefully omitted from the turbo docs or guides
# I am using linux commands to copy all production assets to a "release" folder
RUN mkdir release
COPY --from=builder-server /app/out/json/ .
COPY --from=builder-server /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
# Use rsync to copy files to the release folder. You can easily configure additional files to include or exclude as necessary.
RUN rsync -avz --include='packages/*/dist/' --exclude="packages/*/*" ./packages ./release/
RUN rsync -avz --include='services/*/dist/' --exclude="services/*/*" ./services ./release/
# Change dirctories to the release folder
WORKDIR /app/release
# Run production pnpm install - this should be fast because the previous install hydrated the cache
# You should not need to use `--filter` here - but do what you will
RUN pnpm install --frozen-lockfile --production
# Now copy your release assets into your final prod image
FROM base AS prod
WORKDIR /app
COPY --from=prod-staging /app/release/ ./
# I recommend rebuilding on the final machine in case you are using a different distro
RUN pnpm rebuild
ENTRYPOINT ["node", "apps/server/dist/main"] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Which project is this feature idea for?
Turborepo
Describe the feature you'd like to request
Describe the feature you'd like to request
Upon reviewing the with-docker example, I have got a question regarding best practice for the final production image.
Here is the structure of my projects:
Here is the dockerfile, based on the docker example in the documentation:
My question is about caching the production node_modules.
In the build-server layer, I can't install with the --production flag - because I need the devDependices for the build process.
However, If I will add a layer, that only install the node_modules for running in production:
I have a problem with the "shared" lib. In build-server stage, the "shared" lib is installed into the node_modules folder, but in the build-server-deps it won't exists. In addition, the "shared" lib is installed as symlink, so I don't see any other option.
Describe the solution you'd like
To have the ability to install only "source npm" libraries and cache them, and then to build the internal packages. Of course the internal package can use another "source npm" package, and it should be cached as expected.
Maybe there is an option to "merge" node modules folder, and then to copy @myorg from the node_modules of the build-server stage, and all the rest from build-server-deps stage.
Describe alternatives you've considered
At the moment I copy all the build dependencies, and not only the production one. Didn't found a better way
I thought about adding RUN pnpm prune --prod after building the server, but in the docker build logs I see the command is waiting for input from the user
Beta Was this translation helpful? Give feedback.
All reactions