Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Docker with Go app: cannot find package

Tags:

docker

go

I have my Dockerfile in the root of directory with src/myapp folder, myapp contains myapp.go with main package.

Dockerfile looks like following:

FROM golang:1.9.2

ADD . /
RUN go build myapp;

ENTRYPOINT ["/go/bin/myapp"]

I get following error:

can't load package: package myapp: cannot find package "myapp" in any of:
    /usr/local/go/src/myapp (from $GOROOT)
    /go/src/myapp (from $GOPATH)

What am I doing wrong? Can I log ls command after docker has done ADD?

like image 567
Rudziankoŭ Avatar asked Dec 15 '17 17:12

Rudziankoŭ


2 Answers

You are copying all the files to Image root directory, Didn't installed any dependencies, Trying to Build it and then run the binary from /go/bin/app. The binary doesn't exists in that directory and it's generating errors.

I would recommend using a Dockerfile like this,

FROM golang:1.9.2 
ADD . /go/src/myapp
WORKDIR /go/src/myapp
RUN go get myapp
RUN go install
ENTRYPOINT ["/go/bin/myapp"]

This'll do the following.

  1. Copy project files to /go/src/myapp.
  2. Set Working directory to /go/src/myapp.
  3. Install dependencies, I used go get but replace it with which ever dependency management tool you are using.
  4. Install/build the binary.
  5. Set entry point.

You can run ls or any other command using docker exec.

Example:

docker exec <image name/hash> ls

You can also enter the shell in the generated image to understand it well using

docker run --rm -it <image hash/name> /bin/sh
like image 135
Ishan Jain Avatar answered Sep 25 '22 07:09

Ishan Jain


After experiments I've come to this way of building Golang apps.

This way has several advantages:

  • dependencies are installed on build stage

  • if you need you may uncomment test options

  • build first fully-functional image about 800 MB

  • copies your program to an fresh empty image and produces very small image about 10 MB

Dockerfile:

# Two-stage build:
#    first  FROM prepares a binary file in full environment ~780MB
#    second FROM takes only binary file ~10MB

FROM golang:1.9 AS builder

RUN go version

COPY . "/go/src/github.com/your-login/your-project"
WORKDIR "/go/src/github.com/your-login/your-project"

#RUN go get -v -t  .
RUN set -x && \
    #go get github.com/2tvenom/go-test-teamcity && \  
    go get github.com/golang/dep/cmd/dep && \
    dep ensure -v

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -o /your-app

CMD ["/your-app"]

EXPOSE 8000



#########
# second stage to obtain a very small image
FROM scratch

COPY --from=builder /your-app .

EXPOSE 8000

CMD ["/your-app"]
like image 25
Eugene Lisitsky Avatar answered Sep 21 '22 07:09

Eugene Lisitsky