Skip to main content

go

In this walk through we will package a simple go app and run it on Acorn.

Example

Lets take a simple Go app that starts a webserver and returns a message. Here is all the code for our app put it in the main.go file.

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
html := "<html><body><h1>Hello, From Acorn!</h1></body></html>"
fmt.Fprint(w, html)
})

fmt.Println("Starting server on 0.0.0.0:8080")

http.ListenAndServe(":8080", nil)
}

The above simply code simply starts an HTTP server listening on port 8080 and returns a simple HTML message.

You will also need a simple go.mod file to define the module name and version.

module example/app

go 1.21.5

Building the app

As with most Golang applications we simply need to run go build to create our applications binary. If you have Golang installed on your local machine you can run the go build command and run the resulting app binary. Doing so will let you connect on localhost:8080 and see the message. You can ctrl-c to stop the server.

Package the app

Create the Dockerfile

In order to run our app on Acorn, we first need to containerize it. To do this we will create a Dockerfile. The purpose of the Dockefile is to specify the steps needed to build our application and what commands need to be run to start it.

Here is a our first pass at the Dockerfile:

FROM golang:1.21-alpine

WORKDIR /work
COPY . .
RUN go build

EXPOSE 8080

CMD ["/work/app"]

The above Dockerfile starts with the official Golang image and sets the working directory to /work. Which means everything we do from this point on will be relative to that directory. Next we copy all the files from our local directory into the container. Then we run go build to build our application. We also expose port 8080 because that is where our server is going to run. Finally we set the command to run when the container starts to be app which is the name of our binary.

Create the Acornfile

Now create an Acornfile in the same directory as our Dockerfile and source code.

containers: app: {
build: context: "."
ports: publish: "8080/http"
}

This file tells Acorn that we want to build the app container from the Dockerfile in the current directory. It also tells Acorn that we want to publish the port 8080 as an HTTP endpoint.

Run the app

Now we are ready to run the App. To do so, we run:

acorn run -n go-app .
[+] Building 25.7s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.2s
...
STATUS: ENDPOINTS[] HEALTHY[] UPTODATE[]
STATUS: ENDPOINTS[] HEALTHY[0] UPTODATE[0] waiting for quota allocation; (container: app): pending
STATUS: ENDPOINTS[] HEALTHY[0] UPTODATE[0] (container: app): pending
STATUS: ENDPOINTS[] HEALTHY[0/1] UPTODATE[0] (container: app): not ready
STATUS: ENDPOINTS[https://go-app-790331ce.zvgz4d.on-acorn.io] HEALTHY[0/1] UPTODATE[1] (container: app): ContainerCreating
STATUS: ENDPOINTS[https://go-app-790331ce.zvgz4d.on-acorn.io] HEALTHY[0/1] UPTODATE[1] (container: app): not ready
┌─────────────────────────────────────────────────────────────────────────────────────────┐
| STATUS: ENDPOINTS[https://go-app-790331ce.zvgz4d.on-acorn.io] HEALTHY[1] UPTODATE[1] OK |
└─────────────────────────────────────────────────────────────────────────────────────────┘

We can click on the ENDPOINTS link to see our app running. You can also run acorn ps to see the status of your app.

Wrapping up

You have now launched your first Go app on Acorn. You can follow these steps to launch your own Go apps as well.