Deploy a Go App

This guide explains how to deploy a Go application on Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the Go application from this guide using the Deploy to Koyeb button below.

Deploy to Koyeb (opens in a new tab)

You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.

Create the Go app

Get started by creating a minimalistic Go application that we will deploy on Koyeb. You will need Go (opens in a new tab) installed on your machine.

In your terminal, run the following commands to create the directory that will hold the application code:

mkdir example-go
cd example-go

Koyeb detects Go applications when one of the Go matching criteria is met. In this guide, we will use Go module to trigger the detection.

During deployment, Koyeb will automatically deploy using the version of Go specified in your dependency manager's metadata file or, in some cases, the GOVERSION environment variable. Check the page on building Go projects to learn more.

Run the go mod init command with your module path. Here, we use github.com/koyeb/example-golang. When you publish a module, this must be a path from which your module can be downloaded by Go tools, such as your code's repository.

$ go mod init github.com/koyeb/example-golang
go: creating new go.mod: module github.com/koyeb/example-golang

Create a new file called main.go and copy the following code into it:

main.go
package main
 
import (
	"fmt"
	"log"
	"net/http"
	"os"
)
 
func main() {
	port := os.Getenv("PORT")
 
	if port == "" {
		port = "8080"
	}
 
	http.HandleFunc("/", HelloHandler)
 
	log.Println("Listening on port", port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
 
func HelloHandler(w http.ResponseWriter, _ *http.Request) {
	fmt.Fprintf(w, "Hello from Koyeb")
}

The code above runs an HTTP server using the standard net/http library and responds to GET / requests with Hello from Koyeb.

Run the Go app locally

Launch the application locally to make sure everything is running as expected.

go run main.go

You can now access the application at http://localhost:8080.

Push the project to GitHub

In the project directory, initialize a new git repository by running the following command:

git init

We will use this repository to version the application code and push the changes to a GitHub repository. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:

git add .
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git push -u origin main

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

To deploy the Go app on Koyeb, using the control panel (opens in a new tab) follow the steps below:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public Go example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page: https://github.com/koyeb/example-golang.
  4. Name your App and Service, for example go-service.
  5. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb using a pre-built container

As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.

To dockerize the Go application, create a Dockerfile in your project root directory and copy the content below:

Dockerfile
FROM golang:1.19-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o ./example-golang ./main.go
 
 
FROM alpine:latest AS runner
WORKDIR /app
COPY --from=builder /app/example-golang .
EXPOSE 8080
ENTRYPOINT ["./example-golang"]

The Dockerfile above provides the minimum requirements to run the Go application. You can easily extend it depending on your needs.

To build and push the Docker image to a registry and deploy it on Koyeb, refer to the page on deploying pre-built container images.