Skip to navigationSkip to main contentSkip to footerScaleway DocsSparklesIconAsk our AI
SparklesIconAsk our AI

Hosting a Go Gin web app with Serverless Containers

deployhostwebsitewebappgogingolangcontainerizeapplicationdockerdockerfile

Gin 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 actions presented below, you must have:

Create and host a basic Go Gin web application

To host a Go web application using the Gin framework 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

  1. In a terminal, create a new directory and navigate into it:

    mkdir my-gin-app
    cd my-gin-app
  2. Initialize a Go module:

    go mod init my-gin-app
  3. Create a main.go file, then add the code below to it to create a basic Gin server:

    package 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")
    }
  4. Create a templates folder to store HTTP files.

    mkdir templates
  5. Create an index.html file in the templates folder, then add the code below to it:

    <!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>
  6. Run the following command to install the Gin package and tidy dependencies:

    go get -u github.com/gin-gonic/gin
    go mod tidy
  7. Test the app locally:

    go run main.go

    Visit http://localhost:8080 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.

  1. Create a Dockerfile at the root of your project:

    # 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 --from=builder /app/main .
    
    # Copy templates folder
    COPY templates/ ./templates/
    
    # Expose port
    EXPOSE 8080
    
    # Run the binary
    CMD ["./main"]
    InformationOutlineIcon
    Note

    Why multi-stage builds? Multi-stage builds create smaller, more secure images by separating the build environment from the runtime image. The final image contains only your compiled binary and templates; no Go compiler, build tools, or unnecessary dependencies. Using scratch (an empty base image) results in faster deployments (lower cold starts), lower storage costs, and a reduced attack surface.

  2. Build, tag, and push the image to Scaleway Container Registry:

    docker build \
      --platform linux/amd64 \
      --push \
      -t <CONTAINER_REGISTRY_ENDPOINT>/my-gin-app:latest .
    InformationOutlineIcon
    Note

    You can find your Container Registry endpoint in the Settings tab of your Container Registry namespace in the Scaleway console.

Deploy your app using Serverless Containers

  1. 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 mVCPU and 256 MB memory (Go apps are lightweight)
    • Autoscaling: Set minimum scale to 1 to avoid cold starts (optional)

    Deployment may take up to a minute.

  2. 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.
Questions?

Visit our Help Center and find the answers to your most frequent questions.

Visit Help CenterArrowRightIcon
SearchIcon
No Results