Hi @Erkin_Qarayev and welcome in the MongoDB Community
!
From what I’m guessing here, I think you are trying to use Docker with Compose or maybe K8S. I’m not too sure but anyway, I think you are getting this error because when you execute the command
mongo --eval 'rs.initiate(); rs.status()'
Your mongod is - indeed - not in Primary or Secondary as it’s was just started 0.1 ms before this command, so it’s most probably in STARTUP or STARTUP2 state.
All that to say: your mongod needs a few seconds to start.
Personally, I’m using this alias to start a temporary single node RS with Docker:
alias mdb='docker run --rm -d -p 27017:27017 -h $(hostname) --name mongo mongo:5.0.2 --replSet=test && sleep 4 && docker exec mongo mongo --eval "rs.initiate();"'
And as you can see, there is a tactical sleep 4 in the middle of my command line to make sure that my mongod had enough time to start and get ready to access the first command to initialize the RS.
I hope this help.
Cheers,
Maxime.