All posts

Fix Kubernetes Pod Crash with Flutter Backend

Resolve Kubernetes pod crashes when serving Flutter web builds or running Dart backend services in containerized environments.

Kubernetes Pod Crashes Serving Flutter Apps

While Flutter is primarily a client-side framework, it's common to containerize Flutter web builds behind an Nginx server or run Dart backends with dart_frog or shelf. When these pods crash, here's how to fix them.

Flutter Web + Nginx: 403 Forbidden

The build output must be copied to the right Nginx directory:

# Build stage
FROM ghcr.io/cirruslabs/flutter:stable AS build
WORKDIR /app
COPY . .
RUN flutter build web --release

# Serve stage
FROM nginx:alpine
COPY --from=build /app/build/web /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

Make sure nginx.conf handles SPA routing:

server {
  listen 80;
  root /usr/share/nginx/html;
  index index.html;

  location / {
    try_files $uri $uri/ /index.html;
  }
}

Dart Backend Crashes

For Dart server applications, common pod crash causes include:

// Binding to localhost won't work in K8s
// Wrong:
final server = await shelf_io.serve(handler, 'localhost', 8080);

// Right:
final server = await shelf_io.serve(handler, '0.0.0.0', 8080);

Health Checks

Add a health endpoint to prevent liveness probe failures:

Router()
  ..get('/healthz', (Request req) => Response.ok('ok'))
  ..get('/api/data', handleData);

Bugsly's lightweight Dart integration captures unhandled exceptions from your server process, so crashed pods leave a clear error trail for post-mortem debugging.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free