Docker-compose: 总是收到 找不到指定的 Dockerfile

Docker-compose: always receive Cannot locate specified Dockerfile

我有以下项目结构:

WebApp
  -- docker-compose.yml
  -- Dockerfile
  -- docker
     -- docker-gc
        --Dockerfile

docker/docker-gc 是我在使用此命令之前创建的 git 子模块:

 git submodule add git@github.com:spotify/docker-gc.git docker/docker-gc

在 docker-compose 中,我尝试在 docker-gc 中 运行 Dockerfile。这是我的服务:

gc:
  container_name: docker-gc
  build: ./docker/docker-gc
  dockerfile: ./docker/docker-gc/Dockerfile
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /etc:/etc

但我总是收到错误消息:

Cannot locate specified Dockerfile ./docker/docker-gc/Dockerfile

我试了很多方法docker/docker-gc/Dockerfile/docker/docker-gc/Dockerfile都没有结果。这个问题是否与符号链接有关(由于 git 子模块性质)。请告诉我如何解决这个问题。

最后不用Dockerfile试试。默认应该是它使用指定文件夹中名为 "Dockerfile" 的文件。

错误在您的 docker-compose.yaml 文件中。在该格式的版本 1 中,您可以同时指定 builddockerfile。如果同时提供,dockerfile 属性表示预期在 build.

指定的目录中找到的文件名

在您的情况下,将 dockerfile 属性更改为仅包含文件名而不是与 build:

中相同的路径
gc:
  container_name: docker-gc
  build: ./docker/docker-gc
  dockerfile: Dockerfile
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /etc:/etc

由于 Dockerfile 是默认名称,您也可以简单地删除此行:

gc:
  container_name: docker-gc
  build: ./docker/docker-gc
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /etc:/etc

更多信息请参考Docker Compose documentation

另请注意,Docker Compose 文件格式的版本 2 使用不同的 build 参数概念。