推荐的Docker项目结构

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

Recommended Project Structure for Docker

问题

我正在处理一个具有以下结构(示意图)的Go项目:

./lib1
./prog1
    ./...
./prog2
    ./...

其中:

  • lib1是一个库
  • prog1和prog2是可执行文件(两者都依赖于lib1)

**简而言之:**我应该如何为prog1和prog2创建Dockerfile?


我尝试了几种方法,但都没有成功:

在prog1/中创建Dockerfile

失败了,因为docker无法将../lib1添加到容器中,因为它超出了上下文(参见http://docs.docker.com/reference/builder/#add)。

在根目录中创建Dockerfile

忽略我实际上需要两个Dockerfile(一个用于prog1,一个用于prog2)的事实,我尝试将Dockerfile放在项目的根目录中。

然而,我需要使用的docker镜像(golang:1.4.1-onbuild)无法找到任何Go文件(因为它们在./prog1中,而不是在根目录中):

+ exec go install -v
can't load package: package app: no buildable Go source files in /go/src/app

对于golang:1.4.1-onbuild镜像,推荐的项目结构是什么?


**编辑:**更好地在这里提问:https://stackoverflow.com/questions/29209202/dockerizing-gos-hello-world

英文:

I'm working on a Go project that has the following structure (schematic):

./lib1
./prog1
    ./...
./prog2
    ./...

Where:

  • lib1 is a library
  • prog1 and prog2 are executables (both depend on lib1)

In a nutshell: How do I create Dockerfiles for prog1 and prog2?


I tried several approaches, all to no avail:

Creating a Dockerfile in prog1/

Failed because docker cannot ADD ../lib1 to the container since it is out of the context (see http://docs.docker.com/reference/builder/#add).

Creating a Dockerfile in the root directory

Ignoring the fact that I actually need two Dockerfiles (for prog1, and for prog2), I tried placing the Dockerfile in the root directory of the project.

However, the docker image I need to use (golang:1.4.1-onbuild) fails to find any Go files (since they are in ./prog1, and not in the root):

+ exec go install -v
can't load package: package app: no buildable Go source files in /go/src/app

What is the recommended project structure for the golang:1.4.1-onbuild image?


Edit: Better asked here: https://stackoverflow.com/questions/29209202/dockerizing-gos-hello-world

答案1

得分: 1

大多数人通过版本控制系统(VCS)或者只是使用go get来拉取文件,而不是使用ADD命令。无论使用哪种构建工具,你都可以以相同的方式添加源代码树,因为你需要保持每个工具的$GOPATH结构相同。

注意,你也可以通过docker build -f命令来指定Dockerfile的名称,所以如果你想使用ADD命令,你可以在根目录下拥有多个DOCKERFILE文件。

英文:

Most pull the files in via their VCS or just go get instead of using ADD. You add the the source tree in the same way regardless of what tool your building, since you need the structure of $GOPATH to be the same for each.

Note, you can also name your docker file via docker build -f, so if you want to use ADD, you can have multiple DOCKERFILEs in your root directory.

huangapple
  • 本文由 发表于 2015年3月23日 03:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29198603.html
匿名

发表评论

匿名网友

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

确定