英文:
Building a Docker container for Golang code: package PACKAGE_NAME is not in GOROOT
问题
我建立了一个小的 Golang 应用程序,我想在 Docker 容器中运行它。
我编写了以下的 Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY ./* .
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD ["/docker-gs-ping"]
然而,当我运行以下命令时:
docker build --tag docker-gs-ping .
我得到了以下错误:
> #16 0.560 found packages controllers (controller.go) and repositories (csv_file_repository.go) in /app
> #16 0.560 main.go:4:2: package MyExercise/controllers is not in GOROOT (/usr/local/go/src/MyExercise/controllers)
我想提一下,controllers 包存在于我的工作目录中,并且与该目录相关的所有文件都放在 MyExercise/controllers 文件夹中。
你知道如何解决这个错误吗?
编辑:
这是目录树:
.
├── Dockerfile
├── REDAME
├── controllers
│ └── controller.go
├── go.mod
├── go.sum
├── logging
│ └── logger.go
├── main.go
├── models
│ └── location.go
├── output.log
├── repositories
│ ├── csv_file_repository.go
│ ├── csv_file_repository_builder.go
│ ├── csv_file_repository_builder_test.go
│ ├── csv_file_repository_test.go
│ ├── repository_builder_interface.go
│ ├── repository_interface.go
│ └── resources
│ └── ip_address_list.txt
└── services
├── ip_location_service.go
├── ip_location_service_test.go
├── rate_limiter_service.go
├── rate_limiter_service_interface.go
├── rate_limiter_service_test.go
└── time_service.go
main.go 中的 import 部分:
import (
"MyExercise/controllers"
"MyExercise/logging"
"MyExercise/repositories"
"MyExercise/services"
"errors"
"github.com/gin-gonic/gin"
"os"
"strconv"
"sync"
)
英文:
I built a small Golang application and I want to run it on a Docker container.
I wrote the following Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY ./* .
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]
However, when I run the command:
docker build --tag docker-gs-ping .
I get the errors:
> #16 0.560 found packages controllers (controller.go) and repositories (csv_file_repository.go) in /app
> #16 0.560 main.go:4:2: package MyExercise/controllers is not in GOROOT (/usr/local/go/src/MyExercise/controllers)
I want to mention that the package controllers exists in my working directory and all files associated with this directory are placed in MyExercise/controllers folder.
Do you know how to resolve this error?
Edit:
This is the directory tree:
.
├── Dockerfile
├── REDAME
├── controllers
│   └── controller.go
├── go.mod
├── go.sum
├── logging
│   └── logger.go
├── main.go
├── models
│   └── location.go
├── output.log
├── repositories
│   ├── csv_file_repository.go
│   ├── csv_file_repository_builder.go
│   ├── csv_file_repository_builder_test.go
│   ├── csv_file_repository_test.go
│   ├── repository_builder_interface.go
│   ├── repository_interface.go
│   └── resources
│   └── ip_address_list.txt
└── services
├── ip_location_service.go
├── ip_location_service_test.go
├── rate_limiter_service.go
├── rate_limiter_service_interface.go
├── rate_limiter_service_test.go
└── time_service.go
import section in main.go:
import (
"MyExercise/controllers"
"MyExercise/logging"
"MyExercise/repositories"
"MyExercise/services"
"errors"
"github.com/gin-gonic/gin"
"os"
"strconv"
"sync"
)
答案1
得分: 2
你的 Dockerfile 实际上存在一个问题。
COPY ./* .
并不会像你想的那样工作。它会将所有文件递归地以扁平结构复制到 /app
目录中。
将你的 Dockerfile 修改为以下内容:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
ADD . /app
RUN go mod download
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD ["/docker-gs-ping"]
基本上,删除所有的 COPY
指令,并用一个单独的 ADD
指令替换。
英文:
There is actually an issue with your Dockerfile.
COPY ./* .
does not actually do what you think. It will copy all files recursively in a flat structure to the /app
directory.
Modify your Dockerfile to something like:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
ADD . /app
RUN go mod download
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]
Basically, remove all of the COPY
directives and replace with a single ADD
directive
答案2
得分: 0
在你的应用程序目录中执行go mod vendor
命令。文档。
要构建容器,请使用以下命令:docker build -t app:v1 .
Dockerfile内容如下:
FROM golang:1.16-alpine
WORKDIR /app/
ADD . .
RUN go build -o /app/main
EXPOSE 5055
CMD ["/app/main"]
英文:
Do go mod vendor
in your app directory. Documentaion.
For build the container docker build -t app:v1 .
Dockerfile
FROM golang:1.16-alpine
WORKDIR /app/
ADD . .
RUN go build -o /app/main
EXPOSE 5055
CMD [ "/app/main" ]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论