Build Docker with Go app: cannot find package

前端 未结 6 732
独厮守ぢ
独厮守ぢ 2020-12-29 08:56

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

Dockerfi

6条回答
  •  天涯浪人
    2020-12-29 09:11

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

提交回复
热议问题