# Contents

Developer playgrounds are isolated environments where developers can safely experiment, test code, and explore new technologies without impacting their main projects or production environments. They offer a controlled space for prototyping, learning, and debugging.

Creating a basic developer playground can be straightforward, but complexity increases with customization. Daytona simplifies this process.

Daytona’s Workspaces are isolated, allowing safe testing without impacting main projects or production environments. This isolation is crucial for debugging. Daytona streamlines environment management, enhancing productivity and team collaboration.

This guide will show you how to use Daytona to set up a MongoDB environment. You'll create a reproducible, isolated workspace, configure it with MongoDB, and customize it for your project. Ensure you have Docker, an IDE (like VS Code or JetBrains), and Daytona installed. You’ll have a functional MongoDB playground for development and testing by the end.

TL;DR
  • Developer playgrounds are isolated environments where developers can safely experiment, test code, and explore new technologies without impacting their main projects.

  • Developer playgrounds boost software development by providing a risk-free space to test, learn, and innovate without affecting main projects.

  • Daytona simplifies setting up developer playgrounds by providing reproducible, isolated environments with easy management and flexible configuration options.

  • Prerequisites to follow this guide: Docker, IDE(VS Code or JetBrains) and Daytona.

Overview of MongoDB

MongoDB is a popular NoSQL database that stores and retrieves large amounts of unstructured or semi-structured data. Unlike traditional relational databases (SQL databases), which store data in tables with fixed schemas (rows and columns), MongoDB stores data in a more flexible, JSON-like format called BSON (Binary JSON).

Overview of Daytona

Features of Daytona

  • Pre-configured Environments: You can create environments with all pre-installed dependencies, tools, and configurations so developers can start coding immediately without spending time configuring their setups.

  • Collaborative Workspaces: The platform enables team collaboration by allowing multiple developers to work in the same environment. This can be particularly useful for pair programming, code reviews, or troubleshooting.

  • Containerized Environments: Each development environment can be containerized, ensuring the setup is consistent, reproducible, and isolated from other environments. This helps avoid the common “works on my machine” problem.

  • Reverse Proxy Support: Daytona integrates a reverse prox,y allowing you to access a workspace on a public or restricted network.

Setup a Daytona configuration for MongoDB

Here, you will create a dev container using a devcontainer.json file, a Dockerfile, and a docker-compose.yml file. One of Daytona’s remarkable qualities is its ability to build a projected image according to the dev container standard.

  • Step 1: Create a new directory

    You will create a file directory with any name and move inside of it.

1mkdir daytona-mongodb-playground && cd daytona-mongodb-playground
  • Step 2: Create a .devcontainer directory

    This is where your devcontainer.json and dockerfiles will live

1mkdir .devcontainer && cd .devcontainer
  • Step 3: Create a devcontainer.json file

You are going to create a devcontainer.json file with the following code. This is the configuration file for the dev environment specifying settings and dependencies.

1{
2"name": "mongo Dev Container Playground",
3"dockerComposeFile": "docker-compose.yml",
4"service": "mongodb_playground",
5"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
6"forwardPorts": [27017],
7"customizations": {
8 "vscode": {
9 "extensions": [
10 "mongodb.mongodb-vscode"
11 ]
12 }
13}
14}

Let’s breakdown the devcontainer.json file:

  • name: Specifies the name of the development environment.

  • dockerComposeFile: points to the docker-compose.yml file we are going to use

  • service: Refers to the service name in the docker-compose.yml file that represents the MongoDB container or environment to be used.

  • workspaceFolder:Maps the local workspace folder (where the code is stored) to the /workspaces/ directory inside the container. The variable ${localWorkspaceFolderBasename} automatically resolves to the name of the local workspace folder.

  • forwardPorts: Exposes MongoDB’s default port (27017) so that it is accessible from the host system or other containers for interaction with the MongoDB service.

  • customizations: Allows customization of the development environment, specifically for VS Code.

  • vscode: Automatically installs the MongoDB extension for VS Code, enabling MongoDB-related features (e.g., interacting with MongoDB directly from VS Code).
    Declares a persistent volume for storing MongoDB data.

  • Step 4: Create a Dockerfile

    You will create a Dockerfile in the same directory.

1FROM mcr.microsoft.com/devcontainers/base:focal
2RUN sudo apt-get install -y gnupg wget curl
3RUN wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
4RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/
5mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
6RUN sudo apt-gUpdates the package list and installs MongoDB.et update && export
7DEBIAN_FRONTEND=noninteractive \
8&& sudo apt-get install -y mongodb-org

Let’s break this Dockerfile for you:

  • FROM mcr.microsoft.com/devcontainers/base:focal: Uses the Microsoft DevContainers base image based on Ubuntu 20.04 LTS (Focal Fossa). This provides a standard environment for development containers.

  • RUN sudo apt-get install -y gnupg wget curl: These utilities are necessary for managing GPG keys and downloading files. gnupg is used for key management, while wget and curl are used for downloading files.

  • RUN wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -: Ensures that the packages from the MongoDB repository can be verified and trusted. The -qO - option makes wget output the file to stdout, which is then piped to apt-key add for key addition.

1RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse"
  • sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list: Configures the system to use MongoDB’s official repository for version 4.4. This allows apt-get to retrieve MongoDB packages for installation.

  • Updates the package list and installs MongoDB.

1RUN sudo apt-get update && export DEBIAN_FRONTEND=noninteractive
2sudo apt-get install -y mongodb-org
  • Step 5 Create a docker-compose.yml file:
    You are going to create docker-compose.yml file in the same directory

1version: '3.8'
2services:
3 mongodb_playground:
4 build:
5 context: .
6 dockerfile: Dockerfile
7 volumes:
8 -../..:/workspaces:cached
9 network_mode: service:db
10 command: sleep infinity
11 db:
12 image: mongo:4.4
13 restart: unless-stopped
14 ports:
15 - "27017:27017"
16 volumes:
17 - mongodb-data:/data/db
18volumes:
19 mongodb-data:
20

Let’s breakdown the ``docker-compose.yml` file for you:

  • version: Ensures compatibility with Docker Compose features and syntax defined in version 3.8.

  • services: Defines the containers (services) that will be run as part of this Compose setup.

  • mongodb_playground: Defines a service named mongodb_playground.

  • context: Sets the build context to the current directory.

  • dockerfile: Specifies the Dockerfile to use for building this service.

  • volumes: …/…:/workspaces:cached: Mounts a directory from the host (two levels up to /workspaces in the container. The cached option optimizes performance by indicating that changes in the host are less likely to affect the container.

  • network_mode: service:db: Allows mongodb_playground to communicate with the db container using the same network. This is useful for sharing network settings between containers.

  • command: sleep infinity: Keeps the container running indefinitely, preventing it from stopping after its initial command finishes.

  • db: Defines a service named db.

  • image: mongo:4.4:Uses the official MongoDB image version 4.4.

  • restart: unless-stopped: Restarts the container unless it is explicitly stopped by the user. This helps ensure the MongoDB service remains available.

  • ports: Exposes MongoDB’s default port for external access. This makes MongoDB accessible from the host machine.

  • volumes:

    • mongodb-data:/data/db: Mounts a named volume mongodb-data to /data/db in the container.

  • volumes: This volume is used by the db service to store its database files.

Step 6 Initialize,Commmit and Create a GitHub repository:

You will initialize and commit the code in your current directory

1git init
2git add .
3git commit -m "inital commit"

After committing your code, you will push it to a remote repository of your choice.

1git remote add origin https://github.com/YOUR-GH/YOUR-REPO.git
2git branch -M main
3git push -u origin main

Creating the MongoDB playground in Daytona

Here, you will use Daytona to build the playground using GitHub as a Provider and open a workspace in VS Code. You should ensure daytona is installed on your machine before proceeding.

  • Step 1 Start the Daytona Server daemon :
    Execute the command provided below to start the daytona server daemon. when prompted to start the server in the current terminal session, click yes

1daytona server
  • Step 2: Setup your Git Provider :
    Daytona integrates with your preferred Git provider, streamlining your workflow by allowing direct access to repositories and simplifying workspace creation from existing projects.

Execute the command below to add your Git provider. GitHub is one of the most popular developer tools. Daytona also supports other Git providers, such as Bitbucket and Gitlab.

1daytona git-provider add

Select GitHub and provide your personal access token.

  • Step 3 Choose your preferred IDE

Run this command in the terminal to choose your IDE.

1daytona ide
  • Step 4 Create a Daytona Workspace

Substitute the USERNAME/REPO-NAME from the code below with the Github username and the repository where the devcontainer is stored.

1daytona create https://github.com/USERNAME/REPONAME

Ideally, but optional, you should add the --code flag at the end. This will automatically open your preferred editor after building the dev container.

1daytona create https://github.com/USERNAME/REPONAME --code

Interacting with the MongoDB playground using Mongo Shell.

Your Preferred IDE should have opened. You will be instructed to open a Remote SSH connection. You should see that your devcontainer.json, Dockerfile, and docker-compose.yml files have been downloaded. After creating the connection, navigate to your terminal by clicking Ctrl + Shift + `. In the terminal, execute the following code to start a Mongo shell and carry out operations.

  • Step 1 Create a MongoDB Database:

1use test
  • Step 2 Create MongoDB Collection:

1db.createCollection("collection")
  • Step 3: Create a document into test collection:

1db.collection.insertOne({ name: "John", age: 25, city: "New York" });
  • Step 4: Read (Query) a document from the collection:

1db.collection.findOne({ name: "John" })
  • Step 5: Update a document:

1db.collection.updateOne(
2{ name: "John" }, // Filter
3{ $set: { age: 26 } } // Update operation
4);
  • Step 6: Delete the Collection

1db.collection.deleteOne({ name: "John" });

Using the MongoDB playground extension (Optional)

Alternatively, you can use the official MongoDB extensions playground since you provided "mongodb.mongodb-vscode" as an extension in the devcontainer.json file.

  • Step 1 Create a connection:
    Click on the Connect to MongoDB in the MongoDB view, then enter the connection string: mongodb://localhost:27017.

  • Step 2 Connect to Playground:
    Connect to the MongoDB playground, which is a built-in interactive environment for running MongoDB queries. Navigate to the MongoDB extension and click on the Create MongoDB Playground.

  • Step 3 Run Queries :
    Inside the playground, you can write queries like you did in the MongoDB Shell. For example

1use("test");
2db.collection.insertOne({name:'Eliza',age:40})

Common Issues and Troubleshooting

Connection Refused on Port 3896: If you are experiencing this, check what service is running on that port and close it.

Cannot Start the Daytona Docker Registry: If you can't start the Daytona Docker registry, don’t panic, just re-run the daytona create command, and it should run as expected.

IDE doesn’t open after building Workspace: If your IDE doesn't start immediately after building the dev container, open your terminal and type the daytona list command, select your running workspace and you’re good to go.

Image Compatibilty Issues : There could be a few issues regarding compatibility for the base Ubuntu image. You can fix it by opening Dockerfile and changing FROM mcr.microsoft.com/devcontainers/base:focal to FROM ubuntu:focal.

Build Target : The build target may need to be set to docker manually. You can set it with daytona target set

Conclusion

In conclusion, following this guide, has successfully set up a MongoDB playground using Daytona. With a reproducible and isolated environment, you can now experiment, test, and develop, knowing your main projects remain unaffected. This setup streamlines your workflow and allows you to explore new ideas and technologies.

Tags::
  • daytona
  • devcontainer
  • mongoDB