Golang Rest Api by Docker

984/5000

Those looking for an alternative to virtual machines can have information about Docker.Docker dropped LXC as the default execution environment and replaced it with its own libcontainer library written in the Go programming language.In this article I will show how to make a golang restful api and hosting via Docker.

Docker is an open source platform that facilitates application development, deployment, and deployment using container technology. Today, it has become more popular with companies such as Google, IBM and Microsoft. Docker plays a major role in preferring to be more flexible than virtual machines and to be completely free.

Briefly explaining the container, it is the name given to each of the processes operated by the Docker Engine and isolated from each other. Docker’s virtualization structure does not have a Hypervisor layer, unlike known virtual machines (VirtualBox, Vmware, etc.). Instead, it uses the Docker Engine to access the host operating system and use the system tools in a shared manner. Thus, it consumes less system resources than conventional VMs.Developers can integrate their development environments with the necessary configurations into these containers and transfer them to the media.

Simple Golang Sample with Docker

The point of view of Go is so clear. The goal is to create a flexible, fast, powerful and powerful language with little language capability and rule!See below code . you see how it is so clear!

package main
import "fmt"
func main() {
fmt.Println("Hi Everyone!")
}

Docker file:

Docker easly can build images by using steps from DockerFile which has all commands. A Dockerfile adheres to a specific format and set of instructions which you can find at Dockerfile reference. you have to know 2 things “From” and “Run”.

  • From: getting latest golang docker image from hub
  • Run: build your application via “go build”
FROM golang:latest 
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]

There are 2 important steps. First of all you have to build your golang program and then you have to run it. That’s it!

  • For Build : docker build -t mygosample .
  • For Run : docker run -it mygosample

For checking all saved images we will use “docker images” command line on terminal.

please ignore my another golang sample is called gosample2 because I created before. but if you write “docker ps” on terminal you can’t see anything because there is no any active running dockerized rest services.

Simple Restful Services by Golang and Docker

This is not the only way to make HTTP requests with Golang.But this is an easy way to do it. I think that it is an important point for creating a request with easy steps and then making request from web browser. I want to explain steps means:

  • Describing handler method for routing. “ http.HandleFunc(“/”, handler)”
  • ListenAndServe() method for making alive restful service this is an important point for the article.
  • Managing errors also another one of the important tips please don’t forget something about Golang doesn’t have try-catch structure.You have to handle your error below like that.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s!", r.URL.Path[1:])
fmt.Println("RESTfulServ. on:8093, Controller:",r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Starting Restful services...")
fmt.Println("Using port:8093")
err := http.ListenAndServe(":8093", nil)
log.Print(err)
errorHandler(err)
}
func errorHandler(err error){
if err!=nil {
fmt.Println(err)
os.Exit(1)
}
}

When applying build and run process, restful api is alive at the end.

docker build -t gosample2 .
docker run -it gosample2

when you want to see all running container list. you have to use “docker ps” after hitting that there are all properties about containers which is runnig now.

Let’s call our sample restful api by using writing “http://localhost:8093/Mars” over web browser.after hitting enter button, you can easily see that on web browser screen “ Hello Mars!” . That’s it.

Conclusion

I think that this article is useful for beginners who want to learn and taking a look at Devops skills.Golang is a great programming language to create restful api because it is scalable and powerful for performance also faster than other programming language because of its structure.Also Docker is written by Golang. Using Golang and Docker is one of the best practices to learn what Docker is for. if you’re interested in more professional application you have to take a look at docker-compose.Without this, your application is not fit for production. I am intending to write an artice about that. Therefore, I am looking forward on finding enough time to achieve that.