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/
*/

修改 配置文件, 重新啟動實例,瀏覽器訪問

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM