layout | title | categories | tags | lang | author |
---|---|---|---|---|---|
post |
Running your Go application on Sailabove |
docker |
guide, getting-started, go |
en |
yadutaf |
Go is quite recent, modern, high performance language developed by Google. It emphases on security, performance and concurrent execution. It first focused on system development like C and C++ while bringing a leaner and richer syntax. In became a prominent technology in just a few months. Before Docker, it was the first language to bring a solution to the dependency hell as it links statically all its dependencies by default.
To just give a taste of how powerful it is: this is the technology behind Docker itself!
Even though it is still a young project, it already have a huge standard and contributed library thanks to its huge community of enthusiast. Moreover, any project, from any standard versioning system (Git, Mercurial, SVN) can be installed as a package, directly from the repository.
Before diving in, make sure to read the Getting Started guide.
- Write an awesome application ===============================
Let's do something unique. Like, say, an "Hello Docker" application. In a hurry? You can already see the result on Sailabove: http://hello-go.demo.app.sailabove.io/hello/Docker-Fan
For this example, we'll use the default net/http
package.
Here is the full server.go
source code:
package main
import (
"net/http"
"fmt"
"html"
"log"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
remPartOfURL := r.URL.Path[len("/hello/"):]
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "Hello <b>%s</b>!", html.EscapeString(remPartOfURL))
}
func main() {
http.HandleFunc("/hello/", helloHandler)
log.Fatal(http.ListenAndServe("0.0.0.0:80", nil))
}
All dependencies, even external one, are declared in the import
statement.
- Dockerize it ===============
Go is a Docker first class citizen. It has its own dedicated official image supporting Go 1.2 and 1.3. For this example, we'll use the later.
Here is our Dockerfile
:
FROM golang:1.3-onbuild
EXPOSE 80
CMD [ "/go/bin/app" ]
It first instructs Docker to get Go 1.3
base image with automatic build support (-onbuild
). To use Go 1.2 base image instead, you may choose to use FROM go:1.2-onbuild
for instance.
We then declare the PORT
our application will listen on. In our case, standard HTTP port.
Lastly, it instructs Docker the CMD
to launch our application.
OK, let's build and test it locally:
docker build -t hello-go .
docker run -it --publish 8080:80 --rm -t hello-go
Check if all works fine, visit http://localhost:8080/hello/Docker-Fan. You should see something like "Hello Docker-Fan!".
It works on your development machine. It will work on production.
- Go live! ===========
Let's go: tag
our application so that Docker knows where to prod it:
# docker tag <local app name> sailabove.io/<user name>/<app name>
docker tag hello-go sailabove.io/demo/hello-go
Push your application on Sailabove's private Docker registry:
# docker push <previously created tag>
docker push sailabove.io/demo/hello-go
Launch it using previously installed sail
command line and instruct Sailabove to run it with unprivileged user nobody
for increased security. Please note that, even when running as regular unprivileged user, your application can freely listen on any port, 80
in this case.
# sail services add <user name>/<app name> <service name>
sail services add demo/hello-go hello-go
Eager to see the result? Wait a few seconds. It is now running live on http://hello-go.demo.app.sailabove.io/hello/Docker-Fan. This is http://<service name>.<user name>.app.sailabove.io
.
Enjoy!
- Get started: Getting started quide
- Documentation: Reference documentation, Guides
- Join OVH Docker mailing list: [email protected]
- Visit our Community: https://community.runabove.com/
- Drop us an e-mail: [email protected]
- Create your account: Sailabove.com