{"id":3093,"date":"2017-01-17T12:59:49","date_gmt":"2017-01-17T12:59:49","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3093"},"modified":"2017-01-18T21:25:32","modified_gmt":"2017-01-18T21:25:32","slug":"dockerize-simple-go-app","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/go\/dockerize-simple-go-app\/","title":{"rendered":"How to dockerize a simple Go app"},"content":{"rendered":"<p>In this tutorial, I&#8217;m going to show you how to dockerize a simple app (written in Go) that shows the current time. <a href=\"https:\/\/github.com\/Hitman666\/GoCurrentTimeDockerized\">Github repo is here<\/a>, in case you&#8217;re interested.<\/p>\n<h2>Dockerfile<\/h2>\n<p>So, if you haven&#8217;t played with Docker before, <a href=\"https:\/\/docs.docker.com\/\">official docs<\/a> would be a good start.<\/p>\n<p>For our Go app, we need to create a file named <code>Dockerfile<\/code> with the following content:<\/p>\n<pre><code>FROM golang:onbuild\nEXPOSE 8080\n<\/code><\/pre>\n<h2>Go code<\/h2>\n<p>As for our app, the code is rather short and simple:<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"time\"\n)\n\nfunc main() {\n    http.HandleFunc(\"\/\", handler)\n    http.ListenAndServe(\":8080\", nil)\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    curTime := time.Now().Format(\"02.01.2006 15:04:05\")\n\n    fmt.Fprintf(w, \"%s\", curTime)\n}\n<\/code><\/pre>\n<p>If you haven&#8217;t played with Go before, <a href=\"https:\/\/golang.org\/doc\/\">official docs<\/a> would be a good place to start.<\/p>\n<p>The only important thing to note here is that we&#8217;re listening on port 8080, which we need to set in the Dockerfile through <code>EXPOSE<\/code>.<\/p>\n<h2>Steps on how to make this yourself<\/h2>\n<h3>Go code<\/h3>\n<ul>\n<li>Create the Go app (for testing purposes, you may use the same exact code)<\/li>\n<li>Test it locally with <code>go run curtime.go<\/code><\/li>\n<li>Open your browser and go to <a href=\"http:\/\/localhost:8080\">http:\/\/localhost:8080<\/a><\/li>\n<li>You should see the current time displayed<\/li>\n<\/ul>\n<h3>Docker<\/h3>\n<ul>\n<li>Create a <code>Dockerfile<\/code> with that content (Make sure you set the correct port in case you&#8217;ll be using a different one)<\/li>\n<li>Inside the source folder (where you have <code>curtime.go<\/code> and <code>Dockerfile<\/code>) run <code>docker build -t yourUserName\/curtime .<\/code> &#8211; this will create the image<\/li>\n<li>You can check if the image was created with <code>docker images<\/code><\/li>\n<li>Run the image in a container: <code>docker run -d -p 8000:8080 yourUserName\/curtime<\/code><\/li>\n<li>Access the app via your browser on port <code>8000<\/code>. I used 8000 and 8080 intentionally so that it&#8217;s easier to explain that the 8000 port is the one on your computer and the 8080 is the port to which we&#8217;re &#8216;binding&#8217; to<\/li>\n<\/ul>\n<p>Few other useful Docker commands:<\/p>\n<ul>\n<li><code>docker ps<\/code> will list all the running containers. By adding the <code>-a<\/code> switch, you&#8217;ll see even the stopped ones<\/li>\n<li><code>docker stop<\/code> &#8211; stop the running container<\/li>\n<li><code>docker rm containerID<\/code> &#8211; delete the container with id <code>containerID<\/code><\/li>\n<li><code>docker images<\/code> &#8211; list the images that are on your machine<\/li>\n<li><code>docker rmi imageID<\/code> &#8211; delete the image with id <code>imageID<\/code><\/li>\n<\/ul>\n<blockquote class=\"twitter-tweet\" data-width=\"550\">\n<p lang=\"en\" dir=\"ltr\">How to <a href=\"https:\/\/twitter.com\/hashtag\/dockerize?src=hash\">#dockerize<\/a> a simple <a href=\"https:\/\/twitter.com\/hashtag\/Go?src=hash\">#Go<\/a> app? <a href=\"https:\/\/t.co\/JSIrP3CHYO\">https:\/\/t.co\/JSIrP3CHYO<\/a><\/p>\n<p>&mdash; Nikola Bre\u017enjak (@HitmanHR) <a href=\"https:\/\/twitter.com\/HitmanHR\/status\/821830405145985025\">January 18, 2017<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, I&#8217;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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3094,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-3093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"predecessor-version":[{"id":3097,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3093\/revisions\/3097"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/3094"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}