Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
Go

How to dockerize a simple Go app

In this tutorial, I’m going to show you how to dockerize a simple app (written in Go) that shows the current time. Github repo is here, in case you’re interested.

Dockerfile

So, if you haven’t played with Docker before, official docs would be a good start.

For our Go app, we need to create a file named Dockerfile with the following content:

FROM golang:onbuild
EXPOSE 8080

Go code

As for our app, the code is rather short and simple:

package main

import (
    "fmt"
    "net/http"
    "time"
)

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
    curTime := time.Now().Format("02.01.2006 15:04:05")

    fmt.Fprintf(w, "%s", curTime)
}

If you haven’t played with Go before, official docs would be a good place to start.

The only important thing to note here is that we’re listening on port 8080, which we need to set in the Dockerfile through EXPOSE.

Steps on how to make this yourself

Go code

  • Create the Go app (for testing purposes, you may use the same exact code)
  • Test it locally with go run curtime.go
  • Open your browser and go to http://localhost:8080
  • You should see the current time displayed

Docker

  • Create a Dockerfile with that content (Make sure you set the correct port in case you’ll be using a different one)
  • Inside the source folder (where you have curtime.go and Dockerfile) run docker build -t yourUserName/curtime . – this will create the image
  • You can check if the image was created with docker images
  • Run the image in a container: docker run -d -p 8000:8080 yourUserName/curtime
  • Access the app via your browser on port 8000. I used 8000 and 8080 intentionally so that it’s easier to explain that the 8000 port is the one on your computer and the 8080 is the port to which we’re ‘binding’ to

Few other useful Docker commands:

  • docker ps will list all the running containers. By adding the -a switch, you’ll see even the stopped ones
  • docker stop – stop the running container
  • docker rm containerID – delete the container with id containerID
  • docker images – list the images that are on your machine
  • docker rmi imageID – delete the image with id imageID

How to #dockerize a simple #Go app? https://t.co/JSIrP3CHYO

— Nikola Brežnjak (@HitmanHR) January 18, 2017

Recent posts

  • Discipline is also a talent
  • Play for the fun of it
  • The importance of failing
  • A fresh start
  • Perseverance

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (35)
  • Daily Thoughts (77)
  • Go (3)
  • iOS (5)
  • JavaScript (127)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (2)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (77)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (40)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak