<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mehul Prajapati</title>
    <description>The latest articles on DEV Community by Mehul Prajapati (@mehulcs).</description>
    <link>https://dev.to/mehulcs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F166501%2F61a10592-0e34-4190-afba-8ce54ef279a0.jpg</url>
      <title>DEV Community: Mehul Prajapati</title>
      <link>https://dev.to/mehulcs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehulcs"/>
    <language>en</language>
    <item>
      <title>Add new Volumes to a running container in docker-compose</title>
      <dc:creator>Mehul Prajapati</dc:creator>
      <pubDate>Tue, 03 Mar 2020 15:16:38 +0000</pubDate>
      <link>https://dev.to/mehulcs/add-new-volumes-to-a-running-container-in-docker-compose-nhh</link>
      <guid>https://dev.to/mehulcs/add-new-volumes-to-a-running-container-in-docker-compose-nhh</guid>
      <description>&lt;p&gt;Consider a situation where you have a set of services running together with docker-compose. Now, For one of your services, You want to have one directory of service to be exported and synced with the host. In other terms, you want to have volume for that directory. ( e.g Downloads, Uploads).&lt;/p&gt;

&lt;p&gt;This post shows how to add volume to the running container without letting other services of the network going down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Copy contents of directory from container&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Create a new image from the running container (Commit)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Stop and Delete running container&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Create a container from the image and specify volume and Network&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1 - Copy
&lt;/h2&gt;

&lt;p&gt;If path, where we are going to add volume, is not empty, then make sure to copy the content to the host system, As adding a volume will overwrite container data at that location.&lt;/p&gt;

&lt;p&gt;List the containers and copy the &lt;code&gt;id&lt;/code&gt; of running container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the contents of the directory from container, At which volume is going to be mounted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;cp&lt;/span&gt; &amp;lt;container-id&amp;gt;:&amp;lt;container path to directory&amp;gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2 - Create new Image
&lt;/h2&gt;

&lt;p&gt;This step will create an image from the running container. So the state of running service is preserved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker commit &amp;lt;container-id&amp;gt; &amp;lt;new-image-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3 - Delete container
&lt;/h2&gt;

&lt;p&gt;Delete the running container, so new container with added volume will take this place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stop &amp;lt;container-id&amp;gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; docker &lt;span class="nb"&gt;rm&lt;/span&gt; &amp;lt;container-id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4 - Create a new container
&lt;/h2&gt;

&lt;p&gt;Now, We have an image (snapshot) of container available, Which we can use to create another container and add a new volume.&lt;/p&gt;

&lt;p&gt;Before proceeding, One important thing to notice is that Our services are running together with &lt;strong&gt;docker-compose&lt;/strong&gt;. They can communicate with each other through the network created by &lt;strong&gt;docker-compose&lt;/strong&gt;. Now if we create and run the new container. This container will not able to access other services as a new container is not part of the network.  So while creating new container, we have to add this container in the existing &lt;strong&gt;docker-compose&lt;/strong&gt; network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. List out the list of docker-compose networks and copy network name&lt;/strong&gt;&lt;br&gt;
This will list all the networks, list will include some default networks and also for your service. Find out relevant network and copy network name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Get more details about the network by running &lt;strong&gt;docker inspect NETWORK_ID&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2. Create a new container and run in the existing network&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
        &lt;span class="nt"&gt;-v&lt;/span&gt; &amp;lt;copied-directory&amp;gt;:&amp;lt;path to volume&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
        &lt;span class="nt"&gt;--network&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;network-name&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
        image-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;copied-directory&lt;/strong&gt; - Path to directory copied in &lt;strong&gt;step 1&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;network-name&lt;/strong&gt; - Network name identified in &lt;strong&gt;step 4.1&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;image-name&lt;/strong&gt; - Image name created in &lt;strong&gt;step 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Done.&lt;br&gt;
New service is up in &lt;strong&gt;docker-compose&lt;/strong&gt; network with new volume.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>tips</category>
    </item>
  </channel>
</rss>
