简体   繁体   中英

Error go.mod: no such file or directory Compiler docker compose local package

I have a problem when I build with docker compose an application with local dependencies to create a docker image.

My Dockerfile:

FROM golang:alpine AS build 

ENV GOPATH=$GOPATH
#GOPROXY
ENV GOPROXY=http://proxy.golang.org
ENV GO111MODULE=on


WORKDIR $GOPATH/src/github.com/julianskyline/motorcars-core-business

COPY . . 


# Set OS as linux
RUN GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go

FROM alpine
COPY --from=build $GOPATH/bin/github.com/julianskyline/motorcars-core-business $GOPATH/bin/github.com/julianskyline/motorcars-core-business
ENTRYPOINT [ "/go/bin/motorcars-core-business" ]
My go.mod

module github.com/julianskyline/motorcars-core-business

go 1.15

replace (
    github.com/julianskyline/errors => /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors
    github.com/julianskyline/motorcars-db => /home/julianmarin/proyectos/go/src/github.com/julianskyline/motorcars-db
    github.com/julianskyline/motorcars-models => /home/julianmarin/proyectos/go/src/github.com/julianskyline/motorcars-models)

Projects are in the same folder:

$GOPATH/src/github.com/julianskyline/errors $GOPATH/src/github.com/julianskyline/motorcars-core-business

enter image description here

The go build/run work fine.

Error sudo docker-compose build:

 Step 6/9 : RUN GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go
 ---> Running in 45227441dfdd
go: github.com/julianskyline/errors@v0.0.0-00010101000000-000000000000 (replaced by /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors): reading /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod: open /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod: no such file or directory
The command '/bin/sh -c GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go' returned a non-zero code: 1
ERROR: Service 'api' failed to build 

NOTE: The file /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod exists!

The Dockerfile is in $GOPATH/src/github.com/julianskyline/motorcars-core-business which means that the COPY. . COPY. . within it will only copy $GOPATH/src/github.com/julianskyline/motorcars-core-business into the docker image.

The go.mod contains replace directives that reference folders not under $GOPATH/src/github.com/julianskyline/motorcars-core-business (eg $GOPATH/src/github.com/julianskyline/errors ); this leads to compilation errors because those folders are not present within the docker image.

To resolve this you can:

  • Copy the entire julianskyline folder into the image (by moving Docker file into the parent folder, specifying the context on the command line or using docker-compose ).
  • Remove the replace directives and letting go pull the files from github.

Posting answer as this was requested in the comments; I believe the comments provided sufficient info for the OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM