1- # This Dockerfile creates a production-ready container for a LiveKit Node.js agent using Bun
2- # It uses a multi-stage build to minimize the final image size
31# syntax=docker/dockerfile:1
2+ # For detailed documentation and guides, see:
3+ # https://github.com/livekit/livekit-cli/blob/main/pkg/agentfs/examples/README.md
4+ # For more help: https://docs.livekit.io/agents/
5+ # For help with building and deployment: https://docs.livekit.io/agents/ops/deployment/cloud/build
46
5- # === MULTI-STAGE BUILD STRUCTURE ===
6- # Stage 1 (base): Sets up Bun runtime environment
7- # Stage 2 (build): Installs dependencies and builds the application
8- # Stage 3 (final): Copies only necessary files for runtime
9- #
10- # Benefits: Smaller final image without build tools and source files
11- # Final image contains only: compiled JS, node_modules, and runtime dependencies
12-
13- # Use official Bun image as base
147ARG BUN_VERSION=1
158FROM oven/bun:${BUN_VERSION} AS base
169
17- # Define the program entrypoint file where your agent is started.
10+ # Define the program entrypoint file where your agent is started
1811ARG PROGRAM_MAIN="{{.ProgramMain}}"
1912
20- # Set the working directory where our application will live
2113WORKDIR /app
2214
2315# === BUILD STAGE ===
24- # This stage is discarded after building, keeping the final image small
2516FROM base AS build
2617
27- # Copy package.json and lock file first for better layer caching
28- # This allows Docker to cache the dependency installation step
18+ # Copy package files first for layer caching
2919COPY package.json bun.lock* ./
3020
31- # Install dependencies using bun
32- # Bun automatically uses the lock file if it exists
33- # Install all dependencies including dev for the build stage
21+ # Install all dependencies
3422RUN bun install --frozen-lockfile
3523
36- # Set production environment
3724ENV NODE_ENV=production
3825
39- # Copy all application files into the build container
26+ # Copy source code
4027COPY . .
4128
42- # Build the TypeScript application (if needed)
43- # Bun can run TypeScript directly, but building may still be needed for bundling
29+ # Build if needed (Bun can run TypeScript directly)
4430RUN bun run build
4531
46- # Prune any dev dependencies that might have been needed for build
47- # This keeps only production dependencies
32+ # Reinstall production only
4833RUN bun install --production
4934
50- # === FINAL PRODUCTION STAGE ===
51- # Start from the base image without build tools
35+ # === RUNTIME STAGE ===
5236FROM base
5337
54- # Create a non-privileged user that the app will run under.
55- # See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
38+ # Create non-privileged user
5639ARG UID=10001
5740RUN adduser \
5841 --disabled-password \
@@ -62,93 +45,12 @@ RUN adduser \
6245 --uid "${UID}" \
6346 appuser
6447
65- # Copy the built application from the build stage
66- # This includes node_modules and compiled JavaScript files
48+ # Copy built application from build stage
6749COPY --from=build /app /app
6850
69- # Change ownership of all app files to the non-privileged user
70- # This ensures the application can read/write files as needed
51+ # Set ownership and switch user
7152RUN chown -R appuser:appuser /app
72-
73- # Switch to the non-privileged user for all subsequent operations
74- # This improves security by not running as root
7553USER appuser
7654
77- # Run the application using Bun
78- # The "start" command tells the agent to connect to LiveKit and begin waiting for jobs
79- # Bun can run TypeScript directly, but we use the built JS for production
80- CMD [ "bun" , "run" , "{{.ProgramMain}}" , "start" ]
81-
82- # === COMMON CUSTOMIZATIONS ===
83- #
84- # 1. Production-only dependencies:
85- # To install only production dependencies (exclude devDependencies):
86- # RUN bun install --frozen-lockfile --production
87- #
88- # 2. Direct TypeScript execution:
89- # Bun can run TypeScript files directly without compilation:
90- # CMD ["bun", "run", "./src/agent.ts", "start"]
91- #
92- # 3. Different entry point locations:
93- # - If using src/index.ts: CMD ["bun", "run", "./src/index.ts", "start"]
94- # - If using dist/main.js: CMD ["bun", "run", "./dist/main.js", "start"]
95- # - For development: CMD ["bun", "run", "dev"]
96- #
97- # 4. Environment variables:
98- # Set Node.js environment for production:
99- # ENV NODE_ENV=production
100- #
101- # 5. Running as non-root user (recommended for security):
102- # Add before the final CMD:
103- # RUN adduser --disabled-password --gecos "" --uid 10001 appuser
104- # USER appuser
105- #
106- # 6. Using Node.js compatibility mode:
107- # Some packages may need Node.js APIs:
108- # CMD ["bun", "--bun", "run", "{{.ProgramMain}}", "start"]
109- #
110- # === TROUBLESHOOTING BUN-SPECIFIC ISSUES ===
111- #
112- # 1. "bun.lock not found":
113- # - Run `bun install` locally to generate bun.lock
114- # - Commit bun.lock to version control for reproducible builds
115- # - Use --frozen-lockfile for production builds
116- #
117- # 2. "Module not found" errors:
118- # - Ensure all dependencies are in package.json
119- # - Bun uses a different module resolution than Node.js
120- # - Try using --bun flag for better compatibility
121- # - Check that node_modules are copied correctly
122- #
123- # 3. Node.js API compatibility:
124- # - Some Node.js APIs may not be fully implemented
125- # - Use --bun flag to enable Bun's runtime
126- # - Consider fallback to Node.js for incompatible packages
127- #
128- # 4. TypeScript issues:
129- # - Bun runs TypeScript natively, no compilation needed
130- # - However, some TypeScript features may differ
131- # - Check tsconfig.json compatibility with Bun
132- #
133- # 5. Large image sizes:
134- # - Use oven/bun:alpine for smaller base image
135- # - Ensure .dockerignore excludes unnecessary files
136- # - Use bun install --production for production builds
137- #
138- # 6. Performance differences:
139- # - Bun is generally faster for startup and execution
140- # - However, some optimizations may differ from Node.js
141- # - Profile your application if performance issues arise
142- #
143- # 7. Native module issues:
144- # - Bun has different native module support than Node.js
145- # - Some Node.js native modules may not work
146- # - Check Bun's compatibility list for your dependencies
147- #
148- # 8. Runtime connection issues:
149- # - Verify the agent can reach the LiveKit server
150- # - Check that required environment variables are set
151- # - Ensure the healthcheck endpoint (8081) is accessible
152- #
153- # For more help: https://bun.sh/docs
154- # For LiveKit agent build help: https://docs.livekit.io/agents/ops/deployment/cloud/build
55+ # Bun can run TypeScript directly
56+ CMD [ "bun" , "run" , "{{.ProgramMain}}" , "start" ]
0 commit comments