Docker Community Forums

Share and learn in the Docker community.

Mongo container with a replica set with only one node in docker-compose

Hello,
I want to create a Docker container with an instance of Mongo. In particular, I would like to create a replica set with only one node (since I’m interested in transactions and they are only available for replica sets).

Dockerfile

FROM mongo
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD ["--replSet", "rs0"]

docker-compose.yml

version: "3"
services:
  db:
    build:
      dockerfile: Dockerfile
      context: .
    ports:
      - "27017:27017"

If I use the Dockerfile alone everything is fine, while if I use docker-compose it does not work: in fact if I then log to the container I got prompted as rs0:OTHER> instead of rs0:PRIMARY>.

I consulted these links but the solutions proposed are not working: