Indeed, since docker 1.12, docker swarm mode implements it's own service discovery capabilities.
On a single host setup (testing)
To look into it, and for example it's load balancing capabilities, you can do the following :
#Setup your docker engine as a docker swarm manager
docker swarm init
#Create an nginx service
docker service create --name nginx --publish 80:80 nginx
Now you can list services using docker service ls
, and see that you have an nginx service.
If you do a docker ps
, you'll see that your container is not exposing any ports directly to the machine, but if you try to inspect your service, the port is indeed exposed as a service port. So to access your container, you'll need to connect to the docker swarm manager's address, and your published port. Here since your machine is the manager, you'll need to access localhost:80
, or your $DOCKER_HOST:80
if using docker-machine or equivalent
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f9d93dbbce5 nginx:latest "nginx -g 'daemon off" About a minute ago Up About a minute 80/tcp, 443/tcp nginx.1.4zr3zacuw06ax9swuit4wbacd
> curl -X GET localhost:80
# Result showing nginx stuff
If you want to refer to the documentation, you can have a lot of information on the swarm key concept page and on the swarm mode routing mesh page
On a multi host setup
If you are running a multi host setup, like you would in a normal usage of swarm mode you would have at least two docker engines running in swarm mode : one as a worker, one as a manager. By default, the manager is also a worker and can host containers
When interacting with the swarm, you'll always talk directly to the docker swarm manager. You can then create an nginx service like above, and the service would be created on either the manager or the worker node. Then, to access your container via its port, you'll need to access the manager node's via its ip, which will forward the request to the container, be it on the worker or the manager's node. You can also scale it and see the load balancing happening, as it will query both container in a round robin fashion.
Internal service discovery
Since docker 1.12, there also is an internal service discovery feature that lets you access other services using its service dns.
To access this feature, you'll need to create an overlay network, and attach your service to it
docker network create --driver overlay mynetwork
docker service create --name nginx --network mynetwork nginx
docker service create --name testing --network mynetwork node sleep 10000 #node because it already has ping cmd
#locate your testing service's container, and ping the nginx host
docker exec -ti ping nginx
#See the magic happen
Once again, a lot of things are in the documentation, on the Docker Engine > Manage a swarm section. See Swarm mode overview