首頁 >後端開發 >Golang >Dockerfile 包去

Dockerfile 包去

WBOY
WBOY轉載
2024-02-05 23:36:08440瀏覽

Dockerfile 包去

問題內容

我有一個用go和gin寫的應用程序,這個檔案分割如下:

main.go 
    controller 
        controllers.go 
    database    
        database.go 
    middleware 
        middleware.go 
    models 
        models.go 
    routes 
        routes.go 
go.mod 
go.sum 
dockerfile

在每個資料夾中,我都有一個呼叫另一個套件的套件,例如:

package controller

import (
    "log"
    "net/http"
    "os"

    gomail "gopkg.in/mail.v2"

    "github.com/dgrijalva/jwt-go"
    "github.com/gin-gonic/gin"
    models "github.com/guilherm5/login-user/models"
)

package routes

import (
    "github.com/gin-gonic/gin"
    controller "github.com/guilherm5/login-user/controller"
    middleware "github.com/guilherm5/login-user/middleware"
)

我的 go.mod 是這樣的:

module github.com/guilherm5/login-user



go 1.20

require (
    github.com/dgrijalva/jwt-go v3.2.0+incompatible
    github.com/gin-gonic/gin v1.9.0
    github.com/golang-jwt/jwt v3.2.2+incompatible
    github.com/joho/godotenv v1.5.1
    github.com/lib/pq v1.10.9
    golang.org/x/crypto v0.9.0
    gopkg.in/mail.v2 v2.3.1
)

require (
    github.com/bytedance/sonic v1.8.0 // indirect
    github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/go-playground/locales v0.14.1 // indirect
    github.com/go-playground/universal-translator v0.18.1 // indirect
    github.com/go-playground/validator/v10 v10.11.2 // indirect
    github.com/goccy/go-json v0.10.0 // indirect
    github.com/json-iterator/go v1.1.12 // indirect
    github.com/klauspost/cpuid/v2 v2.0.9 // indirect
    github.com/leodido/go-urn v1.2.1 // indirect
    github.com/mattn/go-isatty v0.0.17 // indirect
    github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
    github.com/modern-go/reflect2 v1.0.2 // indirect
    github.com/pelletier/go-toml/v2 v2.0.6 // indirect
    github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
    github.com/ugorji/go/codec v1.2.9 // indirect
    golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
    golang.org/x/net v0.10.0 // indirect
    golang.org/x/sys v0.8.0 // indirect
    golang.org/x/text v0.9.0 // indirect
    google.golang.org/protobuf v1.28.1 // indirect
    gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)

所以它遵循我的應用程式流程..

我想製作這個應用程式的 docker 映像(出於學習目的,我想學習 docker 的基礎知識,所以我製作了這個 api 進行測試)

docker 映像:

from golang:1.20 as build 

workdir /mail 

copy go.sum ./
copy go.mod ./
copy main.go ./


run go mod download
run go build -o /appmail

from gcr.io/distroless/base-debian10

workdir /

copy --from=build /mail /mail

expose 4000/

user nonroot:nonroot 

entrypoint ["/appmail"]

嘗試建立映像時出現錯誤:

=> error [build 7/7] run go build -o /appmail             0.7s 
------
 > [build 7/7] run go build -o /appmail:
#13 0.680 main.go:5:2: no required module provides package github.com/guilherm5/login-user/routes; to add it:
#13 0.680       go get github.com/guilherm5/login-user/routes   
------
executor failed running [/bin/sh -c go build -o /appmail]: exit code: 1

有人知道為什麼嗎?我真的不知道該怎麼解決這個問題,如何將我的套件匯入到 docker 映像中?

--編輯

我按照建議更新了我的 dockerfile:

from golang:1.20 as build 

workdir /mail 

copy go.sum ./
copy go.mod ./
copy main.go ./

run go mod tidy
run go mod download
run go build -o /appmail

from gcr.io/distroless/base-debian10

workdir /

copy --from=build /mail /mail

expose 4000/

user nonroot:nonroot 

entrypoint ["/appmail"]

錯誤:

github.com/guilherm5/login-user imports
#12 9.121       github.com/guilherm5/login-user/routes: cannot find module providing package github.com/guilherm5/login-user/routes: module github.com/guilherm5/login-user/routes: git ls-remote -q origin in /go/pkg/mod/cache/vcs/a7be81f5c5695c6941aa1b7f5a49aa0800b2d648c4d72609eb5feae6f51cf505: exit status 128:
#12 9.121       fatal: could not read Username for 'https://github.com': terminal prompts disabled
#12 9.121 Confirm the import path was entered correctly.        
#12 9.121 If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
------
executor failed running [/bin/sh -c go mod tidy]: exit code: 1 


正確答案


問題發生在 copy main.go ./ this 上。

golang 將如何查看依賴項/套件?

  • 模組快取:檢查modcache中是否存在軟體包$gopath/pkg/mod
  • 專案目錄:如果在 modcache 中找不到該套件,那麼它將檢查該套件是否相對於我們的專案模組根目錄存在。 (github.com/guilherm5/login-user/models - 檢查名為 models 的目錄是否存在。)
  • 遠端:如果在模組快取或專案根目錄中找不到包,則從遠端儲存庫中取得它。

在您的情況下,您的專案模組根目錄中已經有套件(目錄),但僅將 main.go 複製到容器系統。 因此,當 go mod tidy 運行時,它無法在快取或根目錄中看到套件。然後它嘗試從遠端拉取它。

我在遠端倉庫中有程式碼,那麼為什麼它沒有拉動? 因為存儲庫是私有的。

解決方案

將所有檔案和資料夾複製到容器系統

copy . .

問題將會解決。您已經有了多階段構建,其中只有最終生產構建中必需的文件。

查看

以上是Dockerfile 包去的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除