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?

  • 写回答

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"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 微信小程序蓝牙数据透传
  • ¥15 加氢站氢负荷数据集来源
  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据