Skip to content

Prisma MongoDB Docker Container Fails to Initialize Replica Set due to Missing Network Configuration #7764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mahmoud-bebars opened this issue Apr 2, 2025 · 0 comments

Comments

@mahmoud-bebars
Copy link

Description:

While using Docker containers for development, I encountered an issue where Prisma's recommended MongoDB Docker Compose configuration fails to start the replica set.

Error Log:

When running the provided docker-compose.yml, the following error occurs:

mongodb-prisma-transactions-mongodb_container-1  | Waiting for initialization...
mongodb-prisma-transactions-mongodb_container-1  | Initializing replica set...
mongodb-prisma-transactions-mongodb_container-1  | Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: 
SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused
The container then exits with code 1.

The container then exits with code 1.

Cause:

The issue occurs because the provided docker-compose.yml file lacks a networks definition. Since Docker does not automatically assign a default network in this scenario, the MongoDB container fails to establish a connection, causing the replica set initialization to fail.

Fix:

To resolve this, I added a networks section to the mongodb_container service and defined a named network.

Here is the corrected docker-compose.yml:

version: "3.7"
name: mongodb-prisma-transactions
services:
  mongodb_container:
    # This image automatically creates a replica set required for transactions
    image: prismagraphql/mongo-single-replica:4.4.3-bionic
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: prisma
      INIT_WAIT_SEC: 3
    ports:
      - 27017:27017
    networks:
      - my-network

# Define networks
networks:
  my-network:

With this fix, the MongoDB container starts successfully, and the replica set initializes without issues.

Would it be possible to update the official Prisma documentation or Docker Compose template to include this fix?

Thanks! 🚀

Additional Reference:

According to Docker’s official networking documentation, services within Docker Compose need to be explicitly networked when running multi-container applications.
Since MongoDB’s replica sets require proper network communication, missing this configuration can cause connection failures. This is also mentioned in MongoDB’s deployment guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant