Dockerfile包去

hjzp0vay  于 2023-06-05  发布在  Docker
关注(0)|答案(1)|浏览(138)

我有一个用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
r7s23pms

r7s23pms1#

问题是COPY main.go ./ this。
golang将如何查看依赖项/包?

模块缓存:检查包是否存在于modcache中*$GOPATH/pkg/mod**
*项目目录:如果在modcache中没有找到包,那么它将检查包是否相对于我们的项目模块根存在。(github.com/guilherm5/login-user/models -检查是否存在名为models的目录。)
*远程:如果在模块缓存或项目根目录中找不到包,则从远程存储库获取它。

在您的例子中,您已经在项目模块根目录中拥有了包(目录),但只将main.go复制到容器系统中。因此,当go mod tidy运行时,它无法看到缓存或根目录中的包。然后它试图从远程拉取它。

  • 我在远程仓库中有代码,那么为什么它不拉 *?因为仓库是私有的。
    解决方案

将所有文件和文件夹复制到容器系统

copy . .

问题就解决了您已经有了多阶段构建,它将只在最终生产构建中包含必要的文件。

相关问题