Docker swarm port mapping

To map a port through a service, run the below command -

root@master:~# docker service create -d -p 8080:80 nginx
9e0nbra7i2j7oii50j14g2cqe
root@master:~# docker service ls
ID             NAME               MODE         REPLICAS   IMAGE          PORTS
9e0nbra7i2j7   wonderful_carver   replicated   0/1        nginx:latest   *:8080->80/tcp
root@master:~#

This means that the Nginx service will be available through each of the IP addresses of the container and the port 8080 in the cluster. lets access it using curl command

root@master:~# curl 192.168.0.123:8080
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to nginx!</title>
    <style>
      html {
        color-scheme: light dark;
      }
      body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to nginx!</h1>
    <p>
      If you see this page, the nginx web server is successfully installed and
      working. Further configuration is required.
    </p>
    <p>
      For online documentation and support please refer to
      <a href="http://nginx.org/">nginx.org</a>.<br />
      Commercial support is available at
      <a href="http://nginx.com/">nginx.com</a>.
    </p>
    <p><em>Thank you for using nginx.</em></p>
  </body>
</html>

root@master:~# curl 192.168.0.124:8080
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to nginx!</title>
    <style>
      html {
        color-scheme: light dark;
      }
      body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to nginx!</h1>
    <p>
      If you see this page, the nginx web server is successfully installed and
      working. Further configuration is required.
    </p>
    <p>
      For online documentation and support please refer to
      <a href="http://nginx.org/">nginx.org</a>.<br />
      Commercial support is available at
      <a href="http://nginx.com/">nginx.com</a>.
    </p>
    <p><em>Thank you for using nginx.</em></p>
  </body>
</html>
root@master:~#

Here the 192.168.0.124 is the ip of the container on worker01. And the same service can also be accessed on the master node and the worker node worker01.

you can access the same using web browser.

docker-swarm-service-port-mapping