教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 使用docker部署一个带配置文件的golang项目

使用docker部署一个带配置文件的golang项目

发布时间:2021-12-15   编辑:jiaochengji.com
教程集为您提供使用docker部署一个带配置文件的golang项目等资源,欢迎您收藏本站,我们将为您提供最新的使用docker部署一个带配置文件的golang项目资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg>

<h3>使用docker部署一个带配置文件的golang项目</h3> <ul><li><ul><li>首先看下我这的目录结构</li><li>编写dockerfile</li><li>生成镜像</li><li>完整文件代码</li><li>谢谢支持 附上git地址</li></ul></li></ul>

配置文件放docker内只做为举例,实际并不推荐,建议用配置文件统一管理。

<h2>首先看下我这的目录结构</h2>


我这的gopath为 gowork目录

<h2>编写dockerfile</h2>

首先编译main.go 生成二进制文件,该二进制文件可以直接在相应的linux服务器下运行。
我这里使用如下指令,编译后会多出一个main文件

<pre><code>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go </code></pre>

可以根据自己需要编译的平台更改
使用from加入母镜像 这里使用scratch空镜像,因为编译后的main是可以直接运行的

<pre><code>FROM scratch </code></pre>

MAINTAINER指定维护者信息
WORKDIR .这里使用当前目录作为工作目录,可以修改
将main 与 test.toml 配置文件 放入当前目录
EXPOSE 这是对方开发的端口,可修改,我这使用8082
CMD 附带配置文件test.toml 运行当前目录下的main

<pre><code>MAINTAINER "hcf" WORKDIR . ADD main . ADD test.toml . EXPOSE 8082 CMD ["./main","-config=./test.toml"] </code></pre>

容器的配置就完成了

<h2>
生成镜像</h2>

在dockerfile目录运行

<pre><code>docker build -t dockertest . </code></pre>


successfully built 构建成功

使用

<pre><code>docker images </code></pre>

查看刚生成的镜像

运行镜像

<pre><code>docker run -p 8082:8082 dockertest </code></pre>



成功运行,页面输入http://localhost:8082/ 成功访问

<h2>
完整文件代码</h2>

config.go

<pre><code>package config import ( "github.com/BurntSushi/toml" ) // Config 对应配置文件结构 type Config struct { Listen string `toml:"listen"` } // UnmarshalConfig 解析toml配置 func UnmarshalConfig(tomlfile string) (*Config, error) { c := &Config{} if _, err := toml.DecodeFile(tomlfile, c); err != nil { return c, err } return c, nil } // GetListenAddr 监听地址 func (c Config) GetListenAddr() string { return c.Listen } </code></pre>

main.go

<pre><code>package main import ( "flag" "net/http" "test/dockertest/config" "github.com/gin-gonic/gin" "github.com/go-xweb/log" ) var ( tomlFile = flag.String("config", "test.toml", "config file") ) func indexHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": "docker test", }) } func main() { flag.Parse() // 解析配置文件 tomlConfig, err := config.UnmarshalConfig(*tomlFile) if err != nil { log.Errorf("UnmarshalConfig: err:%v\n", err) return } router := gin.New() router.GET("/", indexHandler) router.Run(tomlConfig.GetListenAddr()) } </code></pre>

test.toml

<pre><code>listen = ":8082" </code></pre>

dockerfile

<pre><code>FROM scratch MAINTAINER "hcf" WORKDIR . ADD main . ADD test.toml . EXPOSE 8082 CMD ["./main","-config=./test.toml"] </code></pre> <h2>
谢谢支持 附上git地址</h2>

https://github.com/516134941/docker-golang-demo 如果觉得有帮助可以star一下

到此这篇关于“使用docker部署一个带配置文件的golang项目”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
使用docker部署一个带配置文件的golang项目
php用什么容器部署
docker提供api访问的方法介绍
超详细分析php docker的原理及作用
golang微服务框架对比_Golang 微服务教程(二)
使用Docker部署PHP开发环境的方法详解
Docker LNMP Jenkins 码云实现 PHP 代码自动化部署
搭建基于consul,registrator,nsq的GO体系Docker开发环境
避坑!用 Docker 搞定 PHP 开发环境搭建
安装docker和docker-compose

[关闭]
~ ~