Home  >  Article  >  Backend Development  >  Dockerfile package to

Dockerfile package to

WBOY
WBOYforward
2024-02-05 23:36:08379browse

Dockerfile 包去

Question content

I have an application written in go and gin. The file is divided as follows:

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

In each folder I have a package that calls another package, for example:

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"
)

My go.mod is like this:

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
)

So it follows my application flow..

I want to make a docker image of this application (for learning purposes, I want to learn the basics of docker, so I made this api for testing)

docker image:

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 occurred while trying to build the image:

=> 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

Does anyone know why? I really don't know how to solve this problem, how can I import my package into the docker image?

--edit

I updated my dockerfile as suggested:

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"]

mistake:

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 


Correct answer


The problem occurs at copy main.go ./ this.

How will golang view dependencies/packages?

  • Module Cache: Check if the package exists in modcache$gopath/pkg/mod
  • Project Directory: If the package is not found in modcache, then it will check if the package exists relative to our project module root. (github.com/guilherm5/login-user/models - Check if a directory named models exists.)
  • Remote: If the package is not found in the module cache or project root, get it from the remote repository.

In your case, you already have the package (directory) in your project module root, but only main.go is copied to the container system. Therefore, when go mod tidy is run, it cannot see the package in the cache or root directory. Then it tries to pull it from the remote.

I have the code in the remote repo, so why isn't it pulling ? Because the repository is private.

solution

Copy all files and folders to the container system

copy . .

The problem will be solved. You already have a multi-stage build with only the necessary files in the final production build.

Check

The above is the detailed content of Dockerfile package to. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete