Github codespaces; setting up Mongodb

Hi everyone.
I’m pretty new to Docker, and would love your input on the following issue. My objective is to convert a rather simple Node.js project to run in Github codespaces. Our setup currently uses a local mongodb server, running with homebrew on developers machines.

To convert this I used the “Node.js & Mongo DB”, and it resulted in a docker-compose.yml file that looks like this:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        # Update 'VARIANT' to pick an LTS version of Node.js: 16, 14, 12.
        # Append -bullseye or -buster to pin to an OS version.
        # Use -bullseye variants on local arm64/Apple Silicon.
        VARIANT: "16"
    volumes:
      - ..:/workspace:cached
    init: true

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
    network_mode: service:db
    # Uncomment the next line to use a non-root user for all processes.
    # user: node

    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. 
    # (Adding the "ports" property to this file will not forward from a Codespace.)

  db:
    image: mongo:latest
    restart: unless-stopped
    volumes:
      - mongodb-data:/data/db
    # Uncomment to change startup options
    # environment:
    #  MONGO_INITDB_ROOT_USERNAME: root
    #  MONGO_INITDB_ROOT_PASSWORD: example
    #  MONGO_INITDB_DATABASE: your-database-here

    # Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
    # (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
  mongodb-data: null

There are no erros when I build the codespace, but I’m unsure about:

  • When/how would you recommend to seed data in db (we currently do this with a simple CLI method, but ideally I think it should probably only be done when the codespace builts?)
  • How do I control the version of MongoDB
  • When we setup MongoDB locally, we update the /usr/local/etc/mongod.conf file, so we have replication. We do this by adding:
replication:
  replSetName: "rs0"

When/how would you do that?

  • The first time we start MongoDb locally, we have to connect with mongo adn then run rs.initiate(). When/how/where do I specify such a configuration?
  • How can I securly connect with MongoDB Compass. I guess I need a secret - do you think that would be ok to have in plain text in the docker-compose.yml, or could it somehow “read” secrets from Github secrets?
  • Locally we currently use .env files. Could I write a script that created a .env file by reading from the environment in the codespace - or how would you go about that challenge?

I guess most of the questions are actually related to the lifecycle, i.e most of the setup only needs to happen when the codespace is built. I know I ask for a lot of advice, but I promise that if you will help me out I will produce a good description/video/post about how we got it up and running in the end :pray: