Installing MongoDB using docker compose with user and password

Author: Al-mamun Sarkar Date: 2020-10-14 13:11:09

In this lesson, I will show how to install MongoDB using Docker compose with the root user and password. To follow this tutorial docker and docker-compose has to be installed on your computer. 

Installing Docker

Installing Docker Compose

 

Create a docker-compose.yml file and paste the following codes.

version: "2.0"
services:
  mongodb_container:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db

volumes:
  mongodb_data_container:

Here username is root and password is also password. Change the username and password if you want.

 

Run the following command to download and run the docker container of MongoDB:

docker-compose up -d

Check the container:

docker ps

Check Volumes:

docker volume ls

 

Use mongo shell with username and password:

To run the following command mongo client has to be installed on your computer.

mongo -u root -p password

Another way:

mongo mongodb://root:password@127.0.0.1:27017

Change username, password, and IP address following your installation and server.