Nov 17, 20266 min read

Deploy a Fiber App to Google Cloud Run

Run a Go Fiber v3 API on Cloud Run with Cloud SQL in your own GCP account, with Encore or with a Dockerfile, the Cloud SQL connector and gcloud by hand.

Illustration for Deploy a Fiber App to Google Cloud Run

A Fiber v3 API compiles to one Go binary, and Cloud Run is a natural place to run that container because it scales the service and takes HTTPS off your hands.

The parts around the container still have to be built: Artifact Registry, a service account, a Cloud SQL instance the internet cannot reach, a connection from Cloud Run into that instance, secrets, migrations and a health check on PORT.

This guide deploys a Fiber v3 RSVP API that stores replies in Postgres. It starts with Encore, which provisions Cloud Run and Cloud SQL in your own GCP account from the Go code, then shows a Dockerfile and gcloud run deploy with the Cloud SQL Go connector.

Ways to deploy Fiber to Cloud Run at a glance

EncoreDocker + gcloud by hand
ContainerBuilt on deployYour Dockerfile, pushed to Artifact Registry
ComputeCloud Run, when you choose it as the compute platformgcloud run deploy
PostgresCloud SQL, declared with sqldb.NewDatabaseCloud SQL you provision
Local PostgresStarted by encore runDocker Compose or Cloud SQL Auth Proxy
Service identityEncore provisions an IAM role for the service, or a service account on GCPA service account you attach to the Cloud Run service
CloudYour GCP accountYour GCP account

How do you deploy Fiber to Cloud Run with Encore?

Encore

Encore works alongside Fiber and deploys it to Cloud Run in your own GCP account together with the database the Go code declares. The Fiber app keeps every route and sits behind a raw fallback route, and the database is a package-level declaration next to the handlers that use it.

A coding agent working in this repository can add the Fiber routes and the sqldb.NewDatabase declaration in the same change, then use encore run to exercise the RSVP flow against local Postgres before the first deploy.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create rsvp-api --example=hello-world$ cd rsvp-api && go get github.com/gofiber/fiber/v3@v3.7.0$ mkdir -p rsvp/migrations
-- rsvp/migrations/1_replies.up.sql CREATE TABLE replies ( id BIGSERIAL PRIMARY KEY, event_id TEXT NOT NULL, email TEXT NOT NULL, attending BOOLEAN NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), UNIQUE (event_id, email) );
// rsvp/rsvp.go package rsvp import ( "encoding/json" "net/http" "strings" "encore.dev/storage/sqldb" "encore.dev/storage/sqldb/sqlerr" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/adaptor" ) var db = sqldb.NewDatabase("rsvp", sqldb.DatabaseConfig{ Migrations: "./migrations", }) //encore:service type Service struct { handler http.HandlerFunc } func initService() (*Service, error) { app := fiber.New() app.Get("/healthz", func(c fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) }) app.Post("/events/:id/replies", createReply) app.Get("/events/:id/replies", listReplies) return &Service{handler: adaptor.FiberApp(app)}, nil } //encore:api public raw path=/!fallback func (s *Service) Fallback(w http.ResponseWriter, req *http.Request) { s.handler(w, req) } func createReply(c fiber.Ctx) error { eventID := c.Params("id") var in struct { Email string `json:"email"` Attending bool `json:"attending"` } if err := c.Bind().Body(&in); err != nil || !strings.Contains(in.Email, "@") || eventID == "" { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "email and event id are required"}) } var id int64 err := db.QueryRow(c.Context(), ` INSERT INTO replies (event_id, email, attending) VALUES ($1, $2, $3) RETURNING id`, eventID, in.Email, in.Attending).Scan(&id) if sqldb.ErrCode(err) == sqlerr.UniqueViolation { return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "already replied"}) } else if err != nil { return err } return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) } func listReplies(c fiber.Ctx) error { // Public listing returns counts only. Organizer email access belongs on an authenticated route. var attending, declined int err := db.QueryRow(c.Context(), ` SELECT count(*) FILTER (WHERE attending) AS attending, count(*) FILTER (WHERE NOT attending) AS declined FROM replies WHERE event_id = $1`, c.Params("id")).Scan(&attending, &declined) if err != nil { return err } return c.JSON(fiber.Map{"attending": attending, "declined": declined}) }

encore run starts Postgres locally, applies the migration and serves the Fiber routes, so you can exercise the same handlers before you deploy.

