Containerize Golang Application

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

Let's assume we have a sample Golang application (in a Go project folder).
Contents of main.go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
In the same folder, run the following commands to initialize go modules
# initialize go modules
go mod init github.com/username/hello-world
# cleans up dependencies and fetches if not present
go mod tidy
Now, create a Dockerfile in the same folder
Contents of Dockerfile
FROM golang:alpine as builder
RUN apk update && apk upgrade && \
apk add --no-cache bash git
WORKDIR /app
COPY . .
RUN go mod tidy
RUN GOFLAGS= GOOS=linux CGO_ENABLED=0 go build -v -o bin/app main.go
FROM alpine:3
RUN apk update \
&& apk add --no-cache ca-certificates \
&& update-ca-certificates 2>/dev/null || true
COPY --from=builder /app/bin/app /app
CMD ["/app"]
What's happening in the above Dockerfile?
We use a builder container (golang:alpine) to gather dependencies
We build the executable in the builder container
We create an alpine container and copy the executable to that.
We also specify CMD which tells docker to run our executable
In the final Docker image, we will only have the final container.
Note that we use alpine images to keep the image size lower.
If you do not want to give your app root access to the container, you can also add a new user and run the executable as that user. Refer GIST
You can now build your docker image
docker build -t app:latest -t mridulganga/app:latest .
You can push or run this docker image now.
# run using the local image
docker run app:latest
# push the image to dockerhub mridulganga's account
docker push mridulganga/app:latest
Make sure you are logged into Docker hub, try docker login if not