{"id":21967411,"url":"https://github.com/keke-li/spike","last_synced_at":"2025-07-12T07:02:29.681Z","repository":{"id":90672665,"uuid":"167476974","full_name":"KeKe-Li/spike","owner":"KeKe-Li","description":"Golang Use Gin Framework  implementation of the project","archived":false,"fork":false,"pushed_at":"2019-09-27T01:50:12.000Z","size":853,"stargazers_count":20,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T05:34:46.377Z","etag":null,"topics":["docker","gin","golang","gomodule"],"latest_commit_sha":null,"homepage":"https://github.com/KeKe-Li/spike","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KeKe-Li.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-01-25T03:12:25.000Z","updated_at":"2020-11-14T06:27:03.000Z","dependencies_parsed_at":"2023-07-09T05:02:59.646Z","dependency_job_id":null,"html_url":"https://github.com/KeKe-Li/spike","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KeKe-Li/spike","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeKe-Li%2Fspike","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeKe-Li%2Fspike/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeKe-Li%2Fspike/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeKe-Li%2Fspike/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KeKe-Li","download_url":"https://codeload.github.com/KeKe-Li/spike/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeKe-Li%2Fspike/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264951976,"owners_count":23687993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","gin","golang","gomodule"],"created_at":"2024-11-29T13:26:34.754Z","updated_at":"2025-07-12T07:02:29.675Z","avatar_url":"https://github.com/KeKe-Li.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"#### spike\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"100\" align=\"center\" src=\"public/images/logo.png\" /\u003e\n\u003c/p\u003e\n\n\u003cp align='center'\u003e\n\u003cimg src=\"https://img.shields.io/badge/build-passing-brightgreen.svg\"\u003e\n\u003ca href=\"https://twitter.com/perfactsen\"\u003e\u003cimg src=\"https://img.shields.io/badge/twitter-keke-green.svg?style=flat\u0026colorA=009df2\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.zhihu.com/people/sencoed.com/activities\"\u003e\u003cimg src=\"https://img.shields.io/badge/%E7%9F%A5%E4%B9%8E-keke-green.svg?style=flat\u0026colorA=009df2\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n`spike`项目用的是`Golang 1.13 `版本,本项目采用了go module,配合Golang的web框架gin实现的.\n\n#### Docker\n```docker\n#母镜像\nFROM golang\n#维护者信息\nMAINTAINER keke\n#工作目录\nWORKDIR $GOPATH/src/go\n#将文件复制到镜像中\nADD . $GOPATH/src/go\n#执行操作\nRUN go build  main.go\n#暴露端口\nEXPOSE 8080\n#程序入口\nENTRYPOINT [\"./main\"]\n```\n\n然后运行下面的命令把当前编译过项目打进docker镜像:\n\n```bash\n\u003e docker build -t spike .\n\n---\u003e 797daa9977c6\nSuccessfully built 797daa9977c6\nSuccessfully tagged main.go:latest\n```\n\n```bash\n\u003e docker images \nspike                               latest              797daa9977c6        8 minutes ago       801MB\n```\n\n表示把项目成功打进docker镜像了.\n\n运行docker镜像:\n\n```bash\n\u003e docker run -p 8080:8080 -d spike\n```\n这样就可以直接docker运行本地镜像启动go项目.\n\n#### go mod使用docker镜像\n\n使用了go mod之后我们可以使用的docker镜像部署go mod的项目,镜像如下:\n\n```dockerfile\n#母镜像\nFROM golang:latest as build\n#维护者信息\nMAINTAINER keke\n\nENV GOPROXY https://goproxy.io/\n# go module开启\nENV GO111MODULE on\n\nWORKDIR /go/cache\n\n# 添加go mod\nADD go.mod .\nADD go.sum .\n\n# 构建缓存包含了该项所有的依赖起到加速构建的作用\nRUN go mod download\n\n#工作目录\nWORKDIR /go/release\n\n#将文件复制到镜像中\nADD . .\n\n# ldflags中-s: 省略符号表和调试信息,-w: 省略DWARF符号表\nRUN GOOS=linux CGO_ENABLED=0 go build -ldflags=\"-s -w\" -installsuffix cgo -o spike .\n\n# scratch空的基础镜像，最小的基础镜像\n# busybox带一些常用的工具，方便调试， 以及它的一些扩展busybox:glibc\n# alpine另一个常用的基础镜像，带包管理功能，方便下载其它依赖的包\nFROM scratch as prod\n\nCOPY --from=build /go/release/spike /\n\n# 配置镜像的时间区\nCOPY --from=build /usr/share/zoneinfo/Asia/Shanghai /etc/localtime\n# 配置镜像的证书\nCOPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt\n# 将构建的可执行文件复制到新镜像中\nCOPY --from=build /go/release/spike /\n\nEXPOSE 8080\n\nCMD [\"/spike\"]\n\n```\n\n这个项目有一些外部依赖，所以在开发的时候都已调整好，并且编译通过，在开发环境已经生成了两个文件go.mod、go.sum.\n\n在dockerfile的中，先启动module模式，且配置代理，因为有些墙外的包服务没有梯子的情况下也是无法下载回来的，这里的代理域名是通用的代理，有需要的也可以用。(这里需要注意下如果是私有仓库的包,可以不配置代理,直接下载拉取即可)\n\n指令`RUN go mod download`执行的时候，会构建一层缓存，包含了该项所有的依赖。之后再次提交的代码中，若是go.mod、go.sum没有变化，就会直接使用该缓存，起到加速构建的作用，也不用重复的去外网下载依赖了。若是这两个文件发生了变化，就会重新构建这个缓存分层。\n\ndocker中,go构建命令使用 `-ldflags=\"-s -w\"`,在官方文档：Command_Line里面说名了-s -w参数的意义，按需选择即可。\n\n-s: 省略符号表和调试信息\n-w: 省略DWARF符号表\n\n使用scratch镜像,golang:latest开发镜像构建好应用后，在使用scratch来包裹生成二进制程序。\n\n这里关于最小基础镜像，在docker里面有这几类：\n\n* scratch: 空的基础镜像，最小的基础镜像.\n* busybox: 带一些常用的工具，方便调试， 以及它的一些扩展busybox:glibc.\n* alpine: 另一个常用的基础镜像，带包管理功能，方便下载其它依赖的包.\n\n然后可以运行上面的普通镜像的部署,编译,部署,运行镜像就可以访问和使用了.\n\n看完这个Dockerfile的内容，可能你的第一感觉是不是把之前的两个Dockerfile合并在一块儿了，每个Dockerfile单独作为一个“阶段”！但是这个Docker也多了一些新的语法形式，用于建立各个“阶段”之间的联系。\n\n针对这样一个Dockerfile，我们应该知道以下几点：\n\n* 支持Multi-stage build的Dockerfile在以往的多个build阶段之间建立内在连接，让后一个阶段构建可以使用前一个阶段构建的产物，形成一条构建阶段的chain；\n* Multi-stages build的最终结果仅产生一个image，避免产生冗余的多个临时images或临时容器对象，这正是我们所需要的：我们只要结果。\n\n多阶段镜像构建可以让开发者通过一个Dockerfile，一次性地、更容易地构建出size较小的image，体验良好并且更容易接入CI/CD等自动化系统。\n\n此外我们如果有私有仓库或者需要加代理请求的一些依赖包的时候需要配置下:\n```markdown\nexport GO111MODULE=on\nexport GOPROXY=https://goproxy.io,https://goproxy.cn,direct\nexport GOPRIVATE=*.private.com\n```\n\n```markdown\n\u003e vim .gitconfig\n\n  1 [core]\n  2         excludesfile = /Users/admin/.gitignore_global\n  3 [difftool \"sourcetree\"]\n  4         cmd = opendiff \\\"$LOCAL\\\" \\\"$REMOTE\\\"\n  5         path =\n  6 [mergetool \"sourcetree\"]\n  7         cmd = /Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh     \\\"$LOCAL\\\" \\\"$REMOTE\\\" -ancestor \\\"$BASE\\\" -merge \\\"$MERGED\\\"\n  8         trustExitCode = true\n  9 [user]\n 10         name =  keke\n 11         email = keke@gmail.com\n 12 [url \"git@gitlab.private.com:\"]\n 13         insteadOf = https://gitlab.private.com/\n 14 [commit]\n 15         template = /Users/admin/.stCommitMsg\n\n```\n这样配置之后,获取和更新项目配置就很容易了.\n\n然后运行编译镜像:\n```bash\n\u003e docker build -f Dockerfile -t spike .\n\nSending build context to Docker daemon  27.94MB\nStep 1/18 : FROM golang:latest as build\n ---\u003e be63d15101cb\nStep 2/18 : MAINTAINER keke\n ---\u003e Using cache\n ---\u003e 9c15ac5391c9\nStep 3/18 : ENV GOPROXY https://goproxy.io/\n ---\u003e Running in 554c68a34b25\nRemoving intermediate container 554c68a34b25\n ---\u003e 1fd761145c9f\nStep 4/18 : ENV GO111MODULE on\n ---\u003e Running in ab4b62e5ca0f\nRemoving intermediate container ab4b62e5ca0f\n ---\u003e 49eb1ddeecfa\nStep 5/18 : WORKDIR /go/cache\n ---\u003e Running in 1b0ac2e50f8e\nRemoving intermediate container 1b0ac2e50f8e\n ---\u003e 300c1af8833d\nStep 6/18 : ADD go.mod .\n ---\u003e 3f93208402e7\nStep 7/18 : ADD go.sum .\n ---\u003e 0ca487979fbd\nStep 8/18 : RUN go mod download\n ---\u003e Running in 7ce0d994e202\ngo: finding github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6\ngo: finding gopkg.in/yaml.v2 v2.2.2\ngo: finding github.com/golang/protobuf v1.2.0\ngo: finding github.com/jinzhu/gorm v1.9.2\ngo: finding github.com/ugorji/go/codec v0.0.0-20181206144755-e72634d4d386\ngo: finding github.com/mattn/go-isatty v0.0.4\ngo: finding github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a\ngo: finding github.com/mattn/go-sqlite3 v1.10.0\ngo: finding github.com/unrolled/render v0.0.0-20180914162206-b9786414de4d\ngo: finding github.com/BurntSushi/toml v0.3.1\ngo: finding github.com/gin-gonic/contrib v0.0.0-20181101072842-54170a7b0b4b\ngo: finding github.com/gin-gonic/gin v1.3.0\ngo: finding github.com/garyburd/redigo v1.6.0\ngo: finding gopkg.in/go-playground/validator.v8 v8.18.2\ngo: finding github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7\ngo: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405\nRemoving intermediate container 7ce0d994e202\n ---\u003e b992949a532e\nStep 9/18 : WORKDIR /go/release\n ---\u003e Running in 5705c728bba2\nRemoving intermediate container 5705c728bba2\n ---\u003e 78a099cbb46a\nStep 10/18 : ADD . .\n ---\u003e 55c28e43a2e6\nStep 11/18 : RUN GOOS=linux CGO_ENABLED=0 go build -ldflags=\"-s -w\" -installsuffix cgo -o spike .\n ---\u003e Running in 07af5e5dd2cf\nRemoving intermediate container 07af5e5dd2cf\n ---\u003e 49a3d84e4670\nStep 12/18 : FROM scratch as prod\n ---\u003e \nStep 13/18 : COPY --from=build /go/release/spike /\n ---\u003e d96e494782fc\nStep 14/18 : COPY --from=build /usr/share/zoneinfo/Asia/Shanghai /etc/localtime\n ---\u003e b2c81502bc8e\nStep 15/18 : COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt\n ---\u003e d70aaaf82f60\nStep 16/18 : COPY --from=build /go/release/spike /\n ---\u003e 97dd96b760f4\nStep 17/18 : EXPOSE 8080\n ---\u003e Running in b0129a1ae043\nRemoving intermediate container b0129a1ae043\n ---\u003e 088bdf206407\nStep 18/18 : CMD [\"/spike\"]\n ---\u003e Running in d593765e9432\nRemoving intermediate container d593765e9432\n ---\u003e e8febeee66ee\nSuccessfully built e8febeee66ee\nSuccessfully tagged spike:latest\n```\n这样镜像就编译成功了,可以开始使用了.\n\n#### Golang编程\n\n觉得此文章不错，支持我的话可以给我star ，:star:！如果有问题可以加我的微信`Sen0676`,也可以加入我们的交流群一起交流goalng技术！\n\n#### License\nThis is free software distributed under the terms of the MIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeke-li%2Fspike","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeke-li%2Fspike","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeke-li%2Fspike/lists"}