Skip to content

Step Inside

Our MongoDB container is running, but we have not actually used it yet.

Before we move on to seeding, we want to step inside the container, connect with mongosh, and create a first real document.

A running container is nice, but we want proof that MongoDB is actually working.

In this step, we will:

  • use docker exec to run a command inside the container
  • connect to MongoDB with mongosh
  • create a database named portfolio
  • create a projects collection by inserting a first document
  • confirm that the document exists

That gives us a clean little win before we move into more automated setup.

The docker exec command lets us run a command inside a container that is already running.

That makes it incredibly handy for things like:

  • opening a shell inside a container
  • running database tools
  • checking files and folders
  • debugging what is happening inside a service

In our case, we are using it to launch the MongoDB shell from inside the MongoDB container.

Terminal window
docker exec -it db_service mongosh
  • docker exec runs a command inside an already running container
  • -it keeps the session interactive so we can type commands
  • db_service is the name of the running MongoDB container
  • mongosh launches the MongoDB shell inside that container

If all goes well, we should end up at a MongoDB prompt.

Why Not Just Connect from the Host?

We did not publish port 27017 to the host machine, so this database is not meant to be accessed directly from our laptop tools yet.

For now, stepping inside the container is the simplest and clearest approach.

Inside mongosh, switch to a database named portfolio:

use portfolio

MongoDB creates databases lazily, which means the database will not really exist until it contains data.

So right now, we have selected the database name, but we have not actually stored anything yet.

Creating the First Collection and Document

Section titled “Creating the First Collection and Document”

Now let’s insert one document into a projects collection:

db.projects.insertOne({
title: "Professor Solo Portfolio",
slug: "professor-solo-portfolio",
category: "web-development",
featured: true,
});

A few neat things happen here:

  • if the projects collection does not exist yet, MongoDB creates it
  • if the portfolio database does not exist yet, MongoDB creates it
  • the inserted object becomes the first document in that collection

Very chill. Very MongoDB.

Now let’s check whether the document is there:

db.projects.find();

We should see the document we just inserted.

We can also check which collections exist in the current database:

show collections

And if we want to confirm our current database:

db;

With only a few commands, we confirmed that:

  • the container is running
  • MongoDB accepts connections
  • the portfolio database can be used
  • the projects collection now exists
  • data can be written and queried successfully

That is a proper first checkpoint.

Technical schematic showing a 'Laptop' terminal opening a secure 'docker exec' tunnel into the 'db_service' container to run the 'mongosh' process internally.

Figure 1: A technical schematic showing a ‘Laptop’ terminal opening a secure ‘docker exec’ tunnel into the ‘db_service’ container to run the ‘mongosh’ process internally.

Still Ephemeral

We have not attached a named volume yet.

That means this document currently lives only inside the container’s writable layer. If the container is removed, the data goes with it.


Docker Exec Reference