前提環(huán)境
#go環(huán)境:
[root@urbancabin go]# go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://mirrors.aliyun.com/goproxy/,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2087487172=/tmp/go-build -gno-record-gcc-switches"
[root@urbancabin urbancabin]# go env|grep -E "GOOS|GOARCH|GO111MODULE|GOPROXY"
GO111MODULE="on"
GOARCH="amd64"
GOOS="linux"
GOPROXY="https://mirrors.aliyun.com/goproxy/,direct" #或者h(yuǎn)ttps://goproxy.cn,direct
#修改環(huán)境配置命令
go env -w GOOS="linux"
go env -w GOHOSTARCH="amd64"
#其他參數(shù)修改類似
1、創(chuàng)建go項目
[root@urbancabin go]# mkdir urbancabin
[root@urbancabin go]# cd urbancabin/
2赫舒、初始化項目
[root@urbancabin urbancabin]# ls
[root@urbancabin urbancabin]# go mod init urbancabin
go: creating new go.mod: module urbancabin
3悍及、創(chuàng)建一個gin項目
[root@urbancabin go]# cat gin.go
package main
import (
"github.com/gin-gonic/gin"
"time"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"message": "Welcome UrbanCabin! 當(dāng)前時間為:" + time.Now().Format("2020-08-15 10:10:10"),
})
})
err := r.Run(":1111")
if err != nil {
return
}
}
可以本地跑一邊運行下程序是否正常。
4接癌、Dockerfile
[root@urbancabin go]# cat Dockerfile
#對應(yīng)自己的go版本心赶, -alpine:指代鏡像比較小
FROM golang:1.19-alpine as urbancabin
#為我們的鏡像設(shè)置必要的環(huán)境變量
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /project/docker_go/
#COPY go.mod , go.sum and download the dependencied
COPY go.* ./
RUN go mod download
#COPY All things inside the project and build
COPY . .
RUN go build -o /project/docker_go/build/urbancabin .
EXPOSE 1111
ENTRYPOINT ["/project/docker_go/build/urbancabin"]
FROM alpine:latest
RUN mkdir -p /usr/share/zoneinfo/Asia/
COPY Shanghai /etc/localtime
WORKDIR /
COPY --from=urbancabin /project/docker_go/build/urbancabin /
CMD "./urbancabin"
釋義:
FROM golang:1.19-alpine as urbancabin :將FROM golang:1.19-alpine用作此Docker構(gòu)建的基礎(chǔ)鏡像,和自己使用的go版本進(jìn)行統(tǒng)一缺猛。不知道的缨叫,可以查看go.mod看看自己使用的版本。as urbancabin 給基礎(chǔ)鏡像一個別名
ENV :環(huán)境配置荔燎,配置代理耻姥,編譯環(huán)境。
WORKDIR:工作目錄路徑有咨。
COPY go.* ./ :從項目復(fù)制go.mod & go.sum文件到工作目錄琐簇。
RUN go mod dowmload:從go modules 下載項目依賴。
COPY . .:將我們項目中的所有內(nèi)容復(fù)制到工作目錄中座享。
RUN go build -o /project/docker_go/build/urbancabin . : 在工作目錄中構(gòu)建我們的項目并將其/project/docker_go/build/urbancabin作為二進(jìn)制文件輸出婉商。
EXPOSE 1111 : 告訴docker我們的代碼將暴露端口1111
ENTRYPOINT ["/project/docker_first/build/myapp"] :當(dāng)我們運行這個鏡像的容器時,它將從我們的構(gòu)建可執(zhí)行文件開始執(zhí)行
FROM alpine:latest 拉取基礎(chǔ)鏡像
RUN mkdir -p /usr/share/zoneinfo/Asia/ 創(chuàng)建時區(qū)目錄
COPY Shanghai /etc/localtime copy本地時間到容器目錄
WORKDIR / 工作目錄路徑
COPY --from=urbancabin /project/docker_go/build/urbancabin / 引用上面編譯好的基礎(chǔ)鏡像
CMD "./urbancabin" 運行
5渣叛、準(zhǔn)備好時區(qū)文件
[root@urbancabin urbancabin]# cp /usr/share/zoneinfo/Asia/Shanghai .
[root@urbancabin urbancabin]# ls
Dockerfile gin.go go.mod Shanghai
6丈秩、構(gòu)建鏡像
docker build -f Dockerfile -t demo:v1 .
報錯:
[root@urbancabin urbancabin]# docker build -f Dockerfile -t demo:v1 .
Sending build context to Docker daemon 6.144kB
Step 1/17 : FROM golang:1.19-alpine as urbancabin
---> d0f5238dcb8b
Step 2/17 : ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct CGO_ENABLED=0 GOOS=linux GOARCH=amd64
---> Using cache
---> 1f82e2837c25
Step 3/17 : ENV TZ=Asia/Shanghai
---> Using cache
---> 3e6cb8a3322e
Step 4/17 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
---> Using cache
---> d2f36d8d1b34
Step 5/17 : WORKDIR /project/docker_go/
---> Using cache
---> b1bc9767a7a9
Step 6/17 : COPY go.* ./
---> 6982bc43881d
Step 7/17 : RUN go mod download
---> Running in 7fb3f8033b7a
go: no module dependencies to download
Removing intermediate container 7fb3f8033b7a
---> 214031849c62
Step 8/17 : COPY . .
---> fdfca09ad314
Step 9/17 : RUN go build -o /project/docker_go/build/urbancabin .
---> Running in deb81a265de2
gin.go:4:9: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
The command '/bin/sh -c go build -o /project/docker_go/build/urbancabin .' returned a non-zero code: 1
解決:
[root@urbancabin go]# go get -v github.com/gin-gonic/gin
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.8.1
go: added github.com/go-playground/locales v0.14.0
go: added github.com/go-playground/universal-translator v0.18.0
go: added github.com/go-playground/validator/v10 v10.10.0
go: added github.com/goccy/go-json v0.9.7
go: added github.com/json-iterator/go v1.1.12
go: added github.com/leodido/go-urn v1.2.1
go: added github.com/mattn/go-isatty v0.0.14
go: added github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.0.1
go: added github.com/ugorji/go/codec v1.2.7
go: added golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
go: added golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
go: added golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
go: added golang.org/x/text v0.3.6
go: added google.golang.org/protobuf v1.28.0
go: added gopkg.in/yaml.v2 v2.4.0
[root@urbancabin go]# ls
Dockerfile gin.go go.mod go.sum Shanghai
再次運行:
[root@urbancabin urbancabin]# docker build -f Dockerfile -t demo:v1 .
Sending build context to Docker daemon 14.85kB
Step 1/17 : FROM golang:1.19-alpine as urbancabin
---> d0f5238dcb8b
Step 2/17 : ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct CGO_ENABLED=0 GOOS=linux GOARCH=amd64
---> Using cache
---> 1f82e2837c25
Step 3/17 : ENV TZ=Asia/Shanghai
---> Using cache
---> 3e6cb8a3322e
Step 4/17 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
---> Using cache
---> d2f36d8d1b34
Step 5/17 : WORKDIR /project/docker_go/
---> Using cache
---> b1bc9767a7a9
Step 6/17 : COPY go.* ./
---> 7c1e1f8c1549
Step 7/17 : RUN go mod download
---> Running in b576c6aaa2fd
Removing intermediate container b576c6aaa2fd
---> e419701dba77
Step 8/17 : COPY . .
---> 327d03a3ef68
Step 9/17 : RUN go build -o /project/docker_go/build/urbancabin .
---> Running in a30ad081517b
Removing intermediate container a30ad081517b
---> 49e03df14b4b
Step 10/17 : EXPOSE 1111
---> Running in bd250a5ccad4
Removing intermediate container bd250a5ccad4
---> f37b5abdf367
Step 11/17 : ENTRYPOINT ["/project/docker_go/build/urbancabin"]
---> Running in 4af9809bcacf
Removing intermediate container 4af9809bcacf
---> eee2ccff7182
Step 12/17 : FROM alpine:latest
---> c059bfaa849c
Step 13/17 : RUN mkdir -p /usr/share/zoneinfo/Asia/
---> Using cache
---> 638ab17ab720
Step 14/17 : COPY Shanghai /etc/localtime
---> Using cache
---> 7c844f1d3dbd
Step 15/17 : WORKDIR /
---> Using cache
---> b90e9bf2497c
Step 16/17 : COPY --from=urbancabin /project/docker_go/build/urbancabin /
---> 366fb9933544
Step 17/17 : CMD "./urbancabin"
---> Running in 72364fc179dd
Removing intermediate container 72364fc179dd
---> 01e08eae887f
Successfully built 01e08eae887f
Successfully tagged demo:v1
7、運行容器
[root@urbancabin urbancabin]# docker run -d -p 1111:1111 demo:v1
c42384b575882b1973f337f307e0fc7c74b13566046e970b15d4dd68d6231241
[root@urbancabin urbancabin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c42384b57588 demo:v1 "/bin/sh -c \"./urban…" 14 seconds ago Up 13 seconds 0.0.0.0:1111->1111/tcp, :::1111->1111/tcp relaxed_snyder
8淳衙、驗證
[root@urbancabin urbancabin]# curl localhost:1111
{"code":200,"message":"Welcome UrbanCabin! 當(dāng)前時間為:2022-08-15 10:40:21"}
至此蘑秽,容器沒問題,接下來將此go程序發(fā)布到k3s...
9箫攀、打包鏡像
[root@urbancabin urbancabin]# docker image list|grep demo
demo v1 50c6dbfa8f74 2 minutes ago 15.9MB
[root@urbancabin urbancabin]# docker tag demo:v1 demo_go:v1
[root@urbancabin urbancabin]# docker image list|grep demo_go
demo_go v1 50c6dbfa8f74 3 minutes ago 15.9MB
10筷狼、推送到遠(yuǎn)程倉庫
[root@urbancabin urbancabin]# push_remote demo_go:v1
0、下載鏡像:demo_go:v1
1匠童、將要備份的鏡像名:demo_go
2埂材、將要備份的鏡像版本:v1
3、給鏡像重新打阿里云Tag:registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
4汤求、給鏡像重新打騰訊云Tag:ccr.ccs.tencentyun.com/urbancabin/demo_go:v1
5俏险、推鏡像到阿里云倉庫
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go]
b44969be388b: Pushed
80598ca4e625: Pushed
8c18e3406525: Pushed
8d3ac3489996: Mounted from urbancabin/alpine-glibc
v1: digest: sha256:98c8436c1b2c31b7103a6e0eed6f1adc8b9b323b92822c39aedf5010a58808cc size: 1153
6、推鏡像到騰訊云倉庫
The push refers to repository [ccr.ccs.tencentyun.com/urbancabin/demo_go]
b44969be388b: Pushed
80598ca4e625: Pushed
8c18e3406525: Pushed
8d3ac3489996: Mounted from urbancabin/alpine-glibc
v1: digest: sha256:98c8436c1b2c31b7103a6e0eed6f1adc8b9b323b92822c39aedf5010a58808cc size: 1153
7扬绪、demo_go:v1鏡像推送完成竖独!
11、更新線上demo
舊版本:
[root@urbancabin urbancabin]# curl http://localhost:30111/
當(dāng)前時間:2022-08-15 10:44:57
[root@urbancabin urbancabin]# kubectl get po -n demo -oyaml|grep registry
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo:v1
修改鏡像版本
[root@urbancabin urbancabin]# kubectl edit deploy -n demo urbancabin-demo
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
新版本:
[root@urbancabin urbancabin]# kubectl get po -n demo -oyaml|grep registry
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
[root@urbancabin urbancabin]# kubectl get po -n demo
NAME READY STATUS RESTARTS AGE
urbancabin-demo-589cd8999b-vxnwr 1/1 Running 0 71s
12挤牛、驗證:
[root@urbancabin urbancabin]# curl -s http://localhost:30111/|jq
{
"code": 200,
"message": "Welcome UrbanCabin! 當(dāng)前時間為:2022-08-15 10:54:33"
}
至此莹痢,發(fā)布成功!
參考鏈接:
https://blog.csdn.net/weixin_51261234/article/details/125906988
https://blog.csdn.net/HenryLeeJ/article/details/122896265
https://cdn.modb.pro/db/381984