go学习笔记 beego 的部署【windows 和docker】

windows下部署

在windows下和linux 下是一样的,windows 用bee pack -beGOOS=window 而linux 用 bee pack -beGOOS=linux -be GOARCH=amd64 ,通过bee创建的项目 默认是开发模式, 所以部署前需要修改 为runmode = prod, 这里我增加一个 配置 website="demo" ,在default.go controller里面使用 c.Data["Website"] = beego.AppConfig.String("website") , 目的是部署后我们一般都会修改配置文件,验证是否读取到正确的配置。

执行命令 bee pack -beGOOS=window 后会生成 对应的exe文件,我们现在创建一个 文件夹 test 来作为发布包, 里面必须包含exe文件, 需要把conf,static和views也拷贝到发布文件夹里面如,

我们修改app.conf 然后启动exe, 启动后验证正常【读取到了 新的配置文件】

可以利用nssm工具将exe打包成windows服务,然后启动服务即可。

如果出现找不到views目录的异常错误。这时需要在main.go入口文件,对配置文件、views目录等静态文件进行指定【我目前没有遇到过】

复制代码
package main

import (
    _ "demo/routers"
    "fmt"
    "os"
    "os/exec"
    "path/filepath"

    "github.com/astaxie/beego"
)

func main() {

    initConfig()
    beego.Run()

}

func initConfig() {
    rootPath := GetAPPRootPath()
    fmt.Println("应用调用路径:" + rootPath)
    beego.SetViewsPath(rootPath + "/views")
    beego.LoadAppConfig("ini", rootPath+"/conf/app.conf")
    beego.SetStaticPath("static", rootPath+"/static")
}

func GetAPPRootPath() string {
    file, err := exec.LookPath(os.Args[0])
    if err != nil {
        return ""
    }

    p, err := filepath.Abs(file)
    if err != nil {
        return ""
    }

    return filepath.Dir(p)
}
复制代码

Docker

1在项目根目录下创建Dockerfile文件, 如下:

复制代码
FROM golang:1.15.6

//正常这2句不需要,上网   逼得
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# Install beego and the bee dev tool*
RUN go get github.com/astaxie/beego && go get github.com/beego/bee

# Expose the application on port 8080
EXPOSE 8080

# Set the entry point of the container to the bee command that runs the
# application and watches for changes
CMD ["bee", "run"]
复制代码

2.构建镜像:

docker build -t demo-image .  //-t 是指tag的意思 构建了一个叫做demo-image的镜像

3..检查镜像并启动

复制代码
docker run -it --name demo-instance -p 8080:8080  -v /root/go/demo:/go/src/demo -w /go/src/demo demo-image
/*
--name:容器名字
--network:指定网络
--rm:容器停止自动删除容器
 
-i:--interactive,交互式启动
-t:--tty,分配终端
-v:--volume,挂在数据卷
-d:--detach,后台运行
-e, --env list             设置env
-u, --user string        指定用户 (格式: <name|uid>[:<group|gid>])
-w, --workdir string       指定工作目录
我的gopath 是 /root/go/
*/
复制代码

修改 配置文件, 重新启动实例,浏览器访问

 

 

posted on   dz45693  阅读(2342)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2014-01-01 SharePoint2010 对象模型 关联列表

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示