![]() Here I am using the ~/Downloads destination as an example: ❯ docker cp :/dump ~/Downloads/dump Method two: Pipe results directly out of the container into a single dump Now that we have the resulting files we can copy them out of the container and to our host. Or if you use a mongodb+srv or similar URL to connect to your database instead: ❯ docker exec -i /usr/bin/mongodump -uri "" -out /dump Here's an example that dumps your entire database to the /dump directory within your container: ❯ docker exec -i /usr/bin/mongodump -username -password -authenticationDatabase admin -db -out /dump For this we can use an intermediary step of storing the dumped database files in a directory within the container, which we can then copy out to our host using docker cp – or if you already have a host directory mounted as a volume, you can use that and skip the docker cp step altogether of course. If this is the method you'd like to use, we won't be able to rely on shell piping to get the files directly out of our database container. Method one: Mongodump's default individual collection dump filesīy default a mongodump export generates individual files, one for reach collection. This assumes your source database is (also) in a container, but the command would be roughly identical if you your source database is directly installed on your (virtual) machine too. And the database ie pre-populated for development, and can be reset any time.We can use the following command to create a dump of your entire database, all collections included. They only existed in memory during build time. So now the MongoDB database has two users set up and the passwords are not in either the build scripts or in an environment variable. Do all the dev build stuff (omitted for brevity) Parameters(, description: 'Reset restores the database to starting state', name: 'BUILD_TYPE')]), Static final String BUILD_TYPE_RESET = 'Reset' ![]() Static final String BUILD_TYPE_DEV = 'Development' In the oovy configuration of the pipeline, one of the steps can simply be to execute the above shell script: // Lots of stuff omitted for brevity. If thenĮcho 'DATABASE: *** Loading or Restoring Initial Data ***'ĭocker exec test-db mongo sample -u $APP_USER -p $APP_PASS -authenticationDatabase courthack /data/setup.jsĮcho "DATABASE: *** Database Setup Complete ***" We’ll create a JavaScript file with all the base data being inserted something like this, but with many more lines and collections: db.request.remove() "ĭocker exec test-db touch /data/db/.mongodb_password_set Let’s start with a script to populate the database. I’m not going to cover all aspects of the build job, but we are using Docker Compose to create a container for a Node app and a container for MongoDB. Our build script defines multiple build types, for the purposes of this article we’ll focus on two of them, Development and Reset, where Development is the default and happens on every git push, and Reset is used to set the database back to this initial state. Our build system is based on Jenkins, and we are using Pipelines so our build job configuration is persisted in source control. Allow the database to be set back to initial state.Populate the collections with initial state. ![]()
0 Comments
Leave a Reply. |