Docker 与图像组合并构建都在单个服务错误中指定?

Docker compose with image and build both specified in a single service bug?

我了解到如果我在目录中有以下内容

Docker文件

FROM fedora
ENV VAR=42
CMD ["bash"]

和下面的合成文件

docker-compose.yml

version: "3.0"
services:
  app:
    build: .
    image: ubuntu

Docker 应该从 fedora 构建一个图像并将其命名为 ubuntu。但是它似乎只是拉取 ubuntu 图像(如果我注释掉 image: ubuntu 它会拉取软呢帽图像)。这是为什么?

docker docs: If the image does not exist, Compose attempts to pull it, unless you have also specified build, in which case it builds it using the specified options and tags it with the specified tag.

我测试过它对我有用,但我确实需要更改撰写文件的内容以使服务成为地图,因为您输入的是我得到的:“services.build 必须是地图”。

version: "3.0"
services:
  so_test:
    build: .
    image: ubuntu

但是我的Docker文件是一样的:

FROM fedora
ENV VAR=42
CMD ["bash"]

构建日志:

[+] Running 2/2
 ⠿ so_test Pulled                                                                                                                                                                                                                                9.3s
   ⠿ 5f3d23ccb99f Pull complete                                                                                                                                                                                                                  3.8s
[+] Building 8.0s (6/6) FINISHED                                                                                                                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                                                                                                                             0.0s
 => => transferring dockerfile: 72B                                                                                                                                                                                                              0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                0.0s
 => => transferring context: 2B                                                                                                                                                                                                                  0.0s
 => [internal] load metadata for docker.io/library/fedora:latest                                                                                                                                                                                 1.3s
 => [auth] library/fedora:pull token for registry-1.docker.io                                                                                                                                                                                    0.0s
 => [1/1] FROM docker.io/library/fedora@sha256:40ba585f0e25c096a08c30ab2f70ef3820b8ea5a4bdd16da0edbfc0a6952fa57                                                                                                                                  6.6s
 => => resolve docker.io/library/fedora@sha256:40ba585f0e25c096a08c30ab2f70ef3820b8ea5a4bdd16da0edbfc0a6952fa57                                                                                                                                  0.0s
 => => sha256:cfe295ee216d5995136f055659294ebc2dcbd736f94c5ac14dffb39fa2eebb27 529B / 529B                                                                                                                                                       0.0s
 => => sha256:788739295edac7b7b34351db644c8a5506e7de56af398985c59bae8d87d45a1e 2.00kB / 2.00kB                                                                                                                                                   0.0s
 => => sha256:3f28ea9d8c33c40fa95a05f63764cc6e8ef9d9449fd75da415eca42543b80f52 53.62MB / 53.62MB                                                                                                                                                 4.8s
 => => sha256:40ba585f0e25c096a08c30ab2f70ef3820b8ea5a4bdd16da0edbfc0a6952fa57 1.20kB / 1.20kB                                                                                                                                                   0.0s
 => => extracting sha256:3f28ea9d8c33c40fa95a05f63764cc6e8ef9d9449fd75da415eca42543b80f52                                                                                                                                                        1.6s
 => exporting to image                                                                                                                                                                                                                           0.0s
 => => exporting layers                                                                                                                                                                                                                          0.0s
 => => writing image sha256:f5f920b43d06c51352da33d13114986564abdfb5ce7e9583d97a414b3e17654a                                                                                                                                                     0.0s
 => => naming to docker.io/library/ubuntu

Screenshot of Docker Desktop container image output

我很好奇想试试这个,因为一开始我不理解 Docker 文档中的措辞,但后来明白了。