How To Run Mongodb In Docker With Username and Password | Java Inspires





How To Run Mongodb In Docker With Username and Password | Java Inspires





In this post, we will see how to run mongodb in docker with username and password.

First we will run without username and password.

Command:
$ docker run --name local-mongo -p 27017:27017 -d mongo


Connect to DB using Robo3T


Stop and remove container
$ docker container ls
$ docker container stop <containerId>
$ docker container rm <containerId>



Run mongo with username and password.

Command:
$ docker run --name local-mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password -d mongo


Connect to DB using Robo3T


Thats it .... THANK YOU...




Power Shell Log:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\developer> docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mongo        latest    4253856b2570   2 weeks ago   701MB
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker run --name local-mongo -p 27017:27017 -d mongo
fe418cb3c38566b86b97127efabc5a3255b7bbc5562654311dcff8d157a90e2d
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker conatiner ls
docker: 'conatiner' is not a docker command.
See 'docker --help'
PS C:\Users\developer> docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                      NAMES
fe418cb3c385   mongo     "docker-entrypoint.s…"   18 seconds ago   Up 15 seconds   0.0.0.0:27017->27017/tcp   local-mongo
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                      NAMES
fe418cb3c385   mongo     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:27017->27017/tcp   local-mongo
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker container stop fe418cb3c385
fe418cb3c385
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker container rm fe418cb3c385
fe418cb3c385
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker run --name local-mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password -d mongo
471e60bf3ec7b294a859446d50fe6cee101e599d867be344f0e2ab67ca9dbbb2
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer> docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                      NAMES
471e60bf3ec7   mongo     "docker-entrypoint.s…"   14 seconds ago   Up 11 seconds   0.0.0.0:27017->27017/tcp   local-mongo
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>
PS C:\Users\developer>





1 Comments

Previous Post Next Post