Is there any way to set multiple subdomain for a container

services:
  server:
    image: alpine:latest
    hostname: serverhost.com
    ports:
          - 8082:8082         
  client:
    image: alpine:latest

Hi the experts,
If My docker-compose spec like above, from inside client container, I’m only able to do ping to serverhost.com. Is there any way to ping to subdomain like abc.serverhost.com?

Hello

I would say it can works like below. Trick is to add the subdomain in your host file inside the container.

services:
  server:
   ... 
    extra_hosts:
      - "sub.server.com:172.20.0.1"

The other containers do not see that extra_hosts, I mean from inside other containers you cannot see these sub domain. I solved my problem by adding the alias to the container. docker network connect --alias sub.serverhost.com multi-host-network server. Thanks

Note: docker is not responsible to handle dns outside the container networks.

I assume you are setting the domains as hostname and alias to test something.

When it comes to container to container communication, you can use the service name, the container name, the host name or the alias to communicate with another container in the same network. This feature is only available in user defined networks, which docker compose deployments always create for the compose project.

In the real world a dns server would be responsible to resolve domain names to your docker host (or a reverse proxy or load balancer in front of it). Many people run a traefik container as reverse proxy and only expose port 80 and 443 for traefik, and let traefik forward the traffic for a sub domain and/or path to a specific container.