Communication between 2 containers in different hosts

I am trying to make communication between 2 containers on different hosts
For that i created a swarm , with 2 nodes , one manager container the rest flask api , and the worker contains the mongodb image ,
i also create a overlay network for enabling the conatiner to communicate to each other ,
when i use docker compose for running the simplified program , there is no issues , but when i try to deploy the stack in the swarm , i can not access the database.
I dont know if the issues is concerning the host .
that is the my code and the error when trying to accessing the page
Any help will be welcome
thanks error1 error

I assume that the two nodes that you have setup has this ports open in the firewalls:

  • TCP port 2377 for cluster management communications
  • TCP and UDP port 7946 for communication among nodes
  • UDP port 4789 for overlay network traffic

Have you created the overlay network outside of the docker-compose or is it created when you do the swarm deploy?

As for the “not allowed”: You are doing a GET from the browser on an endpoint that is declared POST only (/new). Use Postman, curl or any other tool that can define the method.

i created the overlay network inside the compose file like you can see
that is my issues when i run the code in the same host , there is no issues and i get the result i wish ,
that means i can access the mongo databas and insert some data by posting them with the flask api . then i just query the database .
there is no issues with the curl.