Mzia
Mzia

Reputation: 456

How to build docker image with my local go package?

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?

Upvotes: 1

Views: 2506

Answers (1)

Chun Liu
Chun Liu

Reputation: 943

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

Upvotes: 1

Related Questions