skip to Main Content

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


  1. I guess your problem is that you tag your image as myimage (using latest by default). So docker will build a first version of myimage with Dockerfile, then it’ll build another version of myimage with Dockerfile-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 remove image: myimage or do something like:

    serviceA: 
      image: myimage:serviceA
    ...
    serviceB: 
      image: myimage:serviceB
    
    Login or Signup to reply.
  2. 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 the image: 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.

    version: '3.8'
    services:
      serviceA:
        # no image:
        # short form of build: with default dockerfile: and no args:
        build: ${project.basedir}/${project.artifactId}-docker/target
        depends_on:
          - serviceB 
    
      serviceB:
        # no image:
        build:
          context: ${project.basedir}/${project.artifactId}-docker/target
          dockerfile: ./Dockerfile-cloud
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search