Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang在Docker映像中找不到包_Docker_Go - Fatal编程技术网

Golang在Docker映像中找不到包

Golang在Docker映像中找不到包,docker,go,Docker,Go,因此,我试图将一个golang应用程序与包含主文件补充代码的不同目录进行dockerize。 我正在使用gorilla/mux。目录结构如下所示 $GOPATH/src/github.com/user/server |--- Dockerfile |--- main.go |--- routes/ handlers.go |--- public/ index.gohtml 它在我的主机上工作没有问题。问题是,当我尝试部署docke

因此,我试图将一个golang应用程序与包含主文件补充代码的不同目录进行dockerize。 我正在使用gorilla/mux。目录结构如下所示

$GOPATH/src/github.com/user/server
  |--- Dockerfile 
  |--- main.go 
  |--- routes/ 
         handlers.go 
  |--- public/ 
         index.gohtml 
它在我的主机上工作没有问题。问题是,当我尝试部署docker映像时,它不会运行,并在创建后不久退出。我已尝试将dockerfile中的WORKDIR命令更改为/go/src,并将所有文件转储到那里,但仍然没有成功。我还尝试了官方文件。也不行

我的Dockerfile

FROM golang:latest  
WORKDIR /go/src/github.com/user/server
COPY . .
RUN go get -d github.com/gorilla/mux

EXPOSE 8000
CMD ["go","run","main.go"]
我的golangmain.go

main.go:5:2: cannot find package "github.com/user/server/routes" in any of:
    /usr/local/go/src/github.com/user/server/routes (from $GOROOT)
    /go/src/github.com/user/server/routes (from $GOPATH)
主程序包
进口(
“github.com/gorilla/mux”
“github.com/user/server/routes”
“日志”
“net/http”
“时间”
)
func main(){
//... 
}
我在检查docker映像的日志时收到此错误消息

错误消息

main.go:5:2: cannot find package "github.com/user/server/routes" in any of:
    /usr/local/go/src/github.com/user/server/routes (from $GOROOT)
    /go/src/github.com/user/server/routes (from $GOPATH)

尝试以下
Docker
文件:

# GO Repo base repo
FROM golang:1.12.0-alpine3.9 as builder

RUN apk add git

# Add Maintainer Info
LABEL maintainer="<>"

RUN mkdir /app
ADD . /app
WORKDIR /app

COPY go.mod go.sum ./

# Download all the dependencies
RUN go mod download

COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# GO Repo base repo
FROM alpine:latest

RUN apk --no-cache add ca-certificates curl

RUN mkdir /app

WORKDIR /app/

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main .

# Expose port 8000
EXPOSE 8000

# Run Executable
CMD ["./main"]
#去回购基础回购
来自golang:1.12.0-alpine3.9作为建筑商
运行apk添加git
#添加维护人员信息
标签维护器=“”
运行mkdir/app
加上/应用程序
WORKDIR/app
复制go.mod go.sum/
#下载所有依赖项
运行go mod下载
复制
#构建Go应用程序
运行CGO_ENABLED=0 GOOS=linux go build-a-installsuffix CGO-o main。
#GO回购基础回购
来自阿尔卑斯山:最新
运行apk--无缓存添加ca证书
运行mkdir/app
WORKDIR/app/
#从上一阶段复制预构建的二进制文件
复制--from=builder/app/main。
#暴露端口8000
曝光8000
#运行可执行文件
CMD[“/main”]
在这里,我们正在创建一个中间的
docker-builder
容器,将代码复制到其中,在
builder
容器中构建代码,然后将
binary
图像复制到实际的docker


这将有助于在最终容器中包含所有依赖项,并且最终图像的大小将非常小

您是否尝试在图像中运行
/
ls-R
?这与问题中的内容有何不同,为什么会有帮助?(需要注意的是,问题似乎不是使用Go模块。)这将
下载
Go.mod
中提到的所有依赖项,因此不必手动安装任何依赖项。(我尝试过类似于修复依赖项错误的方法。希望对OP也有帮助)是的,这很有效。非常感谢。我没有意识到go.mod,我将进一步研究它。我只需要编写
go mod init
go mod tidy
来创建附加文件。没有其他选项不需要一直重建图像吗?