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

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

Docker-compose 允许您利用预先存在的 docker 图像或从源构建。对于构建选项,official reference 需要

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

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

例如

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

失败并出现错误:

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

我尝试过的其他形式都没有成功:

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

示例:

---

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

测试:

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

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

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

这已通过 compose/config/config.py#L79-L85 确认:

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

我认为现在有更好的方法来做到这一点!

如果您想使用位于存储库内的 Dockerfile 并且存储库是 public,您最好的猜测是使用原始文件。

例如对于 https://github.com/certbot/certbot, you could use https://raw.githubusercontent.com/certbot/certbot/master/Dockerfile-dev

内的文件 Dockerfile_dev

然后在docker-compose中,像这样添加它,以便从远程位置使用Dockerfile。

certbot_dev:
  image: certbot-dev
  build: https://raw.githubusercontent.com/certbot/certbot/master/Dockerfile-dev

您可以在文件预览中单击名为 'Raw' 的按钮时找到原始 link:https://github.com/certbot/certbot/blob/master/Dockerfile-dev

很抱歉重提这个话题,但它是第一个出现的link,我在其他地方找不到任何其他信息。

如果您想从特定的存储库标签构建,您需要附加#tagname 比如

build: https://github.com/postgres/pgadmin4.git#REL-6_4

参见 docker documentation

也建立在@philipp-fock 的答案之上。只要原始 Dockerfile 不包含该存储库中的任何其他文件(无 COPY、ADD)

,就可以使用原始文件

正在使用