docker build and deploy go

ยท 3 mins read

Golang code

Run a helloworld server

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world")
	})
	r.Run()
}

Create dockerfile

scratch image has no shell and can only run binary files

FROM golang:alpine as builder

ENV GO111MODULE=on

WORKDIR /build

COPY . .

RUN go mod init hello && go get
RUN go build -o app .

FROM scratch

COPY --from=builder /build/app /

ENTRYPOINT [ "/app" ]

build image
docker image build -t tast .
docker run
docker run -d -p 8080:8080 test