Domain name for Services in Docker Swarm

Hi, I have deployed a swarm in 1 node (my manager node), and inside my node, I have a Compose with 2 stacks, 1 for every web I have.

Once I deploy them, I can access to both websites with http://localhost:81 and …:82, but I need to access them via domain name, so I can expose them externally.

My answer is, how can I access these webs with a domain name? For example http://example

I tried aliases, networking and every other thing, thanks.

version: "3.9"
services:

  web1:
    image: test/web1:latest
    hostname: web1
    ports:
      - 82:3000
    deploy:
      mode: replicated
      replicas: 1
      endpoint_mode: vip
  
  web2:
    image: test/web2:latest
    hostname: web2
    ports:
      - 81:3000
    deploy:
      mode: replicated
      replicas: 1
      endpoint_mode: vip

I deploy that compose with → docker stack deploy -c .\docker-compose.yml web

If you tried every other thing, then there is nothing left. Can you be more specific?

The services names should work as domain names.

Hi, as I said I tried things like aliases, hostnames, networking with bridge and others.

The services names (in my case ‘web1’ and ‘web2’ didn’t work.
If I try to access to http://web1 from my host machine, in my browser appears ‘DNS_PROBE_FINISHED_NXDOMAIN’.
And if I enter http://localhost/web1/ → ‘ERR_CONNECTION_REFUSED’

What log or configuration can I show to be more specific?
Thanks!

Okay, the problem seems to be my /etc/hosts file, I added → 127.0.0.1 web
And now I can access my website with http://web:81

I still need to access without the port, but thats another thing, for now it’s solved!

The 'problem" is not the hosts file, but you can use it if you want. You expected the service names to be resolvable by the DNS server on your host. It is not. IT works only from inside a container attached to the same custom docker network that Docker Compose and Docker Swarm creates automatically.

IF you don’t want to use a port, you need the srvices to listen on the default ports. That is not a Docker thing, that is just how websites and web browsers work. port 80 for HTTP and port 443 for HTTPS. If you have multiple services listening on the same port, you need a reverse proxy. Traefik, nginx proxy, Caddy. Just to mention a few.

I have a tutorial using Nginx

But I’m using Traefik in production.

1 Like

Sounds like a good use case for Traefik, which can automatically do Configuration Discovery for Docker Swarm services. Check (sorry, undocumented) simple Traefik Swarm example.

1 Like