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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错