Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing docker swarm mode port only to localhost

I've created docker swarm with a website inside swarm, publishing port 8080 outside. I want to consume that port using Nginx running outside swarm on port 80, which will perform server name resolution and host static files.

Problem is, swarm automatically publishes port 8080 to internet using iptables, and I don't know if is it possible to allow only local nginx instance to use it? Because currently users can access site on both 80 and 8080 ports, and second one is broken (without images).

Tried playing with ufw, but it's not working. Also manually changing iptables would be a nightmare, as I would have to do it on every swarm node after every update. Any solutions?

EDIT: I can't use same network for swarm and nginx outside swarm, because overlay network is incompatible with normal, single-host containers. Theoretically I could put nginx to the swarm, but I prefer to keep it separate, on the same host that contains static files.

like image 206
Valar Avatar asked Oct 20 '16 10:10

Valar


2 Answers

No, right now you are not able to bind a published port to an IP (even not to 127.0.0.1) or an interface (like the loopback interface lo). But there are two issues dealing with this problem:

  • github.com - moby/moby - Assigning service published ports to IP
  • github.com - moby/moby - docker swarm mode: ports on 127.0.0.1 are exposed to 0.0.0.0

So you could subscribe to them and/or participate in the discussion.

Further reading:

  • How to bind the published port to specific eth[x] in docker swarm mode
like image 90
Murmel Avatar answered Nov 19 '22 13:11

Murmel


Yes, if the containers are in the same network you don't need to publish ports for containers to access each other.

In your case you can publish port 80 from the nginx container and not publish any ports from the website container. Nginx can still reach the website container on port 8080 as long as both containers are in the same Docker network.

like image 43
Elton Stoneman Avatar answered Nov 19 '22 13:11

Elton Stoneman