Docker packages a program together with everything it needs, so it runs the same on any machine that has Docker. A multi-stage Dockerfile goes a step further: one stage compiles the code and a later stage keeps only the result. With one extra “export” stage you can pull just the compiled files out of a build and hand those over, so someone can run your web app with nothing but Docker and never receives your source code.

I set this up at work to hand a browser-based simulator to someone who only needed to run it, not to read or change its code.

Four words first

TermWhat it is
DockerfileThe recipe: the steps Docker follows to build an image
ImageThe packaged result of the recipe, with all its files and programs. It doesn’t run by itself.
ContainerA running copy of an image, like a small separate computer inside yours
StageEach FROM line starts a stage. AS name gives it a name that later stages and --target can refer to.

For a web app the last stage is usually nginx, a small web server that answers the browser by sending files. A port mapping such as 8080 to 3000 connects a port on your machine to the port nginx listens on inside the container, which is why the browser opens localhost:8080.

Where the hand-off happens

SHARE THE REPOSITORY Source codeyour code clone Compilenpm ci, build Compiled filesHTML, JS, models nginx imageserves the files Browserlocalhost:8080 youthem SHARE THE COMPILED FOLDER Source codeyour code Compileonce, on your side Compiled filesexported folder send nginx imageone COPY, seconds Browserlocalhost:8080 youthem

Both ways run the same five steps. The only difference is where the dashed line sits: everything to its left stays on your machine. Sharing the repository puts the line before the compile step, so the other person needs the full source to build. Sharing a compiled folder puts it after, so their Dockerfile is a single COPY into nginx and builds in seconds.

The export stage

Stage 1: buildFROM node:22-alpineAlpine system filesnode_modules/src/ (your source)dist/ (compiled) COPY dist/ Stage 2: exportFROM scratchindex.htmlassets/, models/nothing else --target export stops here Stage 3: nginxFROM nginx:alpineskipped, no image made --output type=local site/ folder on disk --target build OS + node_modules + source

--target export builds the stages up to and including export, then stops, so the nginx stage never runs. --output type=local copies that stage’s whole filesystem into a folder instead of saving an image. Because the export stage starts FROM scratch, Docker’s empty image, its filesystem holds the compiled files and nothing else. Exporting the build stage directly would also work, but it would dump the operating system, node_modules and your source into the folder: exactly what you meant not to send.

A Dockerfile with all three stages looks like this:

FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build                 # writes /app/dist
 
FROM scratch AS export
COPY --from=build /app/dist /
 
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html

Then, from the project folder:

rm -rf ./site
docker build --target export --output type=local,dest=./site .

Each part of COPY --from=build /app/dist / comes from somewhere: build is the name after AS, /app is the WORKDIR, dist is the build tool’s output folder, and the final / puts the files at the top of the export folder rather than in app/dist/ inside it.

What bites in practice

  • The export stage goes in the middle, not at the end. Without --target, Docker builds the last stage. If export came last, a normal docker build or docker compose up --build would produce an empty image.
  • Clear the folder before exporting. Compiled JavaScript files get new hashed names on every build, so old files pile up unless you empty the destination first.
  • Needs BuildKit. --output is a BuildKit feature. BuildKit is the default builder from Docker Engine 23.0; on older installs use docker buildx build.
  • Don’t fix permission errors with sudo docker build. The exported files would then belong to root. Add your user to the docker group and log in again instead. Be aware that membership in that group is effectively root access on the machine.

Compiled isn't secret

The browser has to download every file in the compiled folder to run the app, so anyone can read the minified JavaScript in the browser’s developer tools. Shipping compiled files keeps your source code private, not the app’s behaviour. Check the folder has no .map source-map files before sending it, because those would let the browser rebuild the original source.

Related: serving-an-app-on-the-lan