容器運作完後退出
在Web開發中,容器是一種常見的技術,如Docker、Kubernetes等。它們能夠提供環境隔離和資源管理的功能,使應用程式能夠在不同的環境中運作。然而,有時候我們希望容器運行完畢後能夠自動退出,而不是一直保持運作狀態。那麼,如何實現容器運作完畢後自動退出呢?本文將為大家介紹一些實作方法和技巧。
問題內容
我的 golang fiber 伺服器在 google cloud run 上執行時會自動退出並顯示以下訊息:
container called exit(0).
我使用以下 dockerfile 來運行它
# use the offical golang image to create a binary. from golang:buster as builder # create and change to the app directory. workdir /app # retrieve application dependencies. copy go.mod ./ copy go.sum ./ run go mod download copy . ./ run go build # use the official debian slim image for a lean production container. # https://hub.docker.com/_/debian # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage- builds from debian:buster-slim run set -x && apt-get update && debian_frontend=noninteractive apt-get install -y \ ca-certificates && \ rm -rf /var/lib/apt/lists/* # copy the binary to the production image from the builder stage. copy --from=builder /app/redirect-middleware.git /app/ copy --from=builder /app/pkg /app/pkg/ expose 8080 # run the web service on container startup. cmd ["/app/redirect-middleware.git", "dev"]
和我的 main.go(僅 func main())
func main() { // Load env config c, err := config.LoadConfig() if err != nil { log.Fatalln("Failed at config", err) } // init DB db.InitDb() // init fiber API app := fiber.New() log.Print("Started new Fiber app...") // initial route sending version of API app.Get("/", func(c *fiber.Ctx) error { return c.SendString(fmt.Sprintf("Redirection middleware - v%s", viper.Get("Version").(string))) }) log.Print("Default root route set...") // api routes api := app.Group("/api") // /api v1 := api.Group("/v1") // /api/v1 log.Print("api/v1 group set...") // register routes v1 mastermenus.RegisterRoutes(v1) log.Print("Route registered...") app.Listen(c.Port) log.Print("Api started listening in port 8080") }
最後一行在 google cloud run 日誌中執行正常,我可以看到 api 開始偵聽連接埠 8080
。
為什麼我的容器會單獨退出?它應該啟動 fiber api。
解決方法
我發現了這個問題。在我的 stage.env
檔案中,我將連接埠設定為 :8080
。
在本地,傳遞 app.listen(c.port)
可以按預期很好地轉換為 app.listen(":8080")
。當在 cloud run 中使用它時,它會轉換為 app.listen("8080")
,這當然不起作用,因為它認為這是主機而不是連接埠。
我新增了 app.listen(":" c.port)
,它可以工作。
如果您遇到這種情況,請捕獲錯誤:
errApp := app.Listen(":" + c.Port) if errApp != nil { log.Printf("An error happened while running the api: %s", errApp) } else { log.Printf("Api started listening in port %s", c.Port) }
並採取相應行動。
以上是容器運作完後退出的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...