Deploy to your GCP account

In the Encore dashboard, go to App Settings > Integrations > Connect Cloud and connect your GCP account, as described in Connect your cloud account. Create an environment that uses Cloud Run as the compute platform, then push:

~
$ git add -A && git commit -m "RSVP API" && git push encore

Encore builds the image and provisions Cloud Run and Cloud SQL for PostgreSQL in your account, as the GCP infrastructure docs describe. Encore provisions an IAM role for the service, or a service account on GCP. Migrations run through a proxy inside your environment.

Best fit: a Fiber API that should run on Cloud Run with Cloud SQL in your own GCP account.

Docker, the Cloud SQL connector and gcloud by hand

The hand-built path is a non-root image that listens on PORT, a Cloud SQL instance, a service account and gcloud run deploy. The Cloud SQL Go connector opens a TLS connection from the container using the instance connection name, which is the usual path when the Cloud Run service and the instance are in the same project.

# Dockerfile FROM golang:1.26-bookworm AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /out/rsvp ./cmd/api FROM debian:bookworm-slim RUN useradd --system --uid 10001 appuser COPY --from=build /out/rsvp /usr/local/bin/rsvp USER 10001 EXPOSE 8080 ENTRYPOINT ["/usr/local/bin/rsvp"]
// cmd/api/main.go package main import ( "context" "log" "net" "os" "cloud.google.com/go/cloudsqlconn" "github.com/gofiber/fiber/v3" "github.com/jackc/pgx/v5/pgxpool" ) func main() { port := os.Getenv("PORT") if port == "" { port = "8080" } dsn := os.Getenv("DATABASE_URL") cfg, err := pgxpool.ParseConfig(dsn) if err != nil { log.Fatal(err) } d, err := cloudsqlconn.NewDialer(context.Background()) if err != nil { log.Fatal(err) } cfg.ConnConfig.DialFunc = func(ctx context.Context, _, _ string) (net.Conn, error) { return d.Dial(ctx, os.Getenv("INSTANCE_CONNECTION_NAME")) } pool, err := pgxpool.NewWithConfig(context.Background(), cfg) if err != nil { log.Fatal(err) } defer pool.Close() app := fiber.New() app.Get("/healthz", func(c fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) }) if err := app.Listen("0.0.0.0:" + port); err != nil { log.Fatal(err) } }

Deploy with a service account that has roles/cloudsql.client and roles/cloudsql.instanceUser as described in Connect from Cloud Run. Bind Fiber to 0.0.0.0:$PORT, and run migrations from a one-off job or Cloud Run job against the same instance before shifting traffic to a new revision.

Best fit: teams that already manage Cloud Run and Cloud SQL in Terraform or gcloud and want the Fiber process to stay a plain binary.

How to choose

Pick Encore when you want Fiber to stay as it is and get Cloud Run and Cloud SQL provisioned in your own GCP account from the API code, with the same database under encore run. Pick a Dockerfile and gcloud when the team already owns those resources and wants to keep the deploy pipeline in Cloud Build or Terraform.

The health check and the listen address do not depend on the choice: bind to PORT and expose a cheap GET /healthz.

Frequently asked questions

How do I deploy a Fiber app to Cloud Run?

Build a container that listens on the PORT environment variable, push the image to Artifact Registry and deploy it with gcloud run deploy. For Postgres, add a Cloud SQL instance and connect from Cloud Run with the Cloud SQL Auth Proxy sidecar or the Cloud SQL Go connector, using a service account that can reach the instance.

How do I deploy Fiber to Cloud Run with Encore?

Mount the Fiber app in a raw endpoint with path=/!fallback through adaptor.FiberApp, declare the database with sqldb.NewDatabase, connect your GCP account in the Encore dashboard and create an environment with Cloud Run as the compute. Encore builds the image and provisions Cloud Run and Cloud SQL in your own account.

What port should a Fiber app listen on in Cloud Run?

Cloud Run sets the PORT environment variable and sends traffic to that port. Bind Fiber to 0.0.0.0 and that port, not to a hard-coded 3000, and point the health check at a cheap route such as GET /healthz.

How does Cloud Run reach Cloud SQL?

The usual path is the Cloud SQL Auth Proxy or the Cloud SQL Go connector, which open a TLS connection using the instance connection name and a service account. Direct IP access needs a VPC path such as Direct VPC egress or a connector, and the instance should not be opened to the public internet.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders