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

25

在Github仓库的非主分支上构建镜像是否可行?

例如,我有一个名为//github.com/myAccount/docker-myImage的仓库,并且有一个development分支,我想要用它来构建我的镜像。不幸的是,下面的命令似乎只允许从master分支构建:

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

以下是来自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.

也许有一个替代方案,像 docker build -t myAccount/myImage git://github.com/myAccount/docker-myImage:development 这样的吗?
4个回答

16

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

更多选项,请参阅docker构建命令参考


6

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

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

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


4
执行git clone -b branch something命令。其中,clone表示克隆代码库,-b表示指定分支,branch为分支名称,something为要克隆的代码库地址。 - Wolfgang

2

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

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

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


实际上,这并不起作用。例如,当我执行上述命令时,我收到了错误消息:“未找到存储库'https://github.com/fedora-cloud/Fedora-Dockerfiles/tree/master/apache/'”。 - modulitos

0

看起来你需要在 Docker 容器的 Git 实现可以从源中拉取之前创建分支:

git fetch
git checkout -b branch_name
git pull origin branch_name

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接