Hosting a Go Gin web app with Serverless Containers
Gin (External link) is a lightweight, high-performance web framework for Go, ideal for building fast APIs and web applications with minimal overhead. Hosting Gin apps on Scaleway Serverless Containers provides automatic scaling, simplified deployment, and reduced infrastructure management. This deployment method enables your application to efficiently handle varying workloads, optimize resource usage, and lets you focus on writing code and building features rather than maintaining servers.
Before you start
To complete the following actions, you must have:
- A Scaleway account logged into the console (External link)
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- Installed Docker (External link) or Docker Engine (External link)
- Created a Scaleway Container Registry namespace and logged into it
- Installed go 1.23 or newer (External link)
Create and host a basic Go Gin web application
To host a Go (External link) web application using the Gin framework (External link) on Scaleway Serverless Containers, you need to create a simple Go project, define HTTP handlers, and containerize it using a production-ready Dockerfile. After building the container image locally, push it to the Scaleway Container Registry and deploy it via Scaleway Serverless Containers.
Gin is a lightweight, high-performance web framework for Go, ideal for building fast APIs and web apps with minimal overhead.
Create your app and test it locally
-
In a terminal, create a new directory and navigate into it:
Copymkdir my-gin-app cd my-gin-app -
Initialize a Go module:
Copygo mod init my-gin-app -
Create a
main.gofile, then add the code below to it to create a basic Gin server:Copypackage main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.LoadHTMLGlob("templates/*") r.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", nil) }) r.Run("0.0.0.0:8080") } -
Create a
templatesfolder to store HTTP files.Copymkdir templates -
Create an
index.htmlfile in thetemplatesfolder, then add the code below to it:Copy<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Hello</title> <style> body { font-family: sans-serif; text-align: center; margin: 0; padding: 100px 20px; background: #fafafa; color: #333; } </style> </head> <body> <h1>Hello from Gin 🌿</h1> <p>A minimal page, fast and clean, running on Scaleway Serverless Containers.</p> </body> </html> -
Run the following command to install the Gin package and tidy dependencies:
Copygo get -u github.com/gin-gonic/gin go mod tidy -
Test the app locally:
Copygo run main.goVisit http://localhost:8080 (External link) to see the homepage of your web app.
Build the app image and push it to Scaleway Container Registry
Before building and pushing your image, ensure you have created a Scaleway Container Registry namespace, and logged into it with the Docker CLI.
-
Create a
Dockerfileat the root of your project:Copy# Build stage FROM golang:1.25-alpine AS builder # Set working directory WORKDIR /app # Copy go mod files COPY go.mod go.sum ./ RUN go mod download # Copy source code COPY . . # Build the Go binary (statically linked) RUN CGO_ENABLED=0 GOOS=linux go build -o main . # Final stage: minimal image FROM scratch # Set working directory WORKDIR /root/ # Copy binary from builder stage COPY /app/main . # Copy templates folder COPY templates/ ./templates/ # Expose port EXPOSE 8080 # Run the binary CMD ["./main"] -
Build, tag, and push the image to Scaleway Container Registry:
Copydocker build \ --platform linux/amd64 \ --push \ -t <CONTAINER_REGISTRY_ENDPOINT>/my-gin-app:latest .
Deploy your app using Serverless Containers
-
Deploy a Serverless Container with the following settings:
- Registry: Scaleway Container Registry
- Registry namespace: The namespace where you pushed your image
- Container port:
8080(matches the port in the Go app and Dockerfile) - Resources:
250 mVCPUand256 MBmemory (Go apps are lightweight) - Autoscaling: Set minimum scale to
1to avoid cold starts (optional)
Deployment may take up to a minute.
-
Once the container is ready, click the container endpoint in the Overview tab. Your Gin app will be live and accessible to anyone with the link.
Going further
- You can deploy an existing Go Gin project by building and pushing its container image as shown above.
- Add a custom domain to your deployed app.
Visit our Help Center and find the answers to your most frequent questions.
Visit Help Center