doutun1875
2018-04-12 05:46
浏览 85

如何使用我的本地go软件包构建docker镜像?

My main.go file's path: /gowork/src/dockerpkgmain/main.go

my package file's path: /gowork/src/dockerpkg/mult/mult.go

my docker files path: /gowork/src/dockerpkgmain/Dockerfile

main.go:

package main

import (
    "dockerpkg/mult"
    "fmt"
)

func main() {
    fmt.Println("From different pkg")
    mult.Multiple()
}

mult.go:

package mult

import (
    "flag"
    "fmt"
)

func Multiple() {

    first := flag.Int("f", 0, "placeholder")
    second := flag.Int("s", 0, "placeholder")
    flag.Parse()
    out := (*first) * (*second)
    fmt.Println(out)

}

Dockerfile:

FROM golang:1.9.1

COPY . /go/src/dockerpkg/mult 

WORKDIR /go/src/app
COPY . .

ADD . /go/src/dockerpkg/mult
RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]

ENTRYPOINT ["app", "-f=7", "-s=9"]

If I try

COPY . /go/src/dockerpkg/mult

I got this:

main.go:4:2: import "dockerpkg/mult" is a program, not an importable package

What must I put in my dockerfile to build my image without changing project structure?

图片转代码服务由CSDN问答提供 功能建议

我的main.go文件的路径: /gowork/src/dockerpkgmain/main.go

我的软件包文件的路径: /gowork/src/dockerpkg/mult/mult.go

我的docker文件路径: / gowork / src / dockerpkgmain / Dockerfile

  main.go:
 
package main 
 
import(  
“ dockerpkg / mult” 
“ fmt” 
)
 
func main(){
 fmt.Println(“来自其他pkg”)
 mult.Multiple()
} 
   
 
 

mult.go:

 包mult 
 
import(
“标志” 
“ fmt” 
)  
 
func Multiple(){
 
第一个:= flag.Int(“ f”,0,“占位符”)
第二个:= flag.Int(“ s”,0,“占位符”)
  flag.Parse()
 out:=(*第一)*(*第二)
 fmt.Println(out)
 
} 
   
 
 

Dockerfile :

  FROM golang:1.9.1 
 
COPY。  / go / src / dockerpkg / mult 
 
WORKDIR / go / src / app 
COPY。  。
 
ADD。  / go / src / dockerpkg / mult 
RUN进入-d -v ./...
RUN进入安装-v ./...

CMD [“ app”] 
 
ENTRYPOINT [“ app”,  “ -f = 7”,“-s = 9”] 
   
 
 

如果我尝试

  COPY。  / go / src / dockerpkg / mult 
   
 
 

我知道了:

main.go:4 :2:导入“ dockerpkg / mult”是一个程序,而不是可导入的 包

在不更改项目的情况下,我必须在dockerfile中放入什么来构建我的映像 结构?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougehe2022 2018-04-12 08:10
    已采纳

    According to your folder layouts, I guess your local $GOPATH is /gowork folder. In golang docker image, its $GOPATH is /go folder.

    You have to create the docker file in this location /gowork/src/Dockerfile, then put the following in it. It works fine in my environment with your code.

    FROM golang:1.9.1
    
    COPY ./dockerpkg /go/src/dockerpkg
    
    WORKDIR /go/src/app
    COPY ./dockerpkgmain .
    
    RUN go get -d -v ./...
    RUN go install -v ./...
    
    CMD ["app"]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题