Let us try to understand what is docker and a simple api in Golang
Tags: docker, go, api, webdev
Published: 2022-08-07 09:31:00

> Docker is an open source containerization platform. It enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment -[IBM](https://www.ibm.com/cloud/learn/docker) 

So, what is containerization then ?

> Containerization is a form of virtualization where applications run in isolated user spaces, called containers, while using the same shared operating system (OS). One of the benefits of containerization is that a container is essentially a fully packaged and portable computing environment. -[Citrix](https://www.citrix.com/en-in/solutions/app-delivery-and-security/what-is-containerization.html)

And also
[From Docker about container](https://www.docker.com/resources/what-container/)

[History of Container](https://d2iq.com/blog/brief-history-containers)

keep surfing to understand the history of computing, it is pretty much interesting!!
After all it is essential to understand the core part of any software concept in the IT and services industry by asking **what is it ? why is it ? how is it ? :)**  

[Bare metal to serverless](https://loige.co/from-bare-metal-to-serverless/)

[Overview of Servers](https://vegibit.com/how-compute-has-evolved/)

We know that we can have linux container and windows container 
(I really not sure about Apple Mac OS X container :) )


If you have bit of application development experience, just assume that container is mini operating system but only bundled up with specific deps, libs etc.. to run the code you have written. You cannot use the container like full operating system. We build the containers based on our requirements to run our software. And finally docker is containerization platform, docker is not the only containerization platform, there are other platforms also [Google it]. And again docker and container both are different.

Anyway, let us say you want to build and run a code for fetching all the opened restaurants near by you (of course you provide place as input to the code), you can write code, once it is working fine in your local, you could just write simple docker commands such as how you want to build and run your code in the mini operating system. 

Ok, I think it is too much about container and docker. Let us write simple Golang application, container it and run it. [We can even push to our docker hub which is just like git VCS but for docker images].

```bash
go mod init github.com/YOUR_GITHUB_USERNAME/go-docker-example
``` 

```golang
module github.com/YOUR_GITHUB_USERNAME/go-docker-example

go 1.18

```
in case if you add deps, just tidy it but for this tutorial, we are not using any packages
 
```golang
go mod tidy
```
```golang
package main

import (
	"fmt"
	"html"
	"log"
	"net/http"
)

func main() {
	// index handler
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
	})

	// hello handler
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello")
	})

	// starting the server
	log.Fatal(http.ListenAndServe(":8081", nil))
}
```

Let us write the `Dockerfile` which are just commands to build our mini OS from base image.

Where we write `Dockerfile`, we build it, then we get `image` and with the `image`, when we run it, we get `container`. 

```text
# Let us specify base image required to for our application
# usually we specify base images to build application
# just assume that we will get linux OS with golang installed in it
# to build and run go programs
FROM golang:1.18-alpine

# all the operations we do here are linux commands only
# basically it is like just you have some linux OS,
# the things you specify here just commands to fetch source code
# and run it

# create new directory app where 
# we we build our application
RUN mkdir /app

# add everything from current source to this container
ADD . /app

# from now onwards this would be our workdire where we would
# be performing all the operations
WORKDIR /app

## let us run go mod download command to pull in any dependencies
RUN go mod download

## let us now build 
RUN go build -o main .

## start command which kicks off
## this newly created binary executable
CMD ["/app/main"]
```
Repo: https://github.com/developer1622/go-docker-example

Thank you, Happy learning. More to share!
```golang
for{ 
  unlearn
  learn
  relearn 
}
```