git Docker:如何从 Github 存储库上的非主分支构建映像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27683927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:39:04  来源:igfitidea点击:

Docker: how to build an image from a non-master branch on Github repository

gitdockergithubbranch

提问by modulitos

Is it possible to build an image from a non-master branch on a Github repository?

是否可以从 Github 存储库上的非主分支构建映像?

For example, I have the repository //github.com/myAccount/docker-myImagewith a branch developmentthat I would like to use for my image. Unfortunately, the following command only seems to allow building from the masterbranch:

例如,我有一个//github.com/myAccount/docker-myImage带有分支的存储库development,我想将其用于我的图像。不幸的是,以下命令似乎只允许从master分支构建:

docker build -t myAccount/myImage git://github.com/myAccount/docker-myImage

Here is the relevant documentation from man docker build:

以下是相关文档man docker build

Building an image using a URL

This will clone the specified Github repository from the URL and use it as context. The Dockerfile at the root of the repository is used as Dockerfile. This only works if the Github repository is a dedicated repository.

docker build github.com/scollier/Fedora-Dockerfiles/tree/master/apache

Note: You can set an arbitrary Git repository via the git:// schema.

使用 URL 构建图像

这将从 URL 克隆指定的 Github 存储库并将其用作上下文。存储库根目录下的 Dockerfile 用作 Dockerfile。这仅在 Github 存储库是专用存储库时才有效。

docker build github.com/scollier/Fedora-Dockerfiles/tree/master/apache

注意:您可以通过 git:// 架构设置任意 Git 存储库。

Perhaps there is an alternative, like docker build -t myAccount/myImage git://github.com/myAccount/docker-myImage:development?

也许有一个替代方案,比如docker build -t myAccount/myImage git://github.com/myAccount/docker-myImage:development

采纳答案by michielbdejong

docker build -t myAccount/myImage https://github.com/myAccount/docker-myImage.git#development

docker build -t myAccount/myImage https://github.com/myAccount/docker-myImage.git#development

See docker build command referencefor more options.

有关更多选项,请参阅docker build 命令参考

回答by manojlds

The doc that you quoted itself mentions how you can specify the branch:

您引用的文档本身提到了如何指定分支:

github.com/scollier/Fedora-Dockerfiles/tree/master/apache

Change tree/masterto the branch you want and see.

更改tree/master到您想要查看的分支。

回答by modulitos

I proposed this question on #dockerat the Freenode IRC and the user scolliercontacted me and he said that he'll get back to me about this issue. I believe he is involved with the Docker documentation that I mentioned in my questions. In the meantime, I found a workaround by adding the following to my Dockerfile:

#docker在 Freenode IRC上提出了这个问题,用户scollier联系了我,他说他会就这个问题回复我。我相信他参与了我在问题中提到的 Docker 文档。与此同时,我找到了一种解决方法,将以下内容添加到我的Dockerfile:

RUN git clone something && cd something && git checkout branch

This solution seems to solve all my needs. Thanks for the support!

这个解决方案似乎解决了我的所有需求。感谢您的支持!

回答by kamran

It seems you need to create the branch before the docker container's git implementation can pull it from origin:

似乎您需要在 docker 容器的 git 实现可以从原点拉取它之前创建分支:

git fetch
git checkout -b branch_name
git pull origin branch_name