DEV Community

Ramu Mangalarapu
Ramu Mangalarapu

Posted on

Let us try to understand what is docker and a simple api in Golang

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

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

And also
From Docker about container

History of Container

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

Overview of Servers

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].

go mod init
Enter fullscreen mode Exit fullscreen mode

go 1.18

Enter fullscreen mode Exit fullscreen mode

in case if you add deps, just tidy it but for this tutorial, we are not using any packages

go mod tidy
Enter fullscreen mode Exit fullscreen mode
package main

import (

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))
Enter fullscreen mode Exit fullscreen mode

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.

# 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

## 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"]
Enter fullscreen mode Exit fullscreen mode


Thank you, Happy learning. More to share!

Enter fullscreen mode Exit fullscreen mode

Top comments (0)