<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Chinmay's Blog]]></title><description><![CDATA[Chinmay's Blog]]></description><link>https://blog.chinmaysingh.me</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 22:21:04 GMT</lastBuildDate><atom:link href="https://blog.chinmaysingh.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to mount storage on a running docker container]]></title><description><![CDATA[Mounting storage on a running Docker container isn’t directly supported, but there are reliable workarounds to achieve similar results. Below, you’ll find two practical methods with step-by-step instructions and relevant citations.

Method 1: Copy Fi...]]></description><link>https://blog.chinmaysingh.me/how-to-mount-storage-on-a-running-docker-container</link><guid isPermaLink="true">https://blog.chinmaysingh.me/how-to-mount-storage-on-a-running-docker-container</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Docker]]></category><category><![CDATA[Devops]]></category><category><![CDATA[tools]]></category><dc:creator><![CDATA[Chinmay S]]></dc:creator><pubDate>Sun, 16 Feb 2025 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/cV9-hOgoaok/upload/46e4cf29852972426bfa1600e0645291.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Mounting storage on a running Docker container isn’t directly supported, but there are reliable workarounds to achieve similar results. Below, you’ll find two practical methods with step-by-step instructions and relevant citations.</p>
<hr />
<h2 id="heading-method-1-copy-files-directly-with-docker-cp">Method 1: Copy Files Directly with <code>docker cp</code></h2>
<p>While you cannot mount a new volume on a running container, you can transfer files between your host and the container using the <code>docker cp</code> command. This is useful for one-time file transfers but does not create a persistent mount.</p>
<p><strong>Command Syntax</strong>:</p>
<pre><code class="lang-plaintext">bash# Copy from container to host
docker cp &lt;containerId/containerName&gt;:/path/in/container /path/on/host

