When you use
network_mode=host, your containers will use the underlying host's network namespace. Therefore, it is equivalent to running your application on an EC2 instance wrt networking.
If you do not specify the above parameter, your containers running in the swarm cluster will be connected to the underlying host's
docker_gwbridge network by default. I believe the issue could be related to the docker network setup in your swarm cluster.
Docker modifies the iptables on your nodes to provide network isolation. If you have configured docker daemon settings in
/etc/docker/daemon.json, make sure you have not set
iptables=false as this will break your container networking. For more info, refer this doc.
I found a stackoverflow issue that is similar to yours. Try to follow the steps provided and see if it helps in finding the root cause.
Lightsail container - connect to ElastiCacheasked 7 months ago
From docker container need to connect RDS - Using Session Managerasked a month ago
CanNot connect EC2 instance using internetasked a month ago
i cannot connect my ec2 by sshasked a year ago
Unable to connect to EC2 instance in private Subnetasked a year ago
EC2 Cannot connect to internetasked 3 months ago
Does WorkSpaces/EC2 Support Docker?Accepted Answerasked 2 years ago
I can't connect to my EC2 instance.asked 5 months ago
Unable to connect to EC2 instance via aws websiteAccepted Answerasked 3 months ago
docker container in EC2 cannot connect to elasticCacheasked 14 days ago