Container on swarm not able to connect to the internet (when ports published)

Hi,
I deploy services through docker stack deploy in a yaml file where each service is on an overlay network, as below:

services:
  a:
    image: org/a:latest
    networks:
      - network_overlay
  b:
    image: org/b:latest
    ports:
      - "8000:8000"
   networks:
      - network_overlay

networks:
  network_overlay:
      driver: overlay
      attachable: true

When I do curl on a it works, on b I get: curl: (6) Could not resolve host:

As seen above the difference between a and b is only some ports published.
Is there a way where a service has a published port but can also access the internet?

Since you quoted compose file has an indentation error, I’m surprised it works at all. Maybe Swarm ignores it somehow. Try to fix the indentaion of the network keyword .

When copied the indentation got corrupt, obviously original yaml file is not.

What you experience is not caused by published ports. There must be an underlying problem that causes this behavior.

Is this a multi node setup, and are both containers running on different nodes? Does it happen if both run on the same node? It might be a problem with the overlay network.

It is a multi node setup, however:

  • both services are deployed to the same node, one works the other doesn’t
  • in the above example even on the same service b, if I remove port statement I can do curl
  • I tried:
    docker run --rm --network network_overlay nginx:alpine ping google.com,
    to rule out network issue.

The last point seems not consistent, earlier you write the expose ports breaks Internet access. Here no port is used.

And you don’t write if the test was successful or a failure. Does only service b have the problem?

The teat is successful meaning that i successfully can curl so I was able to rule out network isn’t the issue.
When ports are not exposed on service b it is able to access the internet. The only difference is expose or not.