{"id":3910,"date":"2023-04-03T05:00:46","date_gmt":"2023-04-03T05:00:46","guid":{"rendered":"https:\/\/blog.osmosys.co\/?p=3910"},"modified":"2024-02-02T04:57:12","modified_gmt":"2024-02-02T04:57:12","slug":"backup-and-restore-of-docker-volumes-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/","title":{"rendered":"Backup and Restore of Docker Volumes: A Step-by-Step Guide"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div>\n<p><\/p>\n\n\n\n<p>As more and more developers turn to Docker to streamline their development processes, it becomes increasingly important to understand how to take backup and restore a volume in Docker. In this article, we&#8217;ll explore the pain areas around volume backup and restoration, the reasons for doing it, and provide a comprehensive solution that will help you take backup and restore volumes with ease.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-what\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-what\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_1 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\r\n<div class=\"ez-toc-title-container\">\r\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\r\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\r\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#What\" >What?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Why\" >Why?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Disaster_Recovery\" >Disaster Recovery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Testing_and_Development\" >Testing and Development<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Replication\" >Replication<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#How\" >How?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Step_1_Identify_the_Volume\" >Step 1: Identify the Volume<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Step_2_Create_a_Backup\" >Step 2: Create a Backup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Step_3_Move_the_Backup_File_to_an_External_Server\" >Step 3: Move the Backup File to an External Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Step_4_Restore_the_Volume\" >Step 4: Restore the Volume<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Example\" >Example<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Setup_MongoDB_Docker\" >Setup MongoDB Docker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Modify_MongoDB_Data\" >Modify MongoDB Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Take_Backup\" >Take Backup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Reset_Docker\" >Reset Docker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Restore_Backup\" >Restore Backup<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/staging.osmosys.co\/uk\/backup-and-restore-of-docker-volumes-a-step-by-step-guide\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\r\n<h2 class=\"wp-block-heading\" id=\"heading-what\"><span class=\"ez-toc-section\" id=\"What\"><\/span><strong>What?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Docker has become an essential tool for many developers in recent years, allowing them to create and manage containerized applications with ease. One of the most significant advantages of Docker is its ability to manage data volumes, allowing you to store data that persists beyond the lifetime of a container.<\/p>\n\n\n\n<p>However, even with its many benefits, Docker&#8217;s volume management can sometimes be challenging. In particular, taking backups and restoring volumes can be a complex and error-prone process. In this article, we&#8217;ll explore why you might need to take backups of your volumes and how to do it correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-why\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-why\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-why\"><span class=\"ez-toc-section\" id=\"Why\"><\/span><strong>Why?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are many compelling reasons to do so. Here are a few reasons why you might need to take backups of your Docker volumes:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-disaster-recovery\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-disaster-recovery\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-disaster-recovery\"><span class=\"ez-toc-section\" id=\"Disaster_Recovery\"><\/span><strong>Disaster Recovery<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The most obvious reason to take backups of your Docker volumes is to protect against data loss in the event of a disaster. By taking regular backups of your volumes, you can ensure that your critical data is safe and recoverable in case of an emergency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-testing-and-development\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-testing-and-development\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-testing-and-development\"><span class=\"ez-toc-section\" id=\"Testing_and_Development\"><\/span><strong>Testing and Development<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another reason to take backups of your Docker volumes is for testing and development purposes. By creating backups of your volumes at different stages of the development process, you can easily roll back to a previous state if you encounter problems or need to test new features.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-replication\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-replication\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-replication\"><span class=\"ez-toc-section\" id=\"Replication\"><\/span><strong>Replication<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Taking backups of your Docker volumes can also be useful for replicating data across multiple environments. By creating backups of your volumes and restoring them in other environments, you can ensure that your data is consistent across all of your development, staging, and production environments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-how\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-how\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-how\"><span class=\"ez-toc-section\" id=\"How\"><\/span><strong>How?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now that we&#8217;ve explored the pain areas and reasons for taking backups and restoring volumes in Docker, let&#8217;s take a look at how to do it correctly. Here are the steps you should follow to take backups of your volumes:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-1-identify-the-volume\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-1-identify-the-volume\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-1-identify-the-volume\"><span class=\"ez-toc-section\" id=\"Step_1_Identify_the_Volume\"><\/span><strong>Step 1: Identify the Volume<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The first step in taking a backup of a volume is to identify the volume that you want to back up. You can do this by running the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker volume ls<\/pre>\n\n\n\n<p>This will show you a list of all the volumes that are currently available on your Docker host. Note down the name of the volume that you want to back up.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-2-create-a-backup\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-2-create-a-backup\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-2-create-a-backup\"><span class=\"ez-toc-section\" id=\"Step_2_Create_a_Backup\"><\/span><strong>Step 2: Create a Backup<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To create a backup of the volume, you can use the&nbsp;<code>docker run<\/code>&nbsp;command to start a container that mounts the volume you want to back up and a separate container that writes the backup data to a file.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to do this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker run --rm \\\n--mount source=&lt;volume-name&gt;,target=&lt;target&gt; \\\n-v $(pwd):\/backup \\\nbusybox \\\ntar -czvf \/backup\/&lt;backup-filename&gt;.tar.gz &lt;target&gt;<\/pre>\n\n\n\n<p>In this command, replace&nbsp;<code>&lt;volume-name&gt;<\/code>&nbsp;with the name of the volume you want to back up,&nbsp;<code>&lt;target&gt;<\/code>&nbsp;with the mount point inside the docker container, and&nbsp;<code>&lt;backup-filename&gt;<\/code>&nbsp;with a name for the backup file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-3-move-the-backup-file-to-an-external-server\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-3-move-the-backup-file-to-an-external-server\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-3-move-the-backup-file-to-an-external-server\"><span class=\"ez-toc-section\" id=\"Step_3_Move_the_Backup_File_to_an_External_Server\"><\/span><strong>Step 3: Move the Backup File to an External Server<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>After you have created a backup file, it&#8217;s a good idea to move it to an external server or storage device to ensure that it&#8217;s safe and secure. Storing the backup file on a separate server or storage device can help to protect it in the event of a disaster, such as a server failure or a security breach.<\/p>\n\n\n\n<p>To move the backup file to an external server, you can use SCP.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>SCP<\/strong>: Secure Copy (SCP) is a secure file transfer protocol that allows you to transfer files between servers using SSH. To use SCP, you will need to have SSH access to both the source and destination servers. You can use the following command to copy the backup file to the external server:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">scp \/path\/to\/backupfile user@external-server:\/path\/to\/destination<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-4-restore-the-volume\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-step-4-restore-the-volume\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-4-restore-the-volume\"><span class=\"ez-toc-section\" id=\"Step_4_Restore_the_Volume\"><\/span><strong>Step 4: Restore the Volume<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you need to restore the volume from the backup, you can use the&nbsp;<code>docker run<\/code>&nbsp;command to start a container that mounts the backup file and a separate container that writes the backup data to the volume.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to do this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker run --rm \\\n--mount source=&lt;volume-name&gt;,target=&lt;target&gt; \\\n-v $(pwd):\/backup \\\nbusybox \\\ntar -xzvf \/backup\/&lt;backup-filename&gt;.tar.gz -C \/<\/pre>\n\n\n\n<p>In this command, replace&nbsp;<code>&lt;volume-name&gt;<\/code>&nbsp;with the name of the volume you want to back up,&nbsp;<code>&lt;target&gt;<\/code>&nbsp;with the mount point inside the docker container, and&nbsp;<code>&lt;backup-filename&gt;<\/code>&nbsp;with a name for the backup file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-example\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-example\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-example\"><span class=\"ez-toc-section\" id=\"Example\"><\/span><strong>Example<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For the purpose of this blog, we will be setting up a basic Docker environment for MongoDB, along with an Express application to visualize the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setup-mongodb-docker\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-setup-mongodb-docker\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setup-mongodb-docker\"><span class=\"ez-toc-section\" id=\"Setup_MongoDB_Docker\"><\/span><strong>Setup MongoDB Docker<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Create a&nbsp;<code>docker-compose.yml<\/code>&nbsp;file.<\/p>\n\n\n\n<p>You can use the below example to setup MongoDB docker:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Use root\/example as user\/password credentials\nversion: '3.1'\n\nservices:\n\n  mongo:\n    image: mongo\n    container_name: mongo\n    restart: always\n    environment:\n      MONGO_INITDB_ROOT_USERNAME: root\n      MONGO_INITDB_ROOT_PASSWORD: example\n    volumes:\n      - mongodb:\/data\/db\n\n  mongo-express:\n    image: mongo-express\n    container_name: mongo-express\n    restart: always\n    ports:\n      - 8081:8081\n    environment:\n      ME_CONFIG_MONGODB_ADMINUSERNAME: root\n      ME_CONFIG_MONGODB_ADMINPASSWORD: example\n      ME_CONFIG_MONGODB_URL: mongodb:\/\/root:example@mongo:27017\/\n\nvolumes:\n  mongodb: ~\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In the above example, I added a named volume&nbsp;<code>mongodb<\/code>. Here we have configured docker to persist its data to a volume.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker-compose up<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>To check the running docker containers you can use the below command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker ps<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>You should see 2 docker containers running i.e.&nbsp;<code>mongo<\/code>&nbsp;and&nbsp;<code>mongo-express<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                       NAMES\n5f68eaf8184a   mongo-express   \"tini -- \/docker-ent\u2026\"   11 seconds ago   Up 9 seconds    0.0.0.0:8081-&gt;8081\/tcp, :::8081-&gt;8081\/tcp   mongo-express\n6789cf1e7bed   mongo           \"docker-entrypoint.s\u2026\"   11 seconds ago   Up 10 seconds   27017\/tcp                                   mongo<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>You can also view the volume which you created using the following command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker volume ls<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>For me the output looks like this<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DRIVER    VOLUME NAME\nlocal     82fd19dfc5dc839a51b2afd59614474038cd0ce8b494739d883d9b503cd0c61e\nlocal     32582d9a00557c9229418062e6308515c4c4c7eb0bb14a8c43203cafffb675c5\nlocal     docker-test_mongodb<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The volume name&nbsp;<code>docker-test_mongodb<\/code>&nbsp;was automatically created when I placed my&nbsp;<code>docker-composer.yml<\/code>&nbsp;file inside the&nbsp;<code>docker-test folder<\/code>. This is the location where all of our MongoDB data will be stored.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-modify-mongodb-data\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-modify-mongodb-data\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-modify-mongodb-data\"><span class=\"ez-toc-section\" id=\"Modify_MongoDB_Data\"><\/span><strong>Modify MongoDB Data<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To confirm that we have successfully restored the database from the volume, we need to make a minor modification to the database.<\/p>\n\n\n\n<p>Visit&nbsp;<code>0.0.0.0:8081<\/code>&nbsp;where the Mongo Express is running.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.hashnode.com\/res\/hashnode\/image\/upload\/v1680247432450\/75d1669d-e2a3-4609-8766-ea9b6581e01f.png?auto=compress,format&amp;format=webp\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Create a new database here. I created&nbsp;<code>my-new-db<\/code>&nbsp;as the new database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.hashnode.com\/res\/hashnode\/image\/upload\/v1680247514236\/6a853b9f-3036-400d-85ff-74935e1a253c.png?auto=compress,format&amp;format=webp\" alt=\"\" \/><\/figure>\n\n\n\n<p>So now we need to ensure that this new database that is created is also restored.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-take-backup\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-take-backup\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-take-backup\"><span class=\"ez-toc-section\" id=\"Take_Backup\"><\/span><strong>Take Backup<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s take the backup of the volume now.<\/p>\n\n\n\n<p>Please run the following command to take the backup.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  docker run --rm \\\n  --mount source=docker-test_mongodb,target=\/data\/db \\\n  -v $(pwd):\/backup \\\n  busybox \\\n  tar -czvf \/backup\/docker-test_mongodb.tar.gz \/data\/db<\/pre>\n\n\n\n<p><code>\/data\/db<\/code>&nbsp;here is the path that is mounted inside the docker container for the volume.<\/p>\n\n\n\n<p>You should see a backup file created with&nbsp;<code>docker-test_mongodb.tar.gz<\/code>&nbsp;as the file name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-reset-docker\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-reset-docker\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-reset-docker\"><span class=\"ez-toc-section\" id=\"Reset_Docker\"><\/span><strong>Reset Docker<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>As I will be performing the restore process on the same machine, I should delete both the container and the volume.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker rm -v mongo-express<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker rm -v mongo<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker volume rm docker-test_mongodb<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-restore-backup\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-restore-backup\" target=\"_blank\" rel=\"noopener\"><\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-restore-backup\"><span class=\"ez-toc-section\" id=\"Restore_Backup\"><\/span><strong>Restore Backup<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>I will proceed to recreate the containers, and we can confirm that the volume has been deleted by checking the databases on Mongo Express.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.hashnode.com\/res\/hashnode\/image\/upload\/v1680247432450\/75d1669d-e2a3-4609-8766-ea9b6581e01f.png\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now we can run the below command to restore the backup.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker run --rm \\\n--mount source=docker-test_mongodb,target=\/data\/db \\\n-v $(pwd):\/backup \\\nbusybox \\\ntar -xzvf \/backup\/docker-test_mongodb.tar.gz -C \/<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Go to Mongo Express and refresh the page.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.hashnode.com\/res\/hashnode\/image\/upload\/v1680247514236\/6a853b9f-3036-400d-85ff-74935e1a253c.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>You will now be able to see that the database which we previously created has been successfully restored.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/headingtag.com\/how-to-take-backup-and-restore-a-volume-in-docker#heading-conclusion\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Taking backups and restoring volumes in Docker can be a complex and error-prone process. However, with the right approach, it&#8217;s possible to ensure that your critical data is safe and recoverable in the event of a disaster. By following the steps outlined in this article, you can take backups of your volumes with ease and restore them when necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As more and more developers turn to Docker to streamline their development processes, it becomes increasingly important to understand how to take backup and restore a volume in Docker. In this article, we&#8217;ll explore the pain areas around volume backup and restoration, the reasons for doing it, and provide a comprehensive solution that will help [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":4010,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[37],"tags":[64,65],"class_list":["post-3910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","tag-devops","tag-dockers"],"modified_by":null,"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts\/3910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/comments?post=3910"}],"version-history":[{"count":0,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts\/3910\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/media?parent=3910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/categories?post=3910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/tags?post=3910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}