# Copy from host to container
docker cp /path/on/host &lt;containerId/containerName&gt;:/path/in/container
</code></pre>
<p><strong>Example</strong>:</p>
<pre><code class="lang-plaintext">bashdocker cp my_container:/app/config.json ./local_dir
docker cp ./updated_file.txt my_container:/app/data
</code></pre>
<p><strong>Limitations</strong>:</p>
<ul>
<li><p>No real-time synchronization; changes are not automatically reflected.</p>
</li>
<li><p>Manual process; must be repeated for each transfer<a target="_blank" href="https://kodekloud.com/blog/docker-cp/">2</a>.</p>
</li>
</ul>
<p>For more details, see the [Docker documentation on <code>docker cp</code>]<a target="_blank" href="https://kodekloud.com/blog/docker-cp/">2</a>.</p>
<hr />
<h2 id="heading-method-2-commit-and-recreate-the-container-with-a-volume">Method 2: Commit and Recreate the Container with a Volume</h2>
<p>For persistent storage, the best approach is to commit your running container to a new image and then launch a new container with the required volume mount.</p>
<p><strong>Steps</strong>:</p>
<ol>
<li><p><strong>Commit the existing container</strong>:</p>
<pre><code class="lang-plaintext"> bashdocker commit &lt;containerId or containerName&gt; new_image_name
</code></pre>
</li>
<li><p><strong>Run a new container with a volume</strong>:</p>
<pre><code class="lang-plaintext"> bashdocker run -v /host/path:/container/path -it new_image_name /bin/bash
</code></pre>
</li>
</ol>
<p><strong>Example workflow</strong>:</p>
<pre><code class="lang-plaintext">bashdocker commit agitated_newton my_ubuntu
docker run -v "$PWD/somedir":/somedir -it my_ubuntu /bin/bash
</code></pre>
<p><strong>Advantages</strong>:</p>
<ul>
<li><p>Enables persistent storage via Docker-managed volumes or bind mounts<a target="_blank" href="https://docs.docker.com/engine/storage/volumes/">1</a><a target="_blank" href="https://docs.docker.com/engine/storage/">6</a>.</p>
</li>
<li><p>Retains changes made in the original container.</p>
</li>
</ul>
<p><strong>Note</strong>: This method requires stopping the old container and using the new one for continued work<a target="_blank" href="https://betterstack.com/community/questions/how-to-add-volume-to-existing-docker-container/">4</a>.</p>
<hr />
<h2 id="heading-why-cant-you-mount-storage-directly-on-a-running-container">Why Can’t You Mount Storage Directly on a Running Container?</h2>
<p>Docker’s architecture does not support adding new mounts to a running container. Mounts (volumes, bind mounts, tmpfs) must be specified at container creation using the <code>--mount</code> or <code>-v</code> flags<a target="_blank" href="https://docs.docker.com/engine/storage/volumes/">1</a><a target="_blank" href="https://docs.docker.com/engine/storage/">6</a>. For persistent or shared storage, always plan your mounts before starting the container.</p>
<hr />
<h2 id="heading-summary-table">Summary Table</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Scenario</strong></td><td><strong>Method</strong></td><td><strong>Reference</strong></td></tr>
</thead>
<tbody>
<tr>
<td>One-time file transfer</td><td><code>docker cp</code></td><td><a target="_blank" href="https://kodekloud.com/blog/docker-cp/">2</a></td></tr>
<tr>
<td>Persistent storage needed</td><td>Commit &amp; rerun</td><td><a target="_blank" href="https://docs.docker.com/engine/storage/volumes/">1</a><a target="_blank" href="https://betterstack.com/community/questions/how-to-add-volume-to-existing-docker-container/">4</a><a target="_blank" href="https://docs.docker.com/engine/storage/">6</a></td></tr>
</tbody>
</table>
</div><hr />
]]></content:encoded></item><item><title><![CDATA[How to Restore SSH Access to a Google Cloud VM After Blocking Port 22]]></title><description><![CDATA[Accidentally blocking SSH (port 22) on your Google Cloud VM can be a nerve-wracking experience, especially if you’ve locked yourself out by misconfiguring the firewall. Fortunately, Google Cloud provides a robust way to recover access using the Seria...]]></description><link>https://blog.chinmaysingh.me/how-to-restore-ssh-access-to-a-google-cloud-vm-after-blocking-port-22</link><guid isPermaLink="true">https://blog.chinmaysingh.me/how-to-restore-ssh-access-to-a-google-cloud-vm-after-blocking-port-22</guid><category><![CDATA[Cloud]]></category><category><![CDATA[Google Cloud Platform]]></category><category><![CDATA[ssh]]></category><category><![CDATA[AWS]]></category><dc:creator><![CDATA[Chinmay S]]></dc:creator><pubDate>Mon, 23 Dec 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/4vzYQAuMcMA/upload/9e9a9d7e3c6b3f9464eb90f18701c0cf.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Accidentally blocking SSH (port 22) on your Google Cloud VM can be a nerve-wracking experience, especially if you’ve locked yourself out by misconfiguring the firewall. Fortunately, Google Cloud provides a robust way to recover access using the Serial Console and startup scripts. Here’s a refined, step-by-step guide inspired by Erik Fredericks’ excellent <a target="_blank" href="https://www.youtube.com/watch?v=8cKnIkYYsDQ&amp;t=611s">YouTube tutorial</a>.</p>
<hr />
<h3 id="heading-why-does-ssh-access-get-blocked"><strong>Why Does SSH Access Get Blocked?</strong></h3>
<p>A common mistake is enabling a firewall (like <code>ufw</code>) inside your VM without explicitly allowing port 22. Even if your Google Cloud VPC firewall allows SSH, a restrictive internal firewall can block all incoming SSH connections, leaving you unable to connect.</p>
<hr />
<h2 id="heading-step-by-step-recovery-guide">Step-by-Step Recovery Guide</h2>
<h3 id="heading-1-stop-your-vm">1. <strong>Stop Your VM</strong></h3>
<ul>
<li><p>Go to your VM instance in the Google Cloud Console.</p>
</li>
<li><p>Click <strong>Stop</strong> to shut down the VM. This is required to make certain configuration changes.</p>
</li>
</ul>
<hr />
<h3 id="heading-2-enable-serial-console-access">2. <strong>Enable Serial Console Access</strong></h3>
<ul>
<li><p>With the VM stopped, click <strong>Edit</strong>.</p>
</li>
<li><p>Scroll to the <strong>“Enable connecting to serial ports”</strong> option and check the box.</p>
</li>
<li><p>Save your changes. This feature lets you interact with your VM as if you were physically at its terminal.</p>
</li>
</ul>
<hr />
<h3 id="heading-3-add-a-startup-script-to-create-a-temporary-admin-user">3. <strong>Add a Startup Script to Create a Temporary Admin User</strong></h3>
<ul>
<li><p>In the VM settings, find the <strong>“Automation”</strong> or <strong>“Metadata”</strong> section.</p>
</li>
<li><p>Add a startup script like this:</p>
</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/bash</span>
useradd -m tempadmin
<span class="hljs-built_in">echo</span> <span class="hljs-string">'tempadmin:TempPassword123!'</span> | chpasswd
usermod -aG sudo tempadmin
</code></pre>
<p>Replace <code>tempadmin</code> and <code>TempPassword123!</code> with your own username and a strong, temporary password.</p>
<hr />
<h3 id="heading-4-restart-the-vm">4. <strong>Restart the VM</strong></h3>
<ul>
<li>Start your VM. The startup script runs automatically, creating a new user with sudo privileges.</li>
</ul>
<hr />
<h3 id="heading-5-connect-via-serial-console">5. <strong>Connect via Serial Console</strong></h3>
<ul>
<li><p>From the VM’s page, click <strong>Connect to Serial Console</strong>.</p>
</li>
<li><p>Log in with the temporary username and password you set in the script.</p>
</li>
</ul>
<hr />
<h3 id="heading-6-re-enable-ssh-access">6. <strong>Re-Enable SSH Access</strong></h3>
<ul>
<li>Once logged in, run:</li>
</ul>
<pre><code class="lang-bash">sudo ufw allow 22
</code></pre>
<p>This command opens port 22 for SSH.</p>
<hr />
<h3 id="heading-7-clean-up-for-security">7. <strong>Clean Up for Security</strong></h3>
<ul>
<li><p><strong>Stop the VM</strong> again.</p>
</li>
<li><p><strong>Remove the startup script</strong> from the metadata to prevent it from running again.</p>
</li>
<li><p><strong>Disable serial port access</strong> for security.</p>
</li>
<li><p><strong>Restart the VM</strong>.</p>
</li>
<li><p><strong>Log in via SSH</strong> as usual.</p>
</li>
<li><p><strong>Delete the temporary user</strong> after confirming SSH works:</p>
</li>
</ul>
<pre><code class="lang-bash">sudo deluser tempadmin
sudo rm -rf /home/tempadmin
</code></pre>
<hr />
<h2 id="heading-best-practices"><strong>Best Practices</strong></h2>
<ul>
<li><p><strong>Always allow port 22</strong> in both your VM and VPC firewalls before enabling <code>ufw</code>.</p>
</li>
<li><p><strong>Remove temporary users and scripts</strong> as soon as you regain access.</p>
</li>
<li><p><strong>Regularly back up your VM</strong> to avoid future lockouts.</p>
</li>
</ul>
<hr />
<h2 id="heading-quick-reference-table"><strong>Quick Reference Table</strong></h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Step</td><td>Action</td></tr>
</thead>
<tbody>
<tr>
<td>Stop VM</td><td>Shut down the affected VM</td></tr>
<tr>
<td>Enable Serial Console</td><td>Edit VM settings to allow serial port access</td></tr>
<tr>
<td>Add Startup Script</td><td>Insert script to create a temp admin user</td></tr>
<tr>
<td>Start VM</td><td>Boot up so the script runs</td></tr>
<tr>
<td>Connect via Serial Console</td><td>Log in with temporary credentials</td></tr>
<tr>
<td>Allow SSH in ufw</td><td>Run <code>sudo ufw allow 22</code></td></tr>
<tr>
<td>Remove Script &amp; Serial Access</td><td>Stop VM, remove script, disable serial, restart, delete temp user</td></tr>
</tbody>
</table>
</div><hr />
]]></content:encoded></item></channel></rss>