There is a way to link /data/db
directory of the container to your localhost. But I can not find anything about configuration. How to link /etc/mongo.conf
to anything from my local file system. Or maybe some other approach is used. Please share your experience.
You can find one by entering the container and taking /etc/mongod. conf. orig or you can get it on MongoDB's GitHub repo: https://github.com/mongodb/mongo/blob/master/rpm/mongod.conf. By default, MongoDB will look for a configuration file in /etc/mongod.
Can MongoDB Run in a Docker Container? MongoDB can run in a container. The official image available on Docker Hub contains the community edition of MongoDB and is maintained by the Docker team. This image can be used in a development environment.
You can connect to MongoDB on localhost:27017 . Then use the following command to open the MongoDB shell. I have used mymongo as an arbitrary container name, though you can replace mymongo with test-mongo or any other container name of your choosing. The show dbs command will display all your existing databases.
I'm using the mongodb 3.4 official docker image. Since the mongod doesn't read a config file by default, this is how I start the mongod service:
docker run -d --name mongodb-test -p 37017:27017 \ -v /home/sa/data/mongod.conf:/etc/mongod.conf \ -v /home/sa/data/db:/data/db mongo --config /etc/mongod.conf
removing -d will show you the initialization of the container
Using a docker-compose.yml
:
version: '3' services: mongodb_server: container_name: mongodb_server image: mongo:3.4 env_file: './dev.env' command: - '--auth' - '-f' - '/etc/mongod.conf' volumes: - '/home/sa/data/mongod.conf:/etc/mongod.conf' - '/home/sa/data/db:/data/db' ports: - '37017:27017'
then
docker-compose up
When you run docker container using this:
docker run -d -v /var/lib/mongo:/data/db \ -v /home/user/mongo.conf:/etc/mongo.conf -p port:port image_name
/var/lib/mongo
is a host's mongo folder.
/data/db
is a folder in docker container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With