使用dockerfile发布go项目

docker安装

下载docker
yum install docker
 
设置docker随系统启动
chkconfig docker on
 
启动docker服务
service docker start
 
启动后查看docker状态
systemctl status docker:
[root@dev-k8s ~]# systemctl status docker● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-06-13 15:50:16 CST; 7min ago
     Docs: http://docs.docker.com
 Main PID: 12197 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─12197 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec...
           └─12207 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --s...
 
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.283640021+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.323651305+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.324181137+08:00" level=info msg="Loading containers: start."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.327645142+08:00" level=info msg="Firewalld running: false"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.421269250+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 192.168.0.0/24. Daemon optio...d IP address"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.452900303+08:00" level=info msg="Loading containers: done."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572134045+08:00" level=info msg="Daemon has completed initialization"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572172608+08:00" level=info msg="Docker daemon" commit="7d71120/1.13.1" graphdriver=overlay2 version=1.13.1
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.585165447+08:00" level=info msg="API listen on /var/run/docker.sock"
Jun 13 15:50:16 dev-k8s systemd[1]: Started Docker Application Container Engine.

go-helloworld代码

package main
 
import (
   "fmt"
   "log"
   "net/http"
   "os"
)
 
func handler(w http.ResponseWriter, r *http.Request) {
   log.Print("Hello world received a request.")
   target := "Welcome to go"
   fmt.Fprintf(w, "Hello, %s!\n", target)
}
 
func main() {
   log.Print("Hello world sample started.")
 
   http.HandleFunc("/", handler)
 
   port := os.Getenv("PORT")
   if port == "" {
      port = "8009"
   }
 
   log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
 
}

go项目打包

CMD中进入main方法目录设置linux运行环境 后 打包
F:\私人代码\go-helloword>set GOARCH=amd64
F:\私人代码\go-helloword>set GOOS=linux
 
 
环境问题导致打包出错,使用go env -w GO111MODULE=off命令解决下
F:\私人代码\go-helloword>go build -o helloworld
go: cannot find main module, but found .git/config in F:\私人代码\go-helloword
to create a module there, run:
go mod init
F:\私人代码\go-helloword>go mod init
go: cannot determine module path for source directory F:\私人代码\go-helloword (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
F:\私人代码\go-helloword>go env -w GO111MODULE=off
 
 
重新打包后成功
F:\私人代码\go-helloword>go build -o helloworld
F:\私人代码\go-helloword>dir
驱动器 F 中的卷是 文档及代码
卷的序列号是 DB44-E624
F:\私人代码\go-helloword 的目录
2022/06/13 16:37 <DIR> .
2022/06/13 16:37 <DIR> ..
2022/06/10 16:43 6,148 .DS_Store
2022/06/13 10:33 <DIR> .idea
2022/06/13 16:20 425 Dockerfile
2022/06/13 16:37 6,066,257 helloworld
2022/06/10 17:50 448 helloworld.go
4 个文件 6,073,278 字节
3 个目录 307,233,075,200 可用字节

dockerfile编写,需要加上RUN chmod +x /build/helloworld命令,否则导致容器启动时对go二进制文件helloworld没有使用权限

FROM golang:alpine
 
WORKDIR /build
 
COPY helloworld .
 
EXPOSE 8009
 
RUN chmod +x /build/helloworld
 
CMD ["/build/helloworld"]

使用dockerfile创建镜像,然后使用镜像创建容器

使用dockerfile创建镜像
[root@dev-k8s docker-go]# docker build . -t go-helloworld:0.0.4
Sending build context to Docker daemon 6.069 MB
Step 1/6 : FROM golang:alpine
 ---> 155ead2e66ca
Step 2/6 : WORKDIR /build
 ---> Using cache
 ---> 6b6f9f40297e
Step 3/6 : COPY helloworld .
 ---> 44fefa2c08df
Removing intermediate container f2ed9e5c80b6
Step 4/6 : EXPOSE 8009
 ---> Running in 3dff69240776
 ---> b7ce1ff0c201
Removing intermediate container 3dff69240776
Step 5/6 : RUN chmod +x /build/helloworld
 ---> Running in 344e235c6ee8
 
 ---> 2dea00206ded
Removing intermediate container 344e235c6ee8
Step 6/6 : CMD /build/helloworld
 ---> Running in a8a56639f2fb
 ---> df2ca4071b8c
Removing intermediate container a8a56639f2fb
Successfully built df2ca4071b8c
 
 
用镜像创建容器
root@dev-k8s ~]# docker run -d -it -p 8009:8009  go-helloworld:0.0.4
29ea4e02b8319f564b6cf7073792f275c754fcbeb08bb6062683c1524f9cbc7c

访问helloworld项目

root@dev-k8s docker-go]# netstat -antp |grep 8009
tcp        0      0 10.6.0.11:33522         172.22.254.3:8009       ESTABLISHED 17128/n9e-agent
tcp6       0      0 :::8009                 :::*                    LISTEN      12318/docker-proxy-
 
[root@dev-k8s docker-go]# curl localhost:8009
Hello, Welcome to go!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的流程,需要多个步骤和工具协同完成。以下是基本的步骤和工具: 1. 创建Gitee代码仓库并将您的Go项目推送到该仓库。 2. 在Jenkins中创建Pipeline,并在Pipeline中定义构建、测试和部署步骤。 3. 创建Harbor镜像仓库,并将您的Go项目打包成Docker镜像并推送到Harbor仓库。 4. 配置Kubernetes集群,以便从Harbor仓库中拉取镜像并部署到Kubernetes集群。 5. 在Jenkins中使用Kubernetes插件,将Go项目部署到Kubernetes集群。 以下是更详细的步骤和工具: 1. 在Gitee上创建代码仓库并推送代码: a. 创建一个新的Gitee代码仓库,将您的Go项目推送到该仓库中。 b. 创建一个名为“Jenkinsfile”的文件,其中包含您的Jenkins Pipeline定义。将此文件推送到Gitee仓库中。 2. 在Jenkins中创建Pipeline: a. 在Jenkins中创建一个新的Pipeline,将您的Gitee仓库链接到Jenkins中。 b. 在Pipeline中定义构建、测试和部署步骤。例如,您可以使用`go build`和`go test`命令来构建和测试您的Go项目。然后,您可以使用Dockerfile将Go项目打包成Docker镜像。 c. 在Pipeline中使用Jenkins Kubernetes插件将Docker镜像推送到Harbor镜像仓库中。 3. 创建Harbor镜像仓库并将镜像推送到仓库: a. 在Harbor中创建一个新的镜像仓库。 b. 在Jenkins Pipeline中使用Docker命令将您的Go项目打包成Docker镜像,并将该镜像推送到Harbor镜像仓库中。 4. 配置Kubernetes集群: a. 在Kubernetes集群中安装并配置Harbor镜像仓库,以便能够从该仓库中拉取镜像。 b. 配置Kubernetes Deployment和Service,以便从Harbor镜像仓库中拉取您的Go项目Docker镜像并在Kubernetes集群中部署该项目。 5. 在Jenkins中使用Kubernetes插件部署Go项目: a. 在Jenkins中使用Kubernetes插件,将您的Go项目部署到Kubernetes集群中。 b. 测试您的Go项目是否正确地部署在Kubernetes集群中,并且可以正常运行。 以上是一个比较完整的流程,其中包含了多个工具和步骤。您可以根据自己的实际情况和需求进行适当地调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值