MongoDB replica set with one dockerized and two non-dockerized nodes

I’m beginner in the world of docker containers. I want to convert a standalone mongodb running in a docker container to a replica set and then add two members from outside of the container (they are seperate virtual machines actually). The docker host machine and the other two mongodb server are in the same network and can communicate with eachother.

To be able to initialize a replica set I created a new container with the --replSet flag. The container is using the default bridge network. From inside the container I could connect to the other two mongodb’s shell and also from those servers I can reach the container’s mongo shell using the docker host’s ip address and the exposed port. What I have problem with is when I initialize the replica set it picks up the container’s inside ip and the two external servers couldn’t receive the replica set config and stucks in the startup phase. From the mongo logs I see that they are trying to connect but it never happens (CONNPOOL [Replication] Connecting to container_ip:27017)

To solve the problem I created a new container with network_mode=host so the container now using the host’s ip address and mongodb listens on port 27017 but this way I get no route to host when connecting from the two remote servers to the container mongo shell. From the container I can stil reach the other two server. I tried to run mongodb with bind_ip_all too but nothing seems to work.

Is it possible to create a replica set like this at all or I’m missing something?