Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build docker image with local package inside folder with main.go?

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)

}
like image 895
Mzia Avatar asked Apr 19 '18 06:04

Mzia


People also ask

Can docker image access local files?

Yes, you can do this. What you are describing is a bind mount. See https://docs.docker.com/storage/bind-mounts/ for documentation on the subject. Now, /mnt/mydata inside the container will have access to /Users/andy/mydata on my host.

Where do docker build images go?

The images are stored in /var/lib/docker/graph/<id>/layer . Note that images are just diffs from the parent image. The parent ID is stored with the image's metadata /var/lib/docker/graph/<id>/json .

How do I build a docker image from a local file?

One way to build a Docker image from our local files is to compress those files into a tar archive first. tar, err := archive.TarWithOptions ("node-hello/", &archive.TarOptions {}) if err != nil { return err } Note that the image tag includes our Docker registry user ID, so we can push this image to our registry later.

How to install go mod inside a docker image?

We’ll copy the go.mod and go.sum file into our project directory /app which, owing to our use of WORKDIR, is the current directory (.) inside the image. COPY go.mod ./ COPY go.sum ./ Now that we have the module files inside the Docker image that we are building, we can use the RUN command to execute the command go mod download there as well.

What is a dockerfile and how to create one?

A Dockerfile is a text document that contains the instructions to assemble a Docker image. When we tell Docker to build our image by executing the docker build command, Docker reads these instructions, executes them, and creates a Docker image as a result. Let’s walk through the process of creating a Dockerfile for our application.

What is the Docker build command?

The docker build command creates Docker images from the Dockerfile and a “context”. A build context is the set of files located in the specified path or URL. The Docker build process can access any of the files located in the context. The build command optionally takes a --tag flag.


2 Answers

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"]
like image 199
Abdullah Al Maruf - Tuhin Avatar answered Nov 14 '22 21:11

Abdullah Al Maruf - Tuhin


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"]
like image 37
Adrian Avatar answered Nov 14 '22 22:11

Adrian