为GO创建的Docker开发环境

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

Docker Dev Environment for GO

问题

我想做什么

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

代码

// 目录结构
项目(绝对路径: /Users/[用户名]/project)
|--- app
|  |--- config
|  |  |___ config.go
|  |--- main.go
|  |___ config.ini 
|--- docker-compose.yml
|___ Dockerfile
// main.go
package main

import (
	"app/config"
	"fmt"
)

func main() {
	fmt.Println("Hello World")
	fmt.Println(config.Config.ApiKey)
	fmt.Println(config.Config.ApiSecrete)
}
// docker-compose.yml
version: '3.8'

services:
  app:
    build: .
    tty: true
    volumes:
    - ./app:/go/src/app
// Dockerfile
FROM golang:latest
RUN mkdir /go/src/app
WORKDIR /go/src/app
ENV GO111MODULE=on
ENV GOPATH /go
ADD ./app /go/src/app/

开发环境

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

GOPATH="/go"
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

// directory
project(absolute path: /Uses/[username]/project)
|--- app
|  |--- config
|  |  |___ config.go
|  |--- main.go
|  |___ config.ini 
|--- docker-compose.yml
|___ Dockerfile
// main.go
package main

import (
	"app/config"
	"fmt"
)

func main() {
	fmt.Println("Hello World")
	fmt.Println(config.Config.ApiKey)
	fmt.Println(config.Config.ApiSecrete)
}
// docker-compose.yml
version: '3.8'

services:
  app:
    build: .
    tty: true
    volumes:
    - ./app:/go/src/app

// Dockerfile
FROM golang:latest
RUN mkdir /go/src/app
WORKDIR /go/src/app
ENV GO111MODULE=on
ENV GOPATH /go
ADD ./app /go/src/app/

Dev Environment

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

GOPATH="/go"
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,让当前目录成为一个模块的根目录:

$ go mod init example.com/hello
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 设置为环境变量。

version: '3.8'

services:
  app:
    build: .
    tty: true
    volumes:
    - ./app:/go/src/app
    environment:
      - "GO111MODULE=on"
// 目录结构
project(绝对路径: /Uses/[username]/project)
|--- app
|  |--- config
|  |  |___ config.go
|  |--- main.go
|  |--- config.ini 
|  |--- go.mod
|  |___ go.sum  
|--- docker-compose.yml
|___ 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.

version: '3.8'

services:
  app:
    build: .
    tty: true
    volumes:
    - ./app:/go/src/app
    environment:
      - "GO111MODULE=on"
// directory
project(absolute path: /Uses/[username]/project)
|--- app
|  |--- config
|  |  |___ config.go
|  |--- main.go
|  |--- config.ini 
|  |--- go.mod
|  |___ go.sum  
|--- docker-compose.yml
|___ 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:

确定