记录自己用dockers部署Go项目
Go
项目文件结构如下
首先实现一个测试demo main.go
1package main
2
3import (
4 "fmt"
5 "github.com/gin-gonic/gin"
6 "net/http"
7)
8
9func main() {
10 fmt.Println("service start")
11 router := gin.Default()
12 router.GET("/ping", func(context *gin.Context) {
13 context.JSON(http.StatusOK, gin.H{"message": "pong"})
14 fmt.Println("service healthy")
15 })
16 router.Run(":9999")
17}
Dockerfile
编写Dockerfile文件Dockerfile
1FROM golang:1.17-alpine
2
3WORKDIR /app
4ADD * /app
5
6ENV GO111MODULE=on \
7 CGO_ENABLED=0 \
8 GOOS=linux \
9 GOARCH=amd64 \
10 GOPROXY="https://goproxy.io,direct"
11RUN go mod download
12
13RUN go build -o test_go .
14
15EXPOSE 9999
16
17CMD ./test_go
编写启动构建和启动脚本
构建脚本docker_build.sh
1docker build -t my_test .
运行脚本 docker_run.sh
1docker run -p 9999:9999 --name gin_test my_test
执行构建脚本
1docker_build.sh
执行运行脚本
1docker_run.sh
测试
在ApiPost进行测试