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

I have understood that if I have in a directory the following

Dockerfile

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

And the following compose file

docker-compose.yml

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

Docker should build an image from fedora and name it ubuntu. But instead it seems it just pulls the ubuntu image (if I comment out image: ubuntu it pulls the fedora image). Why is that?

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.


Solution 1:

I tested and it worked for me, but I did need to change the compose file contents to make the service a map, as yours is typed I got: "services.build must be a mapping".

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

But my Dockerfile was the same:

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

Build logs:

[+] 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

I was curious to try this because I didn't understand the wording from the Docker docs at first, but made sense after.