Docker + Mongo DB + Volume Mount (Persistence)

We observe that once the docker container is removed,  we lose the entire data along with that container and cant recover which was very precious for the business continuity. πŸ˜” 

Here we will fix the issue by mounting the volume in the docker-compose file.

Prerequisites: Basic awareness about Docker containers and how to make it up.


Create docker-compose.yaml file as below 


This configuration will pull the latest mongo image from the docker hub.

Re-creating the data lose Scenario 

Execute the docker file using the command docker-compose up -d 

"-d" preferable to run in a detached mode

run docker ps to check the container status 



Will try to launch to mongo terminal by hitting the below command 

docker exec -it c6d1608207ca bash

(Used container id can use container name also)

Once the terminal got opened execute  mongo admin -u root -p example will help to connect to the mong DB






view the database using show dbs





Now we can create a new database using the command use address

Then create collection 'person' like  db.person.save({name:"Sudeep",location:"Bangalore"}) 








Try to retrieve the record like below : db.person.find({name:"Sudeep"})




exit from the bash by executing: exit 




now stop and remove the container from the docker, and repeated the above procedure to connect the MongoDB shell.

  • docker container stop $(docker ps -a -q)
  • docker rm $(docker ps -a -q)
  • docker ps -a (To check the container availability, there should not be mongo containers)
Now we removed the mongo live instance from the docker. will try to launch the mongo again and check the existence of the address database.

execute docker docker-compose up -d and run docker ps to check the status of mongo

Try to launch the terminal of mongo using docker exec -it <container name/containerid> bash

Enter to MongoDB shell: mongo admin -u root -p example 
After successful connectivity, execute the command: show dbs, we can see that the address database is missing.😞









Fixing Data Loses

Edit the existing docker-compose.yaml with the volume changes
    volumes:
      - mongodb_data_container:/data/db
volumes:
  mongodb_data_container:  
   
/data/db inside the container, where MongoDB by default will write its data files. 

The final docker-compose.yaml will look like this.
















Now practice the above steps that we tried to create address database and person collections. After successful creation remove the containers as described in the above steps.

Connect again and execute show dbs can see the database 'address' created in our previous session











execute use address and search for entry  using the command db.person.find.({key:"Value"}) 









To see the data storage where we mounted the path inside the container, docker-compose.yaml we mentioned data/db

exit from the mongo shell and enter to the bash by doing docker exec -it <containreid> sh.
List all the resources by entering the command: ls




navigate to path data/db where we can find the persistent files stored. 😎






Comments

  1. Wynn Casino - Mapyro
    The Wynn Hotel and Casino in Las Vegas is a 10-minute κ΄‘μ£Ό 좜μž₯λ§ˆμ‚¬μ§€ drive from the Strip and 7 minutes from 원주 좜μž₯μ•ˆλ§ˆ the Las Vegas Convention Center. μ‚Όμ²™ 좜μž₯λ§ˆμ‚¬μ§€ This hotel is connected to its sister property,  Rating: 7.7/10 · κΉ€ν•΄ 좜μž₯샡 ‎2,960 μ–‘μ£Ό 좜μž₯μ•ˆλ§ˆ reviews

    ReplyDelete
  2. TitaniumBand Rings: Gold Rings - Titanium Art
    This piece of micro touch hair trimmer the ring contains titanium teeth 1/4" x titanium granite countertops 1/4" x 1/4" x 3" sterling aluminum rings for use in jewelry. No need to put anything in the ring.$12.99 titanium fidget spinner · titanium iv chloride ‎In stock

    ReplyDelete

Post a Comment

Popular posts from this blog

Linux Basics

JAVA 8 Predefined Functions

RabbitMQ