Cannot initialize a mongodb replica set on Docker using “docker-compose.yaml” file

I already tried to find similar questions, but those do not seem to resolve my issue. My issue is very similar, but my configuration is already good as per this related issues:

Cannot initialize a mongodb replica set on Docker

MongoDB replica set initiate() failing on Docker

https://dev.to/mattdark/deploy-a-mongodb-cluster-with-docker-compose-4ieo


I want to set up locally a MongoDB cluster using Docker.
I have the following docker-compose.yaml file:

version: '3.8'

services:
    mongo_replica_1:
        container_name: mongo_replica_1
        hostname: mongo_replica_1
        image: mongo:7.0.4
        ports:
            - 27017:27017
        restart: always
        entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_1', '--replSet', 'dbrs', '--dbpath', '/data/db']
        volumes:
            - ./.volumes/mongo/replica1:/data/db
            - ./.volumes/mongo/replica1/configdb:/data/configdb
        networks:
            - dashboard_network
    mongo_replica_2:
        container_name: mongo_replica_2
        hostname: mongo_replica_2
        image: mongo:7.0.4
        ports:
            - 27018:27017
        restart: always
        entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_2', '--replSet', 'dbrs', '--dbpath', '/data/db']
        volumes:
            - ./.volumes/mongo/replica2:/data/db
            - ./.volumes/mongo/replica2/configdb:/data/configdb
        networks:
            - dashboard_network
    mongo_replica_3:
        container_name: mongo_replica_3
        hostname: mongo_replica_3
        image: mongo:7.0.4
        ports:
            - 27019:27017
        restart: always
        entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_3', '--replSet', 'dbrs', '--dbpath', '/data/db']
        volumes:
            - ./.volumes/mongo/replica3:/data/db
            - ./.volumes/mongo/replica3/configdb:/data/configdb
        networks:
            - dashboard_network
    mongo_launcher:
        container_name: mongo_launcher
        image: mongo:7.0.4
        restart: on-failure
        depends_on:
            - mongo_replica_1
            - mongo_replica_2
            - mongo_replica_3
        networks:
            - dashboard_network
        volumes:
            - ./scripts/mongo-setup.sh:/scripts/mongo-setup.sh
        entrypoint: ['sh', '/scripts/mongo-setup.sh']

networks:
    dashboard_network:
        driver: bridge

This is my mongo-setup.sh file:

#!/bin/bash

MONGODB_REPLICA_1=mongo_replica_1
MONGODB_REPLICA_2=mongo_replica_2
MONGODB_REPLICA_3=mongo_replica_3

echo "************ [ Waiting for startup ] **************" ${MONGODB_REPLICA_1}

until mongosh --host ${MONGODB_REPLICA_1}:27017 --eval "printjson(db.serverStatus())"; do
  printf '.'
  sleep 1
done

echo "************ [ Startup completed ] **************" ${MONGODB_REPLICA_1}

mongosh --host ${MONGODB_REPLICA_1}:27017 <<EOF
var configuration = {
    "_id": "dbrs",
    "protocolVersion": 1,
    "version": 1,
    "members": [
        {
            "_id": 1,
            "host": "${MONGODB_REPLICA_1}:27017",
            "priority": 3
        },
        {
            "_id": 2,
            "host": "${MONGODB_REPLICA_2}:27018",
            "priority": 2
        },
        {
            "_id": 3,
            "host": "${MONGODB_REPLICA_3}:27019",
            "priority": 1
        }
    ],
    "settings": {
        "chainingAllowed": true
    }
};
rs.initiate(configuration);
rs.secondaryOk();
db.getMongo().setReadPref('nearest');
EOF 

Then I try to connect to the DB with string mongodb://localhost:27017 but it fails: connect ECONNREFUSED 127.0.0.1:27017, connect ECONNREFUSED ::1:27017

So I tried to inspect my mongo_launcher container:

MongoServerError: replSetInitiate quorum check failed because not all proposed set members responded affirmatively: mongo_replica_2:27018 failed with Error connecting to mongo_replica_2:27018 (172.24.0.2:27018) :: caused by :: onInvoke :: caused by :: Connection refused, mongo_replica_3:27019 failed with Error connecting to mongo_replica_3:27019 (172.24.0.3:27019) :: caused by :: onInvoke :: caused by :: Connection refused
test> DeprecationWarning: .setSecondaryOk() is deprecated. Use .setReadPref("primaryPreferred") instead
Setting read preference from "primary" to "primaryPreferred"

And inspected one of the replica set container:

{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật