首页 > 其他分享 >Go代码基于dockerfile做成镜像

Go代码基于dockerfile做成镜像

时间:2023-02-15 10:45:44浏览次数:43  
标签:moban -- root test go gin Go 镜像 dockerfile

一、基础环境安装与初始化

# 手动执行步骤
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") 
} 
View Code

# 由于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

二、编写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 版本"}

 

标签:moban,--,root,test,go,gin,Go,镜像,dockerfile
From: https://www.cnblogs.com/yangmeichong/p/17121927.html

相关文章

  • go template
    /*[email protected],Version2.0(the"License");youmaynotusethisfileexceptincompliancewiththeLicen......
  • Django context must be a dict rather than UserProfile.
    contextmustbeadictratherthanUserProfile. #主页@login_requireddefindex(request):data={}data=request.userprint(type(data))print(da......
  • djiango框架前端数据通过Ajax传到后端进行处理
    最近在琢磨搭建测试平台,python相对来说简单一点,框架的话就选择djiango框架,既然搭建平台就肯定需要进行数据处理,摸索了一下,在此记录一下数据的梳理流程。1.首先自己写一个......
  • Go-31 Go中字符串切割的三种方法 strings.Index()、strings.Cut()、strings.Split()
    packagemainimport( "fmt" "strings")/* 字符串切割 strings包对字符串的操作 */funcmain(){ //方法一 addr:="192.168.0.1:80" pos:=strings.Index(ad......
  • 随记一下之创建 go 项目
    创建go项目go开发工具箱下载Go官网下载地址:https://golang.org/dl/Go官方镜像站:https://golang.google.cn/dl/选用合适的系统和版本,然后进行安装。不同系统安装......
  • CVE-2023-0669 GoAnywhereMFT反序列化漏洞复现
    免责声明文中提到的所有技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途,否则产生的一切后果自行承担,与本文作者无关!GoAnywhereMFTGoAnywher......
  • Go基础系列 01-Golang简介
    1.什么是GolangGo(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。Go支持面向对象,而且具有真正的闭包(closures)和反射(reflecti......
  • golang 复杂数据类型
    1.指针一个指针变量指向了一个值的内存地址,类似于变量和常量,在使用指针前你需要声明指针。每个变量在运行时都拥有一个地址,这个地址代表变量在内存中的位置。使用&字符......
  • Django日志配置
    Django使用Python内建的logging模块打印日志,配置由四个部分组成记录器:Logger处理程序:Handler过滤器:Filter格式化:formatter 记录器-LoggerLogger为日志系......
  • 如何关闭gorm 1.20.0中的数据库实例
    因为我没有在带有*gorm实例的Close()函数中找到dbURI:=fmt.Sprintf("user=%spassword=%sdbname=%sport=%ssslmode=%sTimeZone=%s","username","password","......