Docker container returns 400, request hostname is invalid

I have a project with different docker containers build using .net 6. I am trying to http get to a container from another container, but it returns an error code 400 with invalid hostname.


2024-04-06 13:31:26 info: Ecommerce.Cart.API.Controllers.CartController[0]
2024-04-06 13:31:26       Response Content: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
2024-04-06 13:31:26       <HTML><HEAD><TITLE>Bad Request</TITLE>
2024-04-06 13:31:26       <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></ HEAD >
2024-04-06 13:31:26       <BODY><h2>Bad Request - Invalid Hostname</h2>
2024-04-06 13:31:26       <hr><p>HTTP Error 400. The request hostname is invalid.</p>
2024-04-06 13:31:26       </BODY></HTML>

here is my docker compose file

version: '3.8'
networks:
  ecommerce.backend:
    name: ecommerce.backend

services:
  mongo:
    container_name: mongodb
    image: mongo
    restart: unless-stopped
    environment:
      MONGO_INITDB_ROOT_PASSWORD: '12345'
      MONGO_INITDB_ROOT_USERNAME: mongoadmin
    ports:
      - '27017:27017'
    networks:
      - ecommerce.backend
    volumes:
      - type: volume
        source: ecommerce_data
        target: /data/db

  rabbitmq:
    restart: always
    container_name: rabbitmq
    image: rabbitmq:3-management-alpine
    networks:
      - ecommerce.backend
    ports:
      - 5672:5672
      - 15672:15672

  postgresdb:
    container_name: postgresdb
    image: postgres
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: '12345'
    ports:
      - 5432:5432
    networks:
      - ecommerce.backend
    volumes:
      - "pg_data:/var/lib/postgresql/data"

  catalogservice:
    container_name: catalogservice-v1
    image: 'ecommerce/catalogservice:v1'
    networks:
      - ecommerce.backend
    build:
      context: .
      dockerfile: src/Services/Ecommerce.Catalog.API/Dockerfile
    ports:
      - '5002:80'
      - '5003:443'
    environment:
      'MongoDbSettings:mongoHost': mongo
      'MongoDbSettings:mongoPassword': '12345'
      'MongoDbSettings:mongoPort': '27017'
      'ASPNETCORE_ENVIRONMENT': 'Development'
      'CouponService:Url': 'http://discountservice:8081/api/discounts'
    depends_on:
      - mongo
      - discountservice

  identityservice:
    container_name: identityservice-v1
    image: 'ecommerce/identityservice:v1'
    build:
      context: .
      dockerfile: src/Services/Ecommerce.Identity.API/Dockerfile
    ports:
      - '5000:80'
      - '5001:443'
    environment:
      'MongoDbSettings:mongoHost': mongo
      'MongoDbSettings:mongoPassword': '12345'
      'ASPNETCORE_ENVIRONMENT': 'Development'
      'MongoDbSettings:mongoPort': '27017'
    networks:
      - ecommerce.backend
    depends_on:
      - mongo

  accountservice:
    container_name: accountservice-v1
    image: 'ecommerce/accountservice:v1'
    build:
      context: .
      dockerfile: src/Services/Ecommerce.Account.API/Dockerfile
    ports:
      - '5004:80'
      - '5005:443'
    environment:
      'MongoDbSettings:mongoHost': mongo
      'MongoDbSettings:mongoPassword': '12345'
      'MongoDbSettings:mongoPort': '27017'
      'ASPNETCORE_ENVIRONMENT': 'Development'
      'RabbitMqSettings:password': 'guest'
      'RabbitMqSettings:username': 'guest'
      'RabbitMqSettings:hostname': 'rabbitmq'
      'RabbitMqSettings:port': '5672'
    networks:
      - ecommerce.backend
    depends_on:
      - mongo
      - cartservice
      - rabbitmq

  cartservice:
    container_name: cartservice-v1
    image: 'ecommerce/cartservice:v1'
    networks:
      - ecommerce.backend
    build:
      context: .
      dockerfile: src/Services/Ecommerce.Cart.API/Dockerfile
    ports:
      - '5008:80'
      - '5009:443'
    environment:
      'PostgresDbSettings:postgresHost': 'postgresdb'
      'PostgresDbSettings:postgresPassword': '12345'
      'PostgresDbSettings:postgresUser': 'postgres'
      'PostgresDbSettings:postgresPort': '5432'
      'ASPNETCORE_ENVIRONMENT': 'Development'
      'RabbitMqSettings:password': 'guest'
      'RabbitMqSettings:username': 'guest'
      'RabbitMqSettings:hostname': 'rabbitmq'
      'RabbitMqSettings:port': '5672'
      'IdentityUrl': 'http://identityservice:80/api/Identity/login'
      'CatalogUrl': 'http://172.18.0.8/api/Account/users'
    depends_on:
      - postgresdb
      - rabbitmq
      - orderingservice
      - identityservice
      - catalogservice

  orderingservice:
    container_name: orderingservice-v1
    image: 'ecommerce/orderingservice:v1'
    build:
      context: .
      dockerfile: src/Services/Ordering/Ecommerce.Orders.API/Dockerfile
    ports:
      - '5006:80'
      - '5007:443'
    environment:
      'PostgresDbSettings:postgresHost': 'postgresdb'
      'PostgresDbSettings:postgresPassword': '12345'
      'PostgresDbSettings:postgresUser': 'postgres'
      'PostgresDbSettings:postgresPort': '5432'
      'ASPNETCORE_ENVIRONMENT': 'Development'
    networks:
      - ecommerce.backend
    depends_on:
      - postgresdb

  discountservice:
    container_name: discountservice-v1
    image: projectsofjoel/ecommerce-discountservice:v1
    #build: .
    environment:
      'MONGODB_USERNAME': 'mongoadmin'
      'MONGODB_PASSWORD': '12345'
      'MONGODB_PORTNUMBER': '27017'
      'MONGODB_HOST': mongo
      'MONGO_DB_NAME': 'ecommerce'
    ports:
      - "8081:8081"
    networks:
      - ecommerce.backend
    depends_on: 
      - mongo

  #ecommercefrontend:
    #container_name: frontendservice-v1
    #image: projectsofjoel/ecommerce-frontend:v1
    #build: .
    #environment:
      #'REACT_APP_IDENTITY_LOGIN_URL': 'http://localhost:5000/api/identity/login'
      #'REACT_APP_IDENTITY_REGISTRATION_URL' : 'http://localhost:5000/api/identity/register'
      #'REACT_APP_CATALOG_ITEMS_URL': 'http://localhost:5002/api/CatalogItems/items'
      #'REACT_APP_ACCOUNT_URL': 'http://localhost:5004/api/Account/users'
      #'REACT_APP_CART_URL': 'http://localhost:5008/api/v1/cart/'
    #ports:
      #- "3000:3000"
    #networks:
      #- ecommerce.backend
    #depends_on: 
      #- mongo

