Docker compose multiple dockerfile

I’m currently working on a maven project where I would like to have 2 docker container where one launch all the tests and the other compile de project if all the tests succeed.

The problem is that both containers launch the prod dockerfile.

So my question is why after pointing each Dockerfile in the docker compose they both start on the prod one

docker-compose.yml:

version: '3.8'
services:
  db:
    container_name: db
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
        MYSQL_DATABASE: cuisine
        MYSQL_ROOT_PASSWORD: example
    healthcheck:
          test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
          timeout: 20s
          retries: 10
    ports:
        - "3306:3306"
    volumes:
      - ./docker/mysql-dump/cuisine:/docker-entrypoint-initdb.d
      - mysql:/var/lib/mysql
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
  test:
    container_name: java-test
    image: spring-boot
    build:
      context: .
      dockerfile: docker/test/Dockerfile
    ports:
      - "8081:8081"
      - "5005:5005"
    depends_on:
      db:
        condition: service_healthy
    volumes:
       - ${APPLICATION_ROOT_FOLDER}:/usr/src/mymaven
       - ${MAVEN_SETTINGS_FOLDER}:/root/.m2
  java:
    container_name: java
    image: spring-boot
    build:
      context: .
      dockerfile: docker/prod/Dockerfile
    ports:
      - "8082:8082"
      - "5006:5006"
    depends_on:
      db:
        condition: service_healthy
    volumes:
       - ${APPLICATION_ROOT_FOLDER}:/usr/src/mymaven
       - ${MAVEN_SETTINGS_FOLDER}:/root/.m2
volumes:
  mysql:

>Solution :

When you have both build: and image: on your services, the built image is tagged with the image: name. You have the same image: name for both services, so the last one to be built ‘wins’ and is run for both services. Remove the image: name for both.

Leave a Reply