Dino Geek, try to help you

How to use MongoDB with Docker?


1. Firstly, pull the initial Docker image of MongoDB from the docker hub by running the below command in your terminal:

```
docker pull mongo
```
1. After downloading it, create a new container instance by running this command:

```
docker run -d -p 27017-27019:27017-27019 —name mongodb mongo
```
Here ‘-d’ detach run the MongoDB instance in the background, ‘-p’ publish mongo’s ports inside the docker machine to the host, ‘—name mongodb’ name the instance as “mongodb”

1. You can connect to this MongoDB instance using your local MongoDB client through localhost and the default port (27017) or any Docker Machine IP.

1. If you want to run mongo shell inside the docker container, you can type:

```
docker exec -it mongodb bash
```

1. You’re now in the docker container’s bash. To connect to the mongo instance, just type:

```
mongo
```

Now you are ready to perform operations on MongoDB such as creating, retrieving, updating, and deleting data etc.

1. If you want to add a database user, while inside the mongo shell in your Docker container, use commands similar to this:

```
use
db.createUser( { user: “youruser”, pwd: “yourpassword”, roles: [ { role: “dbOwner”, db: “test” } ] }
)
```
Replace “”, “youruser” and “yourpassword” with your desired database name, user name, and password.

1. Once finished, you can stop the Docker container by running:

```
docker stop mongodb
```

Additionally, it’s good practice to use an official Docker image for MongoDB in production-like settings and handle data persistence separately.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use