'Docker network alias not working as expected

I'm unable to get network alias working. I've tried everything, but haven't been able to get it working.

I've two services:

  1. legacy-service
  2. actualService - actualService calls legacyService using an API and the configuration we've set up makes it call legacy-service.dev/test/preprod/prod.com.

When I spin up the below docker-compose, both legacy-service and actualService spins up, but when actualService calls legacy-service, it's actually calling the one on our dev/test server and not the locally spun up legacy-service even though I have the alias.

How do I make my locally spun actualService talk to my locally spun legacy-service with that alias?

version: '3.7'
services:

  #legacyService component
  legacyService:
    environment:
      AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
      AWS_REGION: ap-southeast-2
      AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
      AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN}
      JAVA_OPTS: -Djava.awt.headless=true -Xms484M -Xmx968M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=768M -XX:CompressedClassSpaceSize=171M -XX:+UnlockDiagnosticVMOptions -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
    image: legacy-service
    networks:
      net:
        aliases:
          - legacy-service.xyz.com
    ports:
      - 9090:8080
      - 9000:8000
    
  production-build:
    build:
      context: ../
      dockerfile: build/Dockerfile
    image: component-api-production-build
    entrypoint: 'echo "overriding Dockerfile ENTRYPOINT, this will terminate this service, we only need the built image"'

  #actual-service
  development-build:
    depends_on:
      - production-build
      - legacyService
    image: component-api-dev-build
    build:
      context: ../
      dockerfile: local-dev/Dockerfile
    ports:
      - 8080:8080
      - 8000:8000
    environment:
      AWS_REGION: ${AWS_REGION}
      AWS_DYNAMODB_TABLE_PROPERTY: ${AWS_DYNAMODB_TABLE_PROPERTY}
      JAVA_OPTS:
        -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
    networks:
      - net

networks:
  net:


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source