{"id":389123,"date":"2022-01-03T23:44:42","date_gmt":"2022-01-03T20:44:42","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-share-data-between-docker-containers-cloudsavvy-it\/"},"modified":"2022-01-03T23:44:42","modified_gmt":"2022-01-03T20:44:42","slug":"how-to-share-data-between-docker-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/","title":{"rendered":"#How to Share Data Between Docker Containers \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a242f68189b9\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" 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: #dd3333;color:#dd3333\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a242f68189b9\" checked aria-label=\"Toggle\" \/><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:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/#Using_Volumes_to_Share_a_Directory\" >Using Volumes to Share a Directory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/#Quickly_Starting_Containers_With_Matching_Volumes\" >Quickly Starting Containers With Matching Volumes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/#Improving_Safety_With_Read-Only_Mounts\" >Improving Safety With Read-Only Mounts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/#Sharing_Data_Over_A_Network\" >Sharing Data Over A Network<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-share-data-between-docker-containers-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Share Data Between Docker Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-14169\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker containers are intentionally isolated environments. Each container has its own filesystem which can\u2019t be directly accessed by other containers or your host.<\/p>\n<p>Sometimes containers may need to share data. Although you should aim for containers to be self-sufficient, there are scenarios where data sharing is unavoidable. This might be so a second container can access a combined cache, use a file-backed database, create a backup, or perform operations on user-generated data, such as an image optimizer container that processes profile photos uploaded via a separate web server container.<\/p>\n<p>In this guide, we\u2019ll look at a few methods for passing data between your Docker containers. We\u2019ll assume you\u2019ve already got Docker set up and are familiar with fundamental concepts such as containers, images, volumes, and networks.<\/p>\n<h2 id=\"using-volumes-to-share-a-directory\"><span class=\"ez-toc-section\" id=\"Using_Volumes_to_Share_a_Directory\"><\/span>Using Volumes to Share a Directory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Volumes are the de facto way to set up data sharing. They\u2019re independent filesystems that store their data outside any individual container. Mounting a volume to a filesystem path within a container provides read-write access to the volume\u2019s data.<\/p>\n<p>Volumes can be attached to multiple containers simultaneously. This facilitates seamless data sharing and persistence that\u2019s managed by Docker.<\/p>\n<p>Create a volume to begin:<\/p>\n<pre>docker volume create --name shared-data<\/pre>\n<p>Next create your containers, mounting the volume to the filesystem path expected by each image:<\/p>\n<pre>docker run -d -v shared-data:\/data --name example example-image:latest&#13;\ndocker run -d -v shared-data:\/backup-source --name backup backup-image:latest<\/pre>\n<p>In this example, the <code>backup<\/code> container will gain effective access to the <code>example<\/code> container\u2019s <code>\/data<\/code> directory. It\u2019ll be mounted as <code>\/backup-source<\/code>; changes made by either container will be reflected in the other.<\/p>\n<h2 id=\"quickly-starting-containers-with-matching-volumes\"><span class=\"ez-toc-section\" id=\"Quickly_Starting_Containers_With_Matching_Volumes\"><\/span>Quickly Starting Containers With Matching Volumes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The example above could be simplified using the <code>docker run<\/code> command\u2019s <code>--volumes-from<\/code>flag. This provides a mechanism to automatically mount volumes that are already used by an existing container:<\/p>\n<pre>docker run -d --volumes-from example --name backup backup-image:latest<\/pre>\n<p>This time the <code>backup<\/code> container will receive the <code>shared-data<\/code> volume mounted into its <code>\/data<\/code> directory. The <code>--volumes-from<\/code> flag pulls in all the volume definitions attached to the <code>example<\/code> container. It\u2019s particularly ideal for backup jobs and other short-lived containers which act as auxiliary components to your main service.<\/p>\n<h2 id=\"improving-safety-with-read-only-mounts\"><span class=\"ez-toc-section\" id=\"Improving_Safety_With_Read-Only_Mounts\"><\/span>Improving Safety With Read-Only Mounts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Volumes are always mounted in read-write mode by default. All your containers with access to a volume are permitted to change its contents, potentially causing unintended data loss.<\/p>\n<p>It\u2019s best practice to mount shared volumes in read-only mode when a container isn\u2019t expected to make modifications. In the above example, the <code>backup<\/code> container only needs to read the content of the <code>shared-data<\/code> volume. Setting the mount to read-only mode enforces this expectation, preventing bugs or malicious binaries in the image from deleting data used by the <code>example<\/code> container.<\/p>\n<pre>docker run -d -v shared-data:\/backup-source:ro --name backup backup-image:latest<\/pre>\n<p>Adding <code>ro<\/code> as a third colon-separated parameter to the <code>-v<\/code> flag indicates the volume should be mounted in read-only mode. You can also write <code>readonly<\/code> instead of <code>ro<\/code> as a more explicit alternative.<\/p>\n<h2 id=\"sharing-data-over-the-network\"><span class=\"ez-toc-section\" id=\"Sharing_Data_Over_A_Network\"><\/span>Sharing Data Over A Network<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use network exchanges as an alternative <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach to data sharing via filesystem volumes. Joining two containers to the same Docker network lets them seamlessly communicate using auto-assigned hostnames:<\/p>\n<pre>docker network create demo-network&#13;\ndocker run -d --net demo-network --name first example-image:latest&#13;\ndocker run -d --net demo-network --name second another-image:latest<\/pre>\n<p>Here <code>first<\/code> will be able to ping <code>second<\/code> and vice versa. Your containers could run an HTTP API service enabling them to interact with each others\u2019 data.<\/p>\n<p>Continuing the backup example, now your <code>backup<\/code> container could make a network request to <code>http:\/\/example:8080\/backup-data<\/code> to acquire the data to backup. The <code>example<\/code> container should respond with an archive containing all the data that needs to be stored. The backup container then has responsibility for persisting the archive to a suitable storage location.<\/p>\n<p>Enforcing that data sharing occurs over a network often aids decoupling efforts. You end up with clearly defined interfaces that don\u2019t create hard dependencies between services. Data access can be more precisely controlled by exposing APIs for each data type, instead of giving every container total access to a volume.<\/p>\n<p>It\u2019s important to consider security if you use this approach. Make sure any HTTP APIs that are designed for internal access by your other Docker containers don\u2019t have ports exposed on your Docker host\u2019s bridge network. This is the default behavior when using the network options shown above; binding a port with <code>-p 8080:8080<\/code> would allow access to the backup API via your host\u2019s network interfaces. This would be a security issue.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker containers are isolated environments that can\u2019t access each others\u2019 filesystems. Nonetheless you can share data by creating a volume that\u2019s mounted into all participating containers. Using a shared Docker network is an alternative option that provides stronger separation in scenarios where direct filesystem interactions aren\u2019t necessary.<\/p>\n<p>It\u2019s good practice to limit inter-container interactions as far as possible. Cases where you need data sharing should be clearly defined to avoid tightly coupling your services together. Containers that have a rigid dependency on data from <em>another<\/em> container can be trickier to deploy and maintain over time, eroding the broader benefits of containerization and isolation.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/15112\/how-to-share-data-between-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Share Data Between Docker Containers \u2013 CloudSavvy IT&#8221; Docker containers are intentionally isolated environments. Each container has its own filesystem which can\u2019t be directly accessed by other containers or your host. Sometimes containers may need to share data. Although you should aim for containers to be self-sufficient, there are scenarios where data sharing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":389124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-389123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/389123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=389123"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/389123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/389124"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=389123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=389123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=389123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}