When Database Migrations Belong in Helm Hooks, Not Init Containers
Init containers run on every pod restart. Helm pre-install hooks run once per release. Use hooks for database migrations and one-time setup—but watch for cleanup, hanging jobs, and rollback gaps.
18 Aug 2025, 23:02 UTC

The Problem: Migrations Running on Every Pod Restart
You deploy a Helm chart with a database migration in an init container. The first deploy works. Then a pod crashes, Kubernetes restarts it, and the migration runs again. If your migration isn't idempotent—and many aren't—you get duplicate key errors, failed deploys, or worse, silent data corruption.
The core issue: init containers run on every pod start. Helm hooks run once per release operation. That distinction matters when you need schema changes, seed data, or one-time setup that must not repeat on pod churn.
How Helm Hooks Fit Into the Release Lifecycle
Helm hooks are ordinary Kubernetes resources (Jobs, Pods, ConfigMaps) annotated with helm.sh/hook. The hook annotation value—pre-install, post-install, pre-upgrade, post-upgrade, pre-delete, post-delete, pre-rollback, post-rollback—tells Helm when to create the resource and wait for it to reach completion before proceeding.
Hook resources are not tracked as part of the release by default. They live alongside your release but aren't rolled back when you run helm rollback. The helm.sh/hook-delete-policy annotation controls cleanup: before-hook-creation (default), hook-succeeded, hook-failed, or a comma-separated list.
Helm 3 scopes releases to a namespace. Hook visibility and cleanup depend on the namespace where the release is installed. If you install the same chart in two namespaces, each gets its own hook jobs.
Worked Example: Pre-Install Migration Job
Below is a minimal chart fragment showing a pre-install hook job for a PostgreSQL migration. Save as templates/migration-job.yaml in your chart.
apiVersion: batch/v1
kind: Job
metadata:
name: "{{ .Release.Name }}-db-migration"
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-delete-policy": hook-succeeded
"helm.sh/hook-weight": "0"
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: migrate
image: myorg/myapp-migrate:v1.2.3
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: "{{ .Release.Name }}-db-secret"
key: url
command: ["/app/migrate", "up"]
backoffLimit: 3
Key points:
helm.sh/hook: pre-install— runs after templates render but before any workload resources (Deployments, StatefulSets) are created.helm.sh/hook-delete-policy: hook-succeeded— removes the job after it completes successfully, keeping the namespace clean.helm.sh/hook-weight— optional; hooks with lower weights run first when multiple hooks share the same event.restartPolicy: OnFailure— required for Jobs;Neveralso works butOnFailurelets the job controller retry on transient errors.backoffLimit: 3— caps retries; after 3 failures the job is marked failed and Helm aborts the install.
To verify the hook renders in the correct order, run from your chart directory (requires helm CLI, no cluster needed):
helm template myrelease ./mychart --namespace production
Check that the Job appears before the Deployment in the output. For a dry-run against a cluster (requires cluster-admin or namespace-scoped create permissions on Jobs and your workload resources):
helm install --dry-run --debug myrelease ./mychart --namespace production
After a real install, confirm execution:
kubectl get jobs -n production -l "helm.sh/hook=pre-install"
helm get manifest myrelease -n production
The first command shows job status (COMPLETIONS should be 1/1). The second shows the full rendered manifest including the hook.
Trade-Offs and Limitations
Hooks don't roll back. If you helm rollback to a previous release, the migration job from the failed release stays completed. You must design migrations to be forward-compatible or manually revert schema changes.
Hanging hooks block operations. Helm waits for hook readiness. A job stuck in Pending (e.g., image pull error, missing secret, resource quota) blocks helm install or helm upgrade indefinitely. Set activeDeadlineSeconds on the job spec as a safety net:
spec:
activeDeadlineSeconds: 600
template:
spec:
# ...
Idempotency is your responsibility. Upgrades re-run pre-upgrade and post-upgrade hooks. If you also use pre-install for migrations, a failed upgrade followed by a retry runs the migration twice. Use a migration tool that tracks applied versions (e.g., Flyway, Liquibase, golang-migrate) rather than raw SQL scripts.
Namespace scoping. In Helm 3, releasing the same chart in two namespaces creates two independent hook jobs. That's usually what you want, but be aware when debugging—kubectl get jobs -A shows both.
When to Choose Hooks Over Init Containers
Use a pre-install/pre-upgrade hook job when:
- The operation must run exactly once per release (schema migration, seed data, one-time API registration).
- It should block the release until complete (application pods shouldn't start until schema exists).
- You can tolerate the hook not being rolled back automatically.
Stick with init containers when:
- The setup is safe to repeat on every pod start (cache warm-up, config generation, waiting for a dependency).
- You need the setup to run per-pod, not per-release.
- You want the setup to be part of the pod's lifecycle and automatically retried on restart.
For the migration case, the hook approach is the better default. Just add activeDeadlineSeconds, use a versioned migration tool, and verify with helm template before every deploy.