vxcontrol/pentagi · error

failed to create container in database: %w

Error message

failed to create container in database: %w

What it means

RunContainer first persists a container row (status Starting, placeholder LocalID tmp-id-<flowID>) via dc.db.CreateContainer before touching the daemon. If the insert fails, the error is wrapped as "failed to create container in database: %w" and no Docker container is created. This wraps a PostgreSQL/SQLC error — inspect the %w cause for the real reason.

Source

Thrown at backend/pkg/docker/client.go:255

		"name":     containerName,
		"type":     containerType,
		"flow_id":  flowID,
		"work_dir": workDir,
		"host_dir": hostDir,
	})
	logger.Info("running container")

	dbContainer, err := dc.db.CreateContainer(ctx, database.CreateContainerParams{
		Type:     containerType,
		Name:     containerName,
		Image:    config.Image,
		Status:   database.ContainerStatusStarting,
		FlowID:   flowID,
		LocalID:  database.StringToNullString(fmt.Sprintf("tmp-id-%d", flowID)),
		LocalDir: database.StringToNullString(hostDir),
	})
	if err != nil {
		return database.Container{}, fmt.Errorf("failed to create container in database: %w", err)
	}

	updateContainerInfo := func(status database.ContainerStatus, localID string) {
		dbContainer, err = dc.db.UpdateContainerStatusLocalID(ctx, database.UpdateContainerStatusLocalIDParams{
			Status:  status,
			LocalID: database.StringToNullString(localID),
			ID:      dbContainer.ID,
		})
		if err != nil {
			logger.WithError(err).Error("failed to update container info in database")
		}
	}

	fallbackDockerImage := func() error {
		logger = logger.WithField("image", dc.defImage)
		logger.Warn("try to use default image")
		config.Image = dc.defImage

View on GitHub (pinned to ea665308ba)

Solutions

  1. Read the wrapped cause in the error chain (database is down vs constraint violation) — check Postgres connectivity first (docker compose ps, pg_isready).
  2. If unique-constraint on name: delete or reset the stale container row for that name/flow before retrying.
  3. Verify the flow exists (SELECT from flows WHERE id=<flowID>) before running a container for it.
  4. Run pending migrations (goose) if the schema predates new columns; check application DB logs for the exact constraint error.

Example fix

-- before: retry fails forever on stale row
SELECT * FROM containers WHERE name='<name>' AND flow_id=42;
-- after
delete from containers where name='<name>' and flow_id=42 and status='starting';
Defensive patterns

Strategy: try-catch

Validate before calling

// Before running the container, verify the flow and name are free:
if _, err := db.GetFlow(ctx, flowID); err != nil {
    return fmt.Errorf("flow %d not found: %w", flowID, err)
}
// optionally delete stale 'starting' rows with the same name

Try / catch

dbContainer, err := dc.db.CreateContainer(ctx, params)
if err != nil {
    var pqe *pgconn.PgError
    if errors.As(err, &pqe) {
        switch pqe.Code {
        case "23505": // unique_violation → stale row, safe to clean+retry
            return retryAfterCleanup()
        case "23503": // foreign_key_violation → flow missing
            return fmt.Errorf("flow %d does not exist", flowID)
        default:
            return fmt.Errorf("db insert failed (%s): %w", pqe.Code, err)
        }
    }
    return err // connectivity: rely on pool/retry middleware
}

Prevention

When it happens

Trigger: dc.db.CreateContainer fails: database unreachable, FK violation (flow_id does not exist in flows table), unique constraint on container name (name reused for the same flow), NOT NULL violation, or connection pool exhausted.

Common situations: Postgres restarted or max_connections exhausted during a heavy run; a container name collides with a row left by a crashed earlier attempt; caller passes a flowID that was never created; DB migrations not applied so the containers table is missing columns.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/b13feb6060f734b7. Report an issue: GitHub.