volumes:
  ecommerce_data:
  pg_data:

How do i solve this problem ?

From which container to which container and how? It’s good that you shared the compose file but without pointing out which part we should focus on, where you did what failed, we would need to spend much more time on your issue. For example we have no idea what hostname you used that was invalid. For now, I don’t see how is it a Docker issue. If you have an application that listens on an HTTP port and you get an HTTP error, that seems to be an application issue.

trying to make a http get call from cartservice to catalogservice. for some reason a http post call from cartservice to identityservice works fine. I believe it is a docker issue because the same code works outside a docker environment while debugging locally.

Since you probably don’t have to refer to different domain names when not running the app in containers, the deployment is different, but it could be just multiple servers. While you made this container-based deployment, you could have made a mistake in a parameter which has nothing to do with Docker.

I still don’t know how you send the http request and what hostname you use. I see the variables with the urls but I don’t know which one gives you the error message. Please describe the issue to someone who knows nothing about your app.

What I can see is that you use a container IP address in the CatalogUrl variable. If the application expects a hostname, that is indeed not a hostname but an IP address.

How do those services talk to each other? Internally directly? It looks like a application error, so maybe one service expects a FQDN instead of just a Docker service name in http host header?

I hope you are not deploying this on a VM directly on the Internet, it’s bad practice to open all those ports, better use Docker network.

Only open ports for frontend or even use a reverse proxy.