使用 docker 时在 Goland 中配置 GOROOT

Configure GOROOT in Goland when using docker

我正在新的 GoLand IDE 中使用 Docker 构建一个 Go 项目。不幸的是,我无法正确配置我的 GOROOT,因此无法最大限度地使用 GoLand 期货。

我有以下 dockerfile:

FROM golang:1.9.3

# allows app_env to be set during build (defaults to empty string)
ARG app_env
# sets an environment variable to app_env argument, this way the variable will persist in the container for use in code
ENV APP_ENV $app_env


COPY ./ /go/src/github.com/Name/ProjectName/

WORKDIR /go/src/github.com/Name/ProjectName/app

# install all dependencies
RUN go get ./...

# build the binary
RUN go build

# Put back once we have an application
RUN app

EXPOSE 8080

我现在按如下方式执行我的项目:

  1. docker build -t project-name .
  2. docker run -it -v ~/project-dir:/go/src/github.com/Name/ProjectName/app

这行得通,但是,我找不到如何配置 GoLand 以将我的 Docker 图像 usr/local/go/bin 上的路径用于我的 GoRoot,这在 GoLand 中是否可行? (如果不是,为什么还要添加 'docker support'?)

自 2018.1.2 起,无法使用 GoLand 开发将源代码放在容器中的 Go 应用程序。您的应用程序的源代码、它的依赖项和 Go 本身需要安装在您的机器上。

如果有人对如何改进基于 Docker 的开发支持有任何想法,欢迎提出建议,请参阅:https://youtrack.jetbrains.com/issue/GO-3322

至于为什么还要在 IDE 中添加对 Docker 的支持?您可以启动容器,可以使用 docker compose,以及来自 IDE 的许多其他工具。但是,根据容器的工作方式,IDE 无法从容器中获取源并进行推断。

此外,您的容器不应在最终容器中包含 Go sources/workspace,以便它针对大小和部署速度进行优化。您可以将类似的东西用于 运行,但请注意,可能需要额外的工作。

FROM golang:1.9.3 as build-env

# allows app_env to be set during build (defaults to empty string)
ARG app_env
# sets an environment variable to app_env argument, this way the variable     will persist in the container for use in code
ENV APP_ENV $app_env

COPY ./ /go/src/github.com/Name/ProjectName/

WORKDIR /go/src/github.com/Name/ProjectName/app

# install all dependencies
RUN go get ./...

# build the binary
RUN go build -o /my_app

# final stage
FROM scratch

COPY --from=build-env /my_app /

# Put back once we have an application
CMD ["/my_app"]

EXPOSE 8080