一、在服务器上先将程序编译完成
基础环境安装与初始化
https://mirror.go-repo.io/centos/7/x86_64/
# 手动执行步骤 yum install go -y # 将go代码拷贝进入机器 # 初始化go项目 [root@moban test]# pwd /root/test # 下载模块 [root@moban test]# go mod init test go: creating new go.mod: module test go: to add module requirements and sums: go mod tidy
[root@moban test]# cat main.go package main import ( "net/http" "github.com/gin-gonic/gin" ) func statusOKHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "success~welcome to study"}) } func versionHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"version": "v1.1 版本"}) } func main() { router := gin.New() router.Use(gin.Recovery()) router.GET("/", statusOKHandler) router.GET("/version", versionHandler) router.Run(":8080") }
# 由于go访问都是国外网址,需要设置代理 [root@moban test]# go env -w GOPROXY=https://goproxy.cn,direct [root@moban test]# go mod tidy go: finding module for package github.com/gin-gonic/gin go: downloading github.com/gin-gonic/gin v1.8.2 .... # 构建源码 [root@moban test]# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.go [root@moban test]# ll 总用量 9912 -rw-r--r-- 1 root root 987 2月 15 10:23 go.mod -rw-r--r-- 1 root root 8620 2月 15 10:23 go.sum -rwxr-xr-x 1 root root 10129011 2月 15 10:24 k8s-demo -rw-r--r-- 1 root root 466 2月 15 10:18 main.go
手动环境变量设置
export GO111MODULE="on" export GOPROXY="https://goproxy.cn,https://gocenter.io,direct" export GOPRIVATE="github.com/gstones/*" export GOPATH="/db/gowork"
阿里云go module代理
1.使用go1.11以上版本并开启go module机制
2.导出GOPROXY环境变量
export GOPROXY=https://mirrors.aliyun.com/goproxy/
二、编写dockerfile文件
[root@moban test]# cat dockerfile FROM alpine WORKDIR /data/app ADD k8s-demo /data/app/ CMD ["/bin/sh","-c","./k8s-demo"] [root@moban test]# docker build -t "go-test" . Sending build context to Docker daemon 10.14MB Step 1/4 : FROM alpine latest: Pulling from library/alpine 63b65145d645: Pull complete Digest: sha256:69665d02cb32192e52e07644d76bc6f25abeb5410edc1c7a81a10ba3f0efb90a Status: Downloaded newer image for alpine:latest ---> b2aa39c304c2 Step 2/4 : WORKDIR /data/app ---> Running in 2fe4717922e2 Removing intermediate container 2fe4717922e2 ---> fa1f50d47552 Step 3/4 : ADD k8s-demo /data/app/ ---> 132cba444301 Step 4/4 : CMD ["/bin/sh","-c","./k8s-demo"] ---> Running in 0852c02add23 Removing intermediate container 0852c02add23 ---> 892024d4c9d4 Successfully built 892024d4c9d4 Successfully tagged go-test:latest [root@moban test]# docker run -di --name go -p 30180:8080 go-test:latest a4cddbaad8a38cdd536965ec24d17351424a808eed6754ca147158b923644334 [root@moban test]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a4cddbaad8a3 go-test:latest "/bin/sh -c ./k8s-de…" 25 seconds ago Up 24 seconds 0.0.0.0:30180->8080/tcp, :::30180->8080/tcp go http://192.168.10.30:30180/ {"status":"success~welcome to study"} http://192.168.10.30:30180/version {"version":"v1.1 版本"}
二、在docker里面进行编译
用好scratch,打包最小镜像
FROM golang:1.18 AS builder ENV GO111MODULE=on \ CGO_ENABLED=0 \ GOOS=linux \ GOARCH=amd64 ENV GOPROXY=https://goproxy.cn WORKDIR /build COPY go.mod ./ COPY go.sum ./ RUN go mod download COPY hello.go ./ RUN go build -o hello . FROM scratch COPY --from=builder /build/hello / ENTRYPOINT ["./hello"] 引用:https://blog.csdn.net/xingyunyang/article/details/125547683