<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by fintlabs on Medium]]></title>
        <description><![CDATA[Stories by fintlabs on Medium]]></description>
        <link>https://medium.com/@fintlabs?source=rss-cd8269340f21------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*v52PwTJzCxomuM1-7OCd4Q.png</url>
            <title>Stories by fintlabs on Medium</title>
            <link>https://medium.com/@fintlabs?source=rss-cd8269340f21------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:02:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fintlabs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optimized multi-stage Docker builds with TurboRepo and PNPM for NodeJS microservices in a monorepo]]></title>
            <link>https://fintlabs.medium.com/optimized-multi-stage-docker-builds-with-turborepo-and-pnpm-for-nodejs-microservices-in-a-monorepo-c686fdcf051f?source=rss-cd8269340f21------2</link>
            <guid isPermaLink="false">https://medium.com/p/c686fdcf051f</guid>
            <category><![CDATA[turborepo]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[pnpm]]></category>
            <dc:creator><![CDATA[fintlabs]]></dc:creator>
            <pubDate>Thu, 19 Oct 2023 15:17:20 GMT</pubDate>
            <atom:updated>2023-10-19T18:52:18.319Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CME5CmGIzoXCQFWGZ11q2g.png" /></figure><blockquote>In the world of microservices and monorepos, optimizing Docker builds is crucial for efficiency and performance.</blockquote><h3>Introduction</h3><p><a href="https://docs.docker.com/build/building/multi-stage">Multi-stage Docker builds</a> is a powerful technique to create lightweight and efficient containers. But it can be tricky to get an optimized <strong>Dockerfile</strong> for a microservice when <strong>TurboRepo</strong> and <strong>PNPM</strong> are also involved. In this article, we’ll explore how to harness the power of multi-stage builds with <a href="https://turbo.build/repo">TurboRepo</a> and <a href="https://pnpm.io/">PNPM</a> to streamline your <strong>NodeJS</strong> microservice build process in a monorepo.</p><h3>The Goal</h3><p>We want to have a single <strong>Dockerfile</strong> that we can use to build every NodeJS-based microservice in our monorepo. It must comply with the following requirements:</p><ul><li>Use build arguments to customize the build (NodeJS version, project to build, port to expose) so that it can be used for any project</li><li>Install only the required dependencies for a particular microservice</li><li>Build only the source code (and dependency packages) for a particular microservice</li><li>Generate a lightweight Docker image with minimum required dependencies and bundle</li></ul><h3>Prerequisites</h3><ul><li>You have Docker installed and <a href="https://docs.docker.com/build/buildkit/">BuildKit</a> enabled.</li><li>A monorepo managed with TurboRepo or you know how to set up a new one. If not, you can read more <a href="https://turbo.build/repo/docs/getting-started/create-new">here</a>.</li><li>You know how to get your head around PNPM Workspaces. If not, you can read more <a href="https://pnpm.io/workspaces">here</a> and <a href="https://turbo.build/repo/docs/handbook/workspaces">here</a>.</li></ul><h3>Final multi-stage Dockerfile</h3><p>Multi-stage builds allow us to create optimized Docker images by breaking down the build process into multiple stages. Each stage focuses on a specific aspect of the build, resulting in smaller and faster containers. For microservices in a monorepo, this is a game-changer.</p><p>Let’s see how we can achieve our goal using the following Dockerfile:</p><pre>ARG NODE_VERSION=18.18.0<br><br># Alpine image<br>FROM node:${NODE_VERSION}-alpine AS alpine<br>RUN apk update<br>RUN apk add --no-cache libc6-compat<br><br># Setup pnpm and turbo on the alpine base<br>FROM alpine as base<br>RUN npm install pnpm turbo --global<br>RUN pnpm config set store-dir ~/.pnpm-store<br><br># Prune projects<br>FROM base AS pruner<br>ARG PROJECT<br><br>WORKDIR /app<br>COPY . .<br>RUN turbo prune --scope=${PROJECT} --docker<br><br># Build the project<br>FROM base AS builder<br>ARG PROJECT<br><br>WORKDIR /app<br><br># Copy lockfile and package.json&#39;s of isolated subworkspace<br>COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml<br>COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml<br>COPY --from=pruner /app/out/json/ .<br><br># First install the dependencies (as they change less often)<br>RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --frozen-lockfile<br><br># Copy source code of isolated subworkspace<br>COPY --from=pruner /app/out/full/ .<br><br>RUN turbo build --filter=${PROJECT}<br>RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm prune --prod --no-optional<br>RUN rm -rf ./**/*/src<br><br># Final image<br>FROM alpine AS runner<br>ARG PROJECT<br><br>RUN addgroup --system --gid 1001 nodejs<br>RUN adduser --system --uid 1001 nodejs<br>USER nodejs<br><br>WORKDIR /app<br>COPY --from=builder --chown=nodejs:nodejs /app .<br>WORKDIR /app/apps/${PROJECT}<br><br>ARG PORT=8080<br>ENV PORT=${PORT}<br>ENV NODE_ENV=production<br>EXPOSE ${PORT}<br><br>CMD node dist/main</pre><h3>Explanation of significant steps</h3><p>Let’s go through the file to understand what each stage is doing.</p><h4>Build arguments</h4><p>First of all, to have a single Dockerfile that can be used to build multiple projects we need to leverage Docker build arguments. These are:</p><ul><li>NODE_VERSION — Optional. Specifies base image version. BTW here we are using an alpine image to get the thinnest possible final image.</li><li>PROJECT — Required. Specifies the project that needs to be built.</li><li>PORT — Optional. Specifies the port that the Docker image exposes.</li></ul><h4>Base image for build</h4><pre># Setup pnpm and turbo on the alpine base<br>FROM alpine as base<br>RUN npm install pnpm turbo --global<br>RUN pnpm config set store-dir ~/.pnpm-store</pre><p>Here we take the alpine image as a base. Install turbo and pnpm globally using npm. Configure the store directory of pnpm to use it later for caching.</p><h4>Pruning</h4><pre>RUN turbo prune --scope=${PROJECT} --docker</pre><p>Explanation of this command is taken from the official <a href="https://turbo.build/repo/docs/reference/command-line-reference/prune">docs</a> for TurboRepo:</p><p>The prune command will generate a folder called out with the following inside of it:</p><ul><li>A folder json with the pruned workspace&#39;s package.json files.</li><li>A folder full with the pruned workspace&#39;s full source code, but only including the internal packages that are needed to build the target.</li><li>A new pruned lockfile that only contains the pruned subset of the original root lockfile with the dependencies that are actually used by the packages in the pruned workspace.</li></ul><h4>Installing dependencies</h4><pre># Copy lockfile and package.json&#39;s of isolated subworkspace<br>COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml<br>COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml<br>COPY --from=pruner /app/out/json/ .<br><br># First install the dependencies (as they change less often)<br>RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --frozen-lockfile</pre><p>This piece copies the pruned lockfile, package.json files of the pruned workspace and installs both <strong>devDependencies </strong>and<strong> dependencies</strong>.</p><p>RUN command also uses the specialized cache mount feature of <strong>BuildKit</strong>.</p><h4>Building</h4><pre># Copy source code of isolated subworkspace<br>COPY --from=pruner /app/out/full/ .<br><br>RUN turbo build --filter=${PROJECT}<br>RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm prune --prod --no-optional<br>RUN rm -rf ./**/*/src</pre><p>Here we COPY the pruned source code from the previous pruner image.</p><p>In the first RUN command, we use turbo build by specifying the target project. It first builds all dependencies, then builds the target project itself.</p><p>The second RUN command uses pnpm prune --prod to remove <strong>devDependencies</strong> from node_modules as we don’t need them anymore.</p><p>The last RUN command removes all src folders, so that source files will not be copied to the final image.</p><h4>Final production image</h4><pre># Final image<br>FROM alpine AS runner<br>ARG PROJECT<br><br>RUN addgroup --system --gid 1001 nodejs<br>RUN adduser --system --uid 1001 nodejs<br>USER nodejs<br><br>WORKDIR /app<br>COPY --from=builder --chown=nodejs:nodejs /app .<br>WORKDIR /app/apps/${PROJECT}<br><br>ARG PORT=8080<br>ENV PORT=${PORT}<br>ENV NODE_ENV=production<br>EXPOSE ${PORT}<br><br>CMD node dist/main</pre><p>For the final image, we are using a node-alpine base image with minimal dependencies installed.</p><p>Next, we create a non-root user for our container as it is not a good idea to run containers as root.</p><pre>COPY --from=builder --chown=nodejs:nodejs /app .<br>WORKDIR /app/apps/${PROJECT}</pre><p>This piece copies the bundled version of our app and its dependencies from the previous stage and sets the current working directory to the folder of the target microservice specified as PROJECT argument during the build.</p><p>It is a good idea to EXPOSE the port on which your service listens as some reverse proxy tools rely on this information.</p><p>Another best practice is to set NODE_ENV=production for your production images.</p><p>You can build the final image using the following command</p><pre>docker build -t api:latest --build-arg PROJECT=api .</pre><p>where <strong>api </strong>is your microservice’s name.</p><h3>Conclusion</h3><p>Multi-stage Docker builds, in combination with <strong>TurboRepo</strong> and <strong>PNPM</strong>, provide a powerful solution for microservices within a monorepo. By adopting techniques provided in this article, you’ll witness significant improvements:</p><ul><li>Smaller Docker images, reducing storage and network costs</li><li>Faster build times due to optimized layers and caching</li><li>Efficient use of resources in a monorepo setup</li><li>Enhanced security and overall performance of the containers</li></ul><p>Embrace this approach to level up your microservices game in your monorepo.</p><p>If you found this article helpful, please clap the button 👏 below and Follow us for more quality content like this.</p><p>Thanks for reading!</p><p>#docker #dockerfile #turborepo #pnpm #monorepo #microservices #nodejs #cicd #containers</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c686fdcf051f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>