DEV Community

Cover image for Containerizing browser-use for Local Runs: Non-Root Images and Compose Limits
Hannah Schmidt
Hannah Schmidt

Posted on

Containerizing browser-use for Local Runs: Non-Root Images and Compose Limits

At 3:11 a.m. the page is rarely about the model. It is about a browser agent that wrote outside its workdir, then pinned the host until the laptop sounded like a dying disk. Local browser-use looks cheap until a root-owned browser child or a cold npm cache turns a five-minute check into an hour and a filesystem you no longer trust.

browser-use is open-source infrastructure for LLM-driven web agents. We integrate it from the outside; we do not maintain the core. The failures that matter on a laptop are environmental: host library drift, lockfiles that rebuild from scratch, and processes that still start as root. An exploit in the agent or the browser does not need to be clever if it inherits the host uid.

A team embedding browser-use in a local pipeline saw builds pass an hour whenever a layer change invalidated the npm cache. Root in the runtime made a supply-chain hit equivalent to host filesystem access. We moved to a multi-stage Buildx image and Compose: non-root execution, cache mounts, and hard resource limits.

Multi-Stage Dockerfile with BuildKit Cache Mounts

The core is TypeScript, so the image is Node. The builder resolves and compiles. The runtime copies artifacts only, installs production dependencies, then switches user before the process starts.

# syntax=docker/dockerfile:1.4

# Builder stage
FROM node:20-alpine AS builder

WORKDIR /usr/src/app

# Install dependencies with cache mount for faster local builds
RUN --mount=type=cache,target=/root/.npm \
    npm ci

COPY package*.json ./
RUN npm ci

COPY . .

RUN npm run build

# Production stage - minimal runtime image
FROM node:20-alpine AS runtime

WORKDIR /usr/src/app

# Copy only necessary artifacts
COPY --from=builder /usr/src/app/dist ./dist
COPY --from=builder /usr/src/app/package*.json ./

# Install production dependencies only
RUN npm ci --only=production

# Create non-root user for security
RUN addgroup -S appgroup && adduser -S -G appgroup appuser
USER appuser

EXPOSE 3000

CMD ["node", "dist/index.js"]
Enter fullscreen mode Exit fullscreen mode

Warm cache mounts dropped our iterations from more than ten minutes to under thirty seconds, as long as the mount path stays fixed and installs stay on the lockfile. The app user cannot write system paths. That will not stop a kernel bug. It does remove the default where a container root is effectively the host.

If npm ci fails, rebuild the lockfile on a trusted machine and rerun the builder. Do not switch the runtime to npm install. If the unprivileged user cannot read an artifact, fix ownership before USER, not by going back to root. A failed health probe is a restart-and-read-logs event, not a reason to exec into the container as root and patch the host.

Production-Grade docker-compose.yml

Compose is the local control plane: cache hints, an internal network, a readiness probe, and CPU and memory caps so one agent cannot starve the machine.

version: '3.8'

services:
  browser-agent:
    build:
      context: .
      dockerfile: Dockerfile
      cache_from:
        - type=registry,ref=local/browser-agent:latest
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      # Example LLM relay configuration
      - LLM_BASE_URL=https://b-lost.com/v1
    resources:
      limits:
        cpus: '0.8'
        memory: 1.5GB
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    networks:
      - agent-network

networks:
  agent-network:
    internal: true
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

The internal bridge keeps companion traffic off the host except for the deliberate 3000 publish. The health check stops dependents from calling a process that is up but not ready. Caps at 0.8 CPU and 1.5 GB keep one session from swapping the laptop into uselessness. On a flap, restart the container and read its logs before touching the host. If memory pressure returns, cut concurrency instead of lifting the cap.

Provider routing stays outside the image. Agent config uses baseURL: "https://b-lost.com/v1" so OpenAI, Claude, or another upstream can fail over without a code change. Quota cliffs mid-session are the page we still get after the container itself is stable.

Single-stage root images are shorter. They are also where host-specific libraries and elevated permissions kept producing incidents. This layout adds build steps and a runtime near 250 MB. The builder compiles. The runtime executes. Compose limits and probes. Uncontainerized runs still show the intermittent bugs tied to whatever the laptop had installed.

The hard dilemma is not the Dockerfile. A browser agent must reach the public web and still be small enough to kill. Tighten seccomp, drop capabilities, and shrink memory, and real sites fail in ways that look like model bugs. Loosen them, and the isolation is mostly decorative.

Where have you drawn that line for local browser agents: more sandbox, or fewer false failures at 3 a.m.?

Top comments (0)