Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Docker

Docker MongoDB Container

Updated on Aug 15, 2024

Are you looking to set up a MongoDB container using Docker on your MacOS machine? Look no further! In this article, we'll walk you through the process of creating and configuring a Docker MongoDB container, including setting environment variables, starting the container, and testing the database.

Step 1: Create an Environment File

The first step is to create an environment file that will store our database password.

.env

DB_PASSWORD=Password1234

This file will be used to set the MONGO_INITDB_ROOT_PASSWORD environment variable in our Docker container.

Step 2: Create a Docker Compose File

Next, we'll create a Docker compose file that defines our MongoDB container.

mongo-docker-compose.yml

version: '3.9'

services:
  mongo-db:
    # Apple M1 Chip
    # platform: linux/amd64
    image: mongo:4.4.18
    container_name: mongo-db
    restart: always
    env_file:
      - .env
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: $DB_PASSWORD
    ports:
      - 27019:27017
    volumes:     
      - mongo_datadir:/data/db
    networks:
      - backend-network

networks:
  backend-network:
    driver: bridge

volumes:
  mongo_datadir:

This file defines a single service, mongo-db, which uses the official MongoDB 4.4.18 image and maps port 27019 on your local machine to port 27017 in the container.

Step 3: Start the Container

Now that we have our environment file and Docker compose file set up, it's time to start the container! Run the following command:

Now we can start the container.

docker-compose -f mongo-docker-compose.yml up -d

This will start the container in detached mode, meaning it will run in the background.

Step4: Test MongoDB Database Container

Once the container is running, we can test our database by creating a collection & some documents. Run the following commands:

Let us create a sample javascript file to create -

loadMovies.js

db = connect( 'mongodb://root:Password1234@127.0.0.1:27019/films?authSource=admin' );

db.movies.insertMany( [
   {
      title: 'Titanic',
      year: 1997,
      genres: [ 'Drama', 'Romance' ]
   },
   {
      title: 'Spirited Away',
      year: 2001,
      genres: [ 'Animation', 'Adventure', 'Family' ]
   },
   {
      title: 'Casablanca',
      genres: [ 'Drama', 'Romance', 'War' ]
   }
] )

Now we will create a database and some tables. 

Note: you should have MongoDB Shell Client installed locally.

db_password=Password1234

mongosh --host 127.0.0.1 --port 27019 --username root --password ${db_password} --authenticationDatabase admin --file loadMovies.js

Test the database objects-

readMovies.js

db = connect( 'mongodb://root:Password1234@127.0.0.1:27019/films?authSource=admin' );

printjson( db.movies.find( {} ) );
mongosh --host 127.0.0.1 --port 27019 --username root --password ${db_password} --authenticationDatabase admin --file readMovies.js

Step 5: Stop the Container

Finally, we can stop the container by running the following command:

docker-compose -f mongo-docker-compose.yml down

This will stop the container and remove it from memory.

That's it! We've successfully created and configured a Docker MongoDB container.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles