7

I'm trying docker build -t test_1 . , but have this err:

package docker_test/mult: unrecognized import path "docker_test/mult" (import path does not begin with hostname)

The command '/bin/sh -c go get -d -v ./...' returned a non-zero code: 1

My dockerfile (path /gowork/src/Dockerfile):

FROM golang:1.9.1
COPY ./docker_test/mult /go/src/app

WORKDIR go/src/app
COPY ./docker_test/main.go .

RUN go get -d -v ./...
RUN go install -v ./...

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

main.go (path: gowork/src/docker_test/main.go)

package main

import (
    "docker_test/mult"
    "fmt"
)

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

mult.go (path: gowork/src/docker_test/mult/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)

}
2
  • can you try change your 1st copy to COPY ./docker_test /go/src/docker_test, If it works, then I will explain in my answer why It worked. Apr 19, 2018 at 7:04
  • Now error is: go install: no install location for directory /go/go/src/app outside GOPATH For more details see: 'go help gopath' The command '/bin/sh -c go install -v ./...' returned a non-zero code: 1
    – Mzia
    Apr 19, 2018 at 7:10

2 Answers 2

7

go get trying to find the package docker_test/mult into /go path. But, you have copied into /go/src/app. That's why go get can't find the package locally and assumes the package is from remote repository, eg, github, and throws error import path does not begin with hostname. So copy the docker_test/mult inside /go path.

Another concern is, when you use WORKDIR go/src/app, it creates go/src/app inside /go path, So finally the path becomes /go/go/src/app. So use absolute path ie, WORKDIR /go/src/app.

Try this dockerfile:

FROM golang:1.9.1
COPY ./docker_test/mult /go/src/docker_test/mult

WORKDIR /go/src/app
COPY ./docker_test/main.go .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]
ENTRYPOINT ["app", "-f=7", "-s=9"]
6
  • That works, thanks! Can you explain why..? I mean, I thought that this way (COPY ./docker_test /go/src/docker_test) I copied all files, includes main.go in docker_test...
    – Mzia
    Apr 19, 2018 at 7:24
  • @Mitch. I was still adding some concepts, while you tried. :) I now added the explanation. Does the changes makes sense? Apr 19, 2018 at 7:31
  • Yep, it's more clearly, thanks, but still doesn't understand why after COPY ./docker_test /go/src/docker_test AND copying main.go I don't have 2 main.go files in project(
    – Mzia
    Apr 19, 2018 at 7:40
  • 1
    @Mitch, that&'s a good catch. Yes there will be one extra main.go file inside /go/src/docker_test, but it is not creating problem because that file doesn't belong to any of project directory (workdir) or dependency folders. Although, it should not be copied. I overlooked it. The 1st copy should have been, COPY ./docker_test/mult /go/src/docker_test/mult Apr 19, 2018 at 7:59
  • 1
    Dockerfile needs to be in parent folder of the files you are trying to Copy. But, it is possible to keep dockerfile in seperate folder. In that case, your pointer needs to be in any parent folder of the files, then provide the the docker file location with file flag. So, in your case, from src folder, you can run this command: docker build -t example-tag . -f builder/Dockerfile Apr 19, 2018 at 13:02
1

Make sure you set the GOPATH, in your example import uses docker_test/mult, so in order compiler to resolve it place it into $GOPATH/docker_test/mult,

I have tweaked your Dockerfile, so you should be able to buld it

Dockerfile

FROM golang:1.9.1

ENV GOPATH /go

FROM golang:1.9.1
COPY ./docker_test /go/src/docker_test
COPY ./docker_test/main.go /go/src/app/main.go

WORKDIR /go/src/app

RUN go get -d -v ./...
RUN go install -v ./...


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.