|
| 1 | +--- |
| 2 | +title: Hosting a Go Gin web app with Serverless Containers |
| 3 | +description: This page provides guidelines on how to host and deploy a Go web application using the Gin framework with Scaleway Serverless Containers |
| 4 | +tags: deploy host website webapp go gin golang containerize application docker dockerfile |
| 5 | +products: |
| 6 | + - containers |
| 7 | + - container-registry |
| 8 | +dates: |
| 9 | + validation: 2025-10-06 |
| 10 | + posted: 2025-10-06 |
| 11 | + validation_frequency: 12 |
| 12 | +difficulty: beginner |
| 13 | +usecase: |
| 14 | + - application-hostor |
| 15 | + - deploy-external-software |
| 16 | + - website-hosting |
| 17 | +ecosystem: |
| 18 | + - third-party |
| 19 | +--- |
| 20 | +import Requirements from '@macros/iam/requirements.mdx' |
| 21 | +<Requirements /> |
| 22 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 23 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 24 | +- Installed [Docker](https://docs.docker.com/get-started/get-docker/) or [Docker Engine](https://docs.docker.com/engine/install/) |
| 25 | +- [Created a Scaleway Container Registry namespace](/container-registry/how-to/create-namespace/) and [logged into it](/container-registry/how-to/connect-docker-cli/) |
| 26 | +``` |
| 27 | +
|
| 28 | +## Create and host a basic Go Gin web application |
| 29 | +
|
| 30 | +To host a [Go](https://go.dev/) web application using the [Gin framework](https://gin-gonic.com/) 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](/container-registry/) and deploy it via [Scaleway Serverless Containers](/serverless-containers/). |
| 31 | +
|
| 32 | +Gin is a lightweight, high-performance web framework for Go, ideal for building fast APIs and web apps with minimal overhead. |
| 33 | +
|
| 34 | +### Create your app and test it locally |
| 35 | +
|
| 36 | +1. In a terminal, create a new directory and navigate into it: |
| 37 | + ```bash |
| 38 | + mkdir my-gin-app |
| 39 | + cd my-gin-app |
| 40 | + ``` |
| 41 | + |
| 42 | +2. Initialize a Go module: |
| 43 | + ```bash |
| 44 | + go mod init my-gin-app |
| 45 | + ``` |
| 46 | + |
| 47 | +3. Create a `main.go` file with a basic Gin server: |
| 48 | + ```go |
| 49 | + package main |
| 50 | + |
| 51 | + import "github.com/gin-gonic/gin" |
| 52 | + |
| 53 | + func main() { |
| 54 | + r := gin.Default() |
| 55 | + |
| 56 | + // Define a simple route |
| 57 | + r.GET("/", func(c *gin.Context) { |
| 58 | + c.JSON(200, gin.H{ |
| 59 | + "message": "Hello from Gin on Scaleway!", |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + // Start server on 0.0.0.0:8080 |
| 64 | + r.Run("0.0.0.0:8080") |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +4. Install Gin: |
| 69 | + ```bash |
| 70 | + go get -u github.com/gin-gonic/gin |
| 71 | + ``` |
| 72 | + |
| 73 | +5. Test the app locally: |
| 74 | + ```bash |
| 75 | + go run main.go |
| 76 | + ``` |
| 77 | + Visit [http://localhost:8080](http://localhost:8080) to see the JSON response. |
| 78 | + |
| 79 | +6. Your project structure should now look like: |
| 80 | + ``` |
| 81 | + my-gin-app/ |
| 82 | + ├── main.go |
| 83 | + ├── go.mod |
| 84 | + ├── go.sum |
| 85 | + └── (optional) Dockerfile |
| 86 | + ``` |
| 87 | + |
| 88 | +### Build the app image and push it to Scaleway Container Registry |
| 89 | + |
| 90 | +Before building and pushing your image, ensure you have: |
| 91 | +- [Created a Scaleway Container Registry namespace](/container-registry/how-to/create-namespace/) |
| 92 | +- [Logged into it with Docker CLI](/container-registry/how-to/connect-docker-cli/) |
| 93 | + |
| 94 | +1. Create a `Dockerfile` at the root of your project: |
| 95 | + ```dockerfile |
| 96 | + # Use a lightweight Go image for building |
| 97 | + FROM golang:1.22-alpine AS builder |
| 98 | + |
| 99 | + # Set working directory |
| 100 | + WORKDIR /app |
| 101 | + |
| 102 | + # Copy go mod files |
| 103 | + COPY go.mod go.sum ./ |
| 104 | + RUN go mod download |
| 105 | + |
| 106 | + # Copy source code |
| 107 | + COPY . . |
| 108 | + |
| 109 | + # Build the Go binary |
| 110 | + RUN go build -o main . |
| 111 | + |
| 112 | + # Final stage: minimal image |
| 113 | + FROM alpine:latest |
| 114 | + |
| 115 | + # Install CA certificates |
| 116 | + RUN apk --no-cache add ca-certificates |
| 117 | + |
| 118 | + # Set working directory |
| 119 | + WORKDIR /root/ |
| 120 | + |
| 121 | + # Copy binary from builder stage |
| 122 | + COPY --from=builder /app/main . |
| 123 | + |
| 124 | + # Expose port |
| 125 | + EXPOSE 8080 |
| 126 | + |
| 127 | + # Run the binary |
| 128 | + CMD ["./main"] |
| 129 | + ``` |
| 130 | + |
| 131 | +2. Build, tag, and push the image to Scaleway Container Registry: |
| 132 | + ```bash |
| 133 | + docker build \ |
| 134 | + --platform linux/amd64 \ |
| 135 | + --push \ |
| 136 | + -t <CONTAINER_REGISTRY_ENDPOINT>/my-gin-app:latest . |
| 137 | + ``` |
| 138 | + |
| 139 | + <Message type="note"> |
| 140 | + You can find your Container Registry endpoint in the **Settings** tab of your Container Registry namespace in the [Scaleway console](https://console.scaleway.com/registry/namespaces). |
| 141 | + </Message> |
| 142 | + |
| 143 | +### Deploy your app using Serverless Containers |
| 144 | + |
| 145 | +1. [Deploy a Serverless Container](/serverless-containers/how-to/deploy-container/) with the following settings: |
| 146 | + - **Registry**: Scaleway Container Registry |
| 147 | + - **Registry namespace**: The namespace where you pushed your image |
| 148 | + - **Container port**: `8080` (matches the port in the Go app and Dockerfile) |
| 149 | + - **Resources**: `256 mVCPU` and `256 MB` memory (Go apps are lightweight) |
| 150 | + - **Autoscaling**: Set minimum scale to `1` to avoid [cold starts](/serverless-containers/concepts/#cold-start) (optional) |
| 151 | + |
| 152 | + Deployment may take up to a minute. |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | +## Going further |
| 157 | + |
| 158 | +- You can deploy an existing Go Gin project by building and pushing its container image as shown above. |
| 159 | +- [Add a custom domain](/serverless-containers/how-to/add-a-custom-domain-to-a-container/) to your deployed app. |
| 160 | +- Secure your app with HTTPS — Scaleway automatically provides TLS for container endpoints. |
0 commit comments