You clone an unfamiliar repo and run npm install. For those few seconds, the postinstall script of a package you've never heard of can read ~/.ssh just like you can.

In 2025, npm packages did exactly that to grab SSH keys, keychains and cloud configs. Shai-Hulud went further and used the stolen tokens to spread to other packages. That's why I moved my projects into devcontainers. I stayed for four other reasons, and the best one is this: no more "works on my machine, breaks in production".

A devcontainer is a few lines of JSON in your repo

You add a .devcontainer/ folder to the repo and describe the environment you need. VS Code (the "Reopen in Container" button) or @devcontainers/cli reads it and builds the container. Your code stays on your machine and you edit it in your usual editor. Installing packages, running the dev server and running tests all happen inside the container, which only sees the project folder.

The minimal version is just this:

// .devcontainer/devcontainer.json { "build": { "dockerfile": "Dockerfile" }, "postCreateCommand": "npm ci", "forwardPorts": [3000] } 
# .devcontainer/Dockerfile FROM mcr.microsoft.com/devcontainers/typescript-node:24 
flowchart LR subgraph Mac["Your machine"] S["SSH keys · credentials · work code"] P["project folder"] end subgraph C["Container"] R["npm install · dev server · test"] end P -- "only this folder goes in" --> C

Win 1: local runs like production, so bugs show up before deploy

I wrote a function that grouped orders by status with Object.groupBy. It ran fine on my machine and every test passed. After deploy, the server threw a TypeError on the first request. My Mac had Node 22 from Homebrew, the server was still on Node 20, and Object.groupBy only exists from Node 21. There was no way that bug could show up on my machine.

A devcontainer lets you run the same Node as your server: server on Node 20, devcontainer Dockerfile on typescript-node:20. If you deploy with Docker, the production image is FROM node:20 too, so both sides match. The Node installed on your machine stops mattering, because your code no longer runs on it.

Win 2: every project gets its own Node, and a broken setup is one rebuild away

The new project runs Node 24. The old one is stuck on Node 20 because nobody dares to upgrade it. I used nvm and had to remember nvm use every time I switched projects. Forget once and node_modules gets installed with the wrong version, so you delete it and start over.

Mismatches between teammates' machines are worse. Our team once lost about two hours because a new hire was still on Node 18 while everyone else had moved to Node 20. They reported that toSorted was not a function, nobody could reproduce it, and it took a while before someone asked "which Node are you on?". Array.prototype.toSorted only exists from Node 20.

Now each project pins its Node in its own Dockerfile. Everyone who clones it gets that exact version, and two containers run side by side without stepping on each other. Upgrading the old project means changing one FROM line. If you break something inside the container, rebuild it (VS Code has a Rebuild Container command): the old container is deleted and a new one is built from the same file. Your code lives on your machine, so nothing is lost.

Win 3: new people clone the repo and it just runs

A new hire on our team once spent half of their first day following the "Setup" section of the README: install the right Node, run npm ci, remember prisma generate, then discover the README was missing a fourth step.

postCreateCommand runs once, right after the container is created, so those steps move out of the README and into the config:

"postCreateCommand": "npm ci && if [ -f prisma/schema.prisma ]; then npx prisma generate; fi" 

A new person opens the repo, waits for the container to build, and npm run dev works. The README can't go stale when there are no steps left to forget.

Win 4: a malicious npm install only gets the project folder

Back to that postinstall from the intro. On your machine, it sees everything you see. In a container, it only sees what you mounted:

A malicious postinstall looks for… On the Mac In the container
SSH keys (~/.ssh) yes doesn't exist
AWS credentials (~/.aws) yes doesn't exist
Keychain, GitHub CLI login (gh) yes doesn't exist
Work code in the folder next door yes doesn't exist
The project folder yes yes

The first rows are exactly what npm malware goes after, and inside the container they're simply not there.

One exception if you open the project in VS Code: the extension forwards your SSH agent and git credentials into the container. The keys themselves can't be read, but code inside can still use them while the container runs. I run containers with @devcontainers/cli, so that path doesn't exist for me.

There's one more gap, in the last row. The project folder is mounted read-write, and .git lives inside it. Malicious code only has to drop a script into .git/hooks/pre-commit, or set core.fsmonitor in .git/config to point at that script. Next time you run git commit, or even just git status, on your Mac, git runs it right there, with your permissions.

flowchart LR A["malicious postinstall<br/>(in container)"] -- "writes hook / core.fsmonitor" --> G[".git in project folder"] B["you run git status<br/>(on the Mac)"] --> G G -- "git runs the script" --> X["malicious script runs on the Mac"]

One line closes it: mount .git separately, read-only.

"mounts": [ "source=${localWorkspaceFolder}/.git,target=/workspaces/app/.git,type=bind,readonly" ] 

A container still shares the kernel with its host (on a Mac, that's Docker's Linux VM), so don't treat it as a sandbox for code you know is malicious. Against the malware sprayed across npm, it's enough.

Win 5: let an AI agent run commands without watching over it

I code with Claude Code, an AI agent that runs in the terminal and installs packages, runs tests and starts the dev server on its own. Once I asked it to add a charting library, and it ran npm install straight on my Mac. Nothing bad happened. But I had just let a program pick a package and run that package's postinstall, without asking me anything.

With a container in place, the only job left is making the agent use it. Claude Code has a PreToolUse hook that runs before every command the agent is about to execute. Mine blocks npm, npx, node or any other JS tool run directly on the host and tells the agent to run it in the container. The hook only checks strings, so a cleverly disguised command still gets through. The real isolation is the container; the hook just makes sure the agent remembers to use it.

The costs, and how to fix each one

Slow on a Mac. On a Mac, containers run inside a Linux VM, and reading or writing files mounted from the Mac is slow. node_modules is tens of thousands of small files. The fix is in the VS Code docs: put node_modules in a named volume, which lives entirely inside the VM.

"mounts": ["source=${devcontainerId}-node_modules,target=/workspaces/app/node_modules,type=volume"] 

On my Next.js project, npm ci went from over 90 seconds to about 25.

The dev server doesn't see new files. Files created on the Mac side don't fire inotify events (the Linux mechanism that tells watchers a file changed) inside the container. New routes return 404 until you restart the dev server. Turn on polling: WATCHPACK_POLLING=true for Next.js, server.watch.usePolling for Vite.

A database on the Mac. Inside the container, localhost is the container itself. Point DATABASE_URL at host.docker.internal, Docker's name for your Mac as seen from inside the container.

Committing on the Mac. .git is read-only and the container (when run with the CLI) has no credentials, so you commit and push from a terminal outside it.

Disk space. Each project has an image and a few volumes, which add up to several GB. Run docker system df now and then and clean up.

Ports. If two containers ask for the same host port, the second one won't start. Give each project its own port. NextAuth apps with a callback hardcoded to localhost:3000 can only run one at a time.

Most of these you fix once, when you set up the project.

Where to start

Pick a side project, or the unfamiliar repo you're about to clone. Copy the minimal version above and add three things:

  1. node_modules in a named volume, so it isn't slow.
  2. .git mounted read-only, so nothing can plant a hook that runs on your machine.
  3. If you use appPort (Docker publishes the port; you need it when running the CLI instead of VS Code), always put 127.0.0.1: in front, as in "127.0.0.1:3000:3000". Without an IP, Docker publishes the port on every address of your machine, so anyone on the same Wi-Fi can open your dev server.

Try it for a week, then decide whether the next project moves too. Next time you clone a strange repo, go ahead and run npm install in the container, no need to skim package.json for anything suspicious first.