pnpm.io Open in urlscan Pro
76.76.21.61  Public Scan

Submitted URL: https://pnpm.io/docker#example-1-build-a-bundle-in-a-docker-container
Effective URL: https://pnpm.io/docker
Submission: On November 28 via api from US — Scanned from DE

Form analysis 0 forms found in the DOM

Text Content

Skip to main content

pnpmDocsBlogFAQBenchmarksCommunity
More
 * Blog
 * FAQ
 * Community
 * Benchmarks

8.x
 * Next
 * 8.x
 * 7.x

English
 * English
 * Italiano (72%)
 * 简体中文 (89%)
 * 日本語 (70%)
 * 한국어 (60%)
 * Português Brasileiro (68%)
 * 正體中文 (44%)
 * Русский (41%)
 * Українська (11%)
 * Français (48%)
 * Türkçe (26%)
 * Español (59%)
 * Bahasa Indonesia (32%)
 * Help Us Translate

🧡 Sponsor Us
 * Open Collective
 * GitHub Sponsors
 * Crypto Donations


SearchK

 * Introduction
 * Usage
 * CLI commands
 * Configuration
 * Features
 * Recipes
   * Using Changesets with pnpm
   * Continuous Integration
   * Working with Git
   * Working with Docker
   * Working with Podman
 * Advanced

 * 
 * Recipes
 * Working with Docker

Version: 8.x
On this page


WORKING WITH DOCKER


note

It is impossible to create reflinks or hardlinks between a Docker container and
the host filesystem during build time. The next best thing you can do is using
BuildKit cache mount to share cache between builds. Alternatively, you may use
podman because it can mount Btrfs volumes during build time.


MINIMIZING DOCKER IMAGE SIZE AND BUILD TIME

 * Use a small image, e.g. node:XX-slim.
 * Leverage multi-stage if possible and makes sense.
 * Leverage BuildKit cache mounts.


EXAMPLE 1: BUILD A BUNDLE IN A DOCKER CONTAINER

Since devDependencies is only necessary for building the bundle, pnpm install
--prod will be a separate stage from pnpm install and pnpm run build, allowing
the final stage to copy only necessary files from the earlier stages, minimizing
the size of the final image.

.dockerignore

node_modules
.git
.gitignore
*.md
dist



Dockerfile

FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY . /app
WORKDIR /app

FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

FROM base
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
EXPOSE 8000
CMD [ "pnpm", "start" ]





EXAMPLE 2: BUILD MULTIPLE DOCKER IMAGES IN A MONOREPO

Assuming you have a monorepo with 3 packages: app1, app2, and common; app1 and
app2 depend on common but not each other.

You want to save only necessary dependencies for each package, pnpm deploy
should help you with copying only necessary files and packages.

Structure of the monorepo

./
├── Dockerfile
├── .dockerignore
├── .gitignore
├── packages/
│   ├── app1/
│   │   ├── dist/
│   │   ├── package.json
│   │   ├── src/
│   │   └── tsconfig.json
│   ├── app2/
│   │   ├── dist/
│   │   ├── package.json
│   │   ├── src/
│   │   └── tsconfig.json
│   └── common/
│       ├── dist/
│       ├── package.json
│       ├── src/
│       └── tsconfig.json
├── pnpm-lock.yaml
├── pnpm-workspace.yaml
└── tsconfig.json



pnpm-workspace.yaml

packages:
  - 'packages/*'



.dockerignore

node_modules
.git
.gitignore
*.md
dist



Dockerfile

FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS build
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run -r build
RUN pnpm deploy --filter=app1 --prod /prod/app1
RUN pnpm deploy --filter=app2 --prod /prod/app2

FROM base AS app1
COPY --from=build /prod/app1 /prod/app1
WORKDIR /prod/app1
EXPOSE 8000
CMD [ "pnpm", "start" ]

FROM base AS app2
COPY --from=build /prod/app2 /prod/app2
WORKDIR /prod/app2
EXPOSE 8001
CMD [ "pnpm", "start" ]




Run the following commands to build images for app1 and app2:

docker build . --target app1 --tag app1:latest
docker build . --target app2 --tag app2:latest



Edit this page

Previous
Working with Git
Next
Working with Podman
 * Minimizing Docker image size and build time
   * Example 1: Build a bundle in a Docker container
   * Example 2: Build multiple Docker images in a monorepo

Docs
 * Getting Started
 * pnpm CLI
 * Workspace
 * .npmrc

Community
 * User Showcase
 * Project Chat
 * Twitter
 * Mastodon
 * YouTube
 * Reddit
 * DEV
 * Hashnode
 * Keybase

Contributing
 * GitHub
 * Help Us Translate

Copyright © 2015-2023 contributors of pnpm