Even though Docker caches layers and the second build is fast, it's not necessary for Compose to build the same thing twice. As far as I can tell, if two services in the yaml have exactly the same string, you can be sure they will be exactly the same build:
21:02 ~/docker/test $ cat fig.yml
svc1:
build: oog
svc2:
build: oog
21:02 ~/docker/test $ fig build
Building svc2...
---> e72ac664f4f0
Successfully built e72ac664f4f0
Building svc1...
---> e72ac664f4f0
Successfully built e72ac664f4f0
Naturally, the image built is the same for both services.
There's also the question of the race condition if someone edits the Dockerfile while fig is already building. This is a bit contrived, but does work:
$ cat fig.yml
svc1:
build: oog
svc2:
build: oog
$ diff oog/Dockerfile[12]
3c3
< && echo hi
---
> && echo bye
$ ( cd oog;ln -sf Dockerfile{1,} ); fig build --no-cache & ( cd oog;sleep 2;ln -sf Dockerfile{2,} ); fg
[1] 1374
Building svc2...
---> e72ac664f4f0
Step 1 : RUN sleep 5 && echo hi
---> Running in dd0c813779ec
fig build --no-cache
hi
---> 9f1aed6d839c
Removing intermediate container dd0c813779ec
Successfully built 9f1aed6d839c
Building svc1...
---> e72ac664f4f0
Step 1 : RUN sleep 5 && echo bye
---> Running in 3c7d54f6145a
bye
---> 02ece1cf8f17
Removing intermediate container 3c7d54f6145a
Successfully built 02ece1cf8f17
Contrived or not, if it didn't attempt to run the same build twice, it wouldn't have divergent images.
Even though Docker caches layers and the second build is fast, it's not necessary for Compose to build the same thing twice. As far as I can tell, if two services in the yaml have exactly the same string, you can be sure they will be exactly the same build:
Naturally, the image built is the same for both services.
There's also the question of the race condition if someone edits the Dockerfile while fig is already building. This is a bit contrived, but does work:
Contrived or not, if it didn't attempt to run the same build twice, it wouldn't have divergent images.