为GO创建的Docker开发环境

huangapple go评论129阅读模式
英文:

Docker Dev Environment for GO

问题

我想做什么

我想为Go设置Docker开发环境。

代码

  1. // 目录结构
  2. 项目(绝对路径: /Users/[用户名]/project)
  3. |--- app
  4. | |--- config
  5. | | |___ config.go
  6. | |--- main.go
  7. | |___ config.ini
  8. |--- docker-compose.yml
  9. |___ Dockerfile
  1. // main.go
  2. package main
  3. import (
  4. "app/config"
  5. "fmt"
  6. )
  7. func main() {
  8. fmt.Println("Hello World")
  9. fmt.Println(config.Config.ApiKey)
  10. fmt.Println(config.Config.ApiSecrete)
  11. }
  1. // docker-compose.yml
  2. version: '3.8'
  3. services:
  4. app:
  5. build: .
  6. tty: true
  7. volumes:
  8. - ./app:/go/src/app
  1. // Dockerfile
  2. FROM golang:latest
  3. RUN mkdir /go/src/app
  4. WORKDIR /go/src/app
  5. ENV GO111MODULE=on
  6. ENV GOPATH /go
  7. ADD ./app /go/src/app/

开发环境

当我运行 docker-compose exec app go env 时,我得到以下输出:

  1. GOPATH="/go"
  2. GOROOT="/usr/local/go"

问题

当我运行 docker-compose up -d --build 时,我得到以下错误信息:package app/config is not in GOROOT (/usr/local/go/src/app/config)。因此,我无法在 main.go 文件中导入 app/config

我想知道在使用Docker设置GO开发环境时如何导入自定义包。

英文:

What I want to do

I want to set up docker dev environment fo Go.

Code

  1. // directory
  2. project(absolute path: /Uses/[username]/project)
  3. |--- app
  4. | |--- config
  5. | | |___ config.go
  6. | |--- main.go
  7. | |___ config.ini
  8. |--- docker-compose.yml
  9. |___ Dockerfile
  1. // main.go
  2. package main
  3. import (
  4. "app/config"
  5. "fmt"
  6. )
  7. func main() {
  8. fmt.Println("Hello World")
  9. fmt.Println(config.Config.ApiKey)
  10. fmt.Println(config.Config.ApiSecrete)
  11. }
  1. // docker-compose.yml
  2. version: '3.8'
  3. services:
  4. app:
  5. build: .
  6. tty: true
  7. volumes:
  8. - ./app:/go/src/app
  1. // Dockerfile
  2. FROM golang:latest
  3. RUN mkdir /go/src/app
  4. WORKDIR /go/src/app
  5. ENV GO111MODULE=on
  6. ENV GOPATH /go
  7. ADD ./app /go/src/app/

Dev Environment

When I run docker-compose exec app go env, I get

  1. GOPATH="/go"
  2. GOROOT="/usr/local/go"

Problem

When I run docker-compose up -d --build,
I get
package app/config is not in GOROOT (/usr/local/go/src/app/config).
So, I can't import "app/config" in main.go file.

I want to know how import self-made package when I set up GO dev environment with docker.

答案1

得分: 1

你可以参考go-env-series,了解如何使用Docker来定义你的Go开发环境的代码。例如:chris-crone/containerized-go-dev

第二篇文章提到了go mod init,它有助于处理导入路径:

通过使用go mod init,让当前目录成为一个模块的根目录:

  1. $ go mod init example.com/hello
  2. go: creating new go.mod: module example.com/hello

go.mod文件只会出现在模块的根目录中。

子目录中的包的导入路径由模块路径加上子目录的路径组成

例如,如果我们创建了一个名为world的子目录,我们不需要(也不应该)在那里运行go mod init。该包会自动被识别为example.com/hello模块的一部分,其导入路径为example.com/hello/world

英文:

You can follow the go-env-series about how to use Docker to define your Go development environment in code.
Example: chris-crone/containerized-go-dev

The second article does mention go mod init, which helps with import path:

> Let’s make the current directory the root of a module by using go mod init:
>
> $ go mod init example.com/hello
> go: creating new go.mod: module example.com/hello
>
> The go.mod file only appears in the root of the module.
>
> Packages in subdirectories have import paths consisting of the module path plus the path to the subdirectory.
>
> For example, if we created a subdirectory world, we would not need to (nor want to) run go mod init there.
The package would automatically be recognized as part of the example.com/hello module, with import path example.com/hello/world.

答案2

得分: 1

感谢你,我已经解决了这个问题,所以我会分享给你。

我运行了 go mod init appgo mod tidy

然后,我修改了 Dockerfile,将 GO111MODULE=on 设置为环境变量。

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. tty: true
  6. volumes:
  7. - ./app:/go/src/app
  8. environment:
  9. - "GO111MODULE=on"
  1. // 目录结构
  2. project(绝对路径: /Uses/[username]/project)
  3. |--- app
  4. | |--- config
  5. | | |___ config.go
  6. | |--- main.go
  7. | |--- config.ini
  8. | |--- go.mod
  9. | |___ go.sum
  10. |--- docker-compose.yml
  11. |___ Dockerfile

参考:
1
2

英文:

Thanks to you, I've solved this problem, so I'll share.

I run go mod init app, and go mod tidy.

Then, I change Dockerfile to set GO111MODULE=on.

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. tty: true
  6. volumes:
  7. - ./app:/go/src/app
  8. environment:
  9. - "GO111MODULE=on"
  1. // directory
  2. project(absolute path: /Uses/[username]/project)
  3. |--- app
  4. | |--- config
  5. | | |___ config.go
  6. | |--- main.go
  7. | |--- config.ini
  8. | |--- go.mod
  9. | |___ go.sum
  10. |--- docker-compose.yml
  11. |___ Dockerfile

cf.
1
2

huangapple
  • 本文由 发表于 2021年11月2日 22:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69811858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定