I am trying to build the two services with the same image, but two different Dockerfile. However Docker will always use only one Dockerfile for both, even though two have been defined:
version: '3.4'
services:
serviceA:
image: myimage
build:
dockerfile: ./Dockerfile
context: ${project.basedir}/${project.artifactId}-docker/target
depends_on:
- serviceB
serviceB:
image: myimage
build:
dockerfile: ./Dockerfile-cloud
context: ${project.basedir}/${project.artifactId}-docker/target
Even though I also say dependsOn, running
docker-compose up -f docker-compose.yml
it only used the Dockerfile-cloud for both.
2
Answers
I guess your problem is that you tag your image as
myimage
(using latest by default). So docker will build a first version ofmyimage
withDockerfile
, then it’ll build another version ofmyimage
withDockerfile-cloud
, and in the end it will only use the latest version. This is why it will use Dockerfile-cloud for both. To fix it removeimage: myimage
or do something like:Since you’re building the two containers’ images from different Dockerfiles, they can’t really be the same image; they’ll have different content and metadata.
Compose is capable of assigning unique names for the various things it generates. Unless you need to do something like
docker-compose push
built images to a registry, you can generally just omit theimage:
line. The two containers will use separate images built from their own Dockerfiles, and the Compose-assigned names will avoid the ambiguity you’re running into here.