https://eddycjy.com/posts/go/gin/2018-03-24-golang-docker/
https://studygolang.com/articles/11547
https://segmentfault.com/a/1190000013960558
go.mod
module go-gin-example go 1.14 require ( github.com/gin-gonic/gin v1.6.2 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v9 v9.29.1 // indirect )
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/index", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello Docker Form Golang!",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
Dockerfile
FROM golang:latest WORKDIR $GOPATH/src/go-gin-example COPY . $GOPATH/src/go-gin-example RUN GOPROXY="https://goproxy.io" GO111MODULE=on go build . EXPOSE 8080 ENTRYPOINT ["./go-gin-example"]
[root@VM_0_2_centos go-gin-example]# docker build -t go-gin-example .
Sending build context to Docker daemon 4.096 kB
Step 1/6 : FROM golang:latest
---> 315fc470b445
Step 2/6 : WORKDIR $GOPATH/src/go-gin-example
---> 4be5d67ad27d
Removing intermediate container 815220e6809a
Step 3/6 : COPY . $GOPATH/src/go-gin-example
---> 6a99647121bf
Removing intermediate container ceac88366ea1
Step 4/6 : RUN GOPROXY="https://goproxy.io" GO111MODULE=on go build .
---> Running in 879ca3d0654d
go: downloading github.com/gin-gonic/gin v1.6.2
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/go-playground/validator/v10 v10.2.0
go: downloading github.com/ugorji/go v1.1.7
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading github.com/go-playground/universal-translator v0.17.0
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: downloading github.com/go-playground/locales v0.13.0
---> 4bfdd98f7261
Removing intermediate container 879ca3d0654d
Step 5/6 : EXPOSE 8080
---> Running in ba412a5818f6
---> 9c36caca6c7d
Removing intermediate container ba412a5818f6
Step 6/6 : ENTRYPOINT ./go-gin-example
---> Running in e369c0b44c45
---> 659eda3e71d9
Removing intermediate container e369c0b44c45
Successfully built 659eda3e71d9
[root@VM_0_2_centos go-gin-example]# docker run -d -p 8080:8080 go-gin-example
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /index --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[root@VM_0_2_centos go-gin-example]# curl http://localhost:8080/index
{"message":"Hello Docker from Golang!"}
