我有一个用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 将如何查看依赖项/包?
models
的目录是否存在。)在您的情况下,您的项目模块根目录中已经有包(目录),但仅将 main.go
复制到容器系统。
因此,当 go mod tidy
运行时,它无法在缓存或根目录中看到包。然后它尝试从远程拉取它。
我在远程仓库中有代码,那么为什么它没有拉动? 因为存储库是私有的。
解决方案
将所有文件和文件夹复制到容器系统
copy . .
问题将会得到解决。您已经有了多阶段构建,其中只有最终生产构建中必需的文件。
查看
以上是Dockerfile 包去的详细内容。更多信息请关注PHP中文网其他相关文章!