如何让 docker-compose 从远程 git 存储库构建映像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34120504/
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-09 03:45:13  来源:igfitidea点击:

How can I make docker-compose build an image from a remote git repository?

gitdockerdocker-compose

提问by billkw

Docker-compose allows you to utilize either pre?xisting docker images or build from source. For the build option, the official referencerequires

Docker-compose 允许您使用预先存在的 docker 镜像或从源代码构建。对于构建选项,官方参考要求

Either a path to a directory containing a Dockerfile, or a url to a git repository.

包含 Dockerfile 的目录的路径,或 git 存储库的 url

I'd like to take advantage of the latter case, so that I don't have to create a git submodule in my project, or register a new repository on Docker Hub. Unfortunately, there are no examples for how to format the url, and every form I've tried is mistaken for a relative file path.

我想利用后一种情况,这样我就不必在我的项目中创建一个 git 子模块,或者在 Docker Hub 上注册一个新的存储库。不幸的是,没有关于如何格式化 url 的示例,而且我尝试过的每种形式都被误认为是相对文件路径。

e.g.

例如

---
letsencrypt:
  build: https://github.com/letsencrypt/letsencrypt.git
...

Fails with the error:

因错误而失败:

ERROR: build path /{MY_CURRENT_PATH}/https:/github.com/letsencrypt/letsencrypt.git either does not exist or is not accessible.

错误:构建路径 /{MY_CURRENT_PATH}/https:/github.com/letsencrypt/letsencrypt.git 不存在或不可访问。

I didn't have any more luck with the other forms I've tried:

我尝试过的其他形式没有更多的运气:

采纳答案by Andy Shinn

Are you running version 1.5.2? It looks like this was actually recently added in https://github.com/docker/compose/pull/2430. Try upgrading.

您运行的是 1.5.2 版吗?看起来这实际上是最近在https://github.com/docker/compose/pull/2430 中添加的。尝试升级。

Example:

例子:

---

version: '2'

services:
  redis:
    image: "redis:3.2.3"
    hostname: redis

  redis-commander:
    build: https://github.com/joeferner/redis-commander.git
    command: --redis-host redis
    links:
      - "redis:redis"
    ports:
      - 8081

Tested with:

测试:

$ docker-compose -v
docker-compose version 1.11.2, build dfed245

回答by VonC

The file tests/unit/config/config_test.pyshows:

该文件tests/unit/config/config_test.py显示:

def test_valid_url_in_build_path(self):
    valid_urls = [
        'git://github.com/docker/docker',
        '[email protected]:docker/docker.git',
        '[email protected]:atlassianlabs/atlassian-docker.git',
        'https://github.com/docker/docker.git',
        'http://github.com/docker/docker.git',
        'github.com/docker/docker.git',
    ]

This is confirmed with compose/config/config.py#L79-L85:

这证实了compose/config/config.py#L79-L85

DOCKER_VALID_URL_PREFIXES = (
    'http://',
    'https://',
    'git://',
    'github.com/',
    'git@',
)