Getting started with Go and Docker for an elegant Gopher Gala

Jan 23, 2015

This weekend is the Gopher Gala the world’s first Go(lang) distributed hackathon. As Golang is our primary and beloved language we figured it’s a perfect timing to show how easy it is to deploy Go code with Docker and a free account on Giant Swarm.


The example


If you have read a bit through our documentation, you know our famous CurrentWeather tutorial using NodeJS. Here is the adaption for Go. Find the source on GitHub:

The example queries the Openweather API and, to be a good citizen, caches the data in a Redis cache:


 

Have a look at the main.go file for the application code.


Starting it locally


The great thing about Go is that you can create small self-contained independent binaries and deploy them with small Docker containers. We are using the official busybox image with just 2.5 MB in size. Together with our application logic the whole image is only 12 MB!

To build and link the Docker containers we leverage docker-compose:

$ docker-compose up

If you want to make sure the image is built freshly, use make docker-build before.


Starting it on Giant Swarm


To deploy and run it on Giant Swarm is just as easy. We made a Makefile target:

$ make swarm-up

which uploads your custom Docker image and starts your application with your custom container and redis container. Wait till it reports with up and head over to http://currentweather-demo.gigantic.io/. Done!

To find out more about that check out our Docs especially the possible configurations in our swarm.json.

{
  "name": "currentweather",
  "components": {
    "webserver": {
      "image": "registry.giantswarm.io/$GIANTSWARM_LOGIN_NAME/currentweather-go:latest",
      "ports": [8080],
      "links": [
        {
          "component": "redis",
          "target_port": 6379
        }
      ],
      "domains": {
        "8080": "currentweather-$GIANTSWARM_LOGIN_NAME.gigantic.io"
      }
    },
    "redis": {
      "image": "redis:latest",
      "ports": [6379]
    }
  }
}

Summary


Building small self-contained apps has never been easier thanks to Docker and Golang. We at Giant Swarm try to make the next steps just as easy.

So whether you are taking part at the Gopher Gala or just want to give Go a try: check out our example and start programming Go. And while your at it request your free invite and deploy and run your application in the cloud within minutes.

You May Also Like

These Stories on Docker

Mar 12, 2015
Mar 2, 2015
Feb 23, 2015