title: WSL2: Access container build result from host: `docker cp` vs. bind mount
body: <p>I am using Docker with WSL2 to build a project and would like to access the build results of the container from the host.</p>
  <p>It appears there are two ways for accessing the build results:</p>
  <ul>
  <li>Use <code>docker cp</code></li>
  <li>Use bind mount</li>
  </ul>
  <p>With WSL2 there is a performance decrease when bind mounting the Windows file system (opposed to the WSL2 <code>\\wsl$</code> Linux file system), see <a href="https://stackoverflow.com/q/64484579">Docker Desktop - Filesharing notification about poor performance</a> and <a href="https://docs.microsoft.com/en-us/windows/wsl/compare-versions#performance-across-os-file-systems" rel="nofollow noreferrer">WSL Documentation</a>. However, I would like to avoid using <code>\\wsl$</code>  since I would then have to make sure to pick a unique path there for storing the build results, copying them to the current directory and then properly cleaning the <code>\\wsl$</code> dir up again (unless I am misunderstand something here?).</p>
  <p>This leaves me with the following two options:</p>
  <ul>
  <li>Use <code>docker cp</code><br />
  This requires three Docker commands:
  <ol>
  <li>Build the artifact<br />
  <code>docker container run ...</code></li>
  <li>Copy the artifact to the host<br />
  <code>docker cp ...</code></li>
  <li>Remove container<br />
  <code>docker container rm ...</code></li>
  </ol>
  </li>
  <li>Use bind mount
  <ol>
  <li>Build the artifact in a non-mounted directory (for performance, see above)</li>
  <li>Copy the artifact to the mounted directory</li>
  </ol>
  </li>
  </ul>
  <p>My question is now: Which of these options is more efficient? Or are there any other efficient and simple alternatives?</p>
  <p>I would assume <code>docker cp ...</code> <em>could</em> be more efficient since it might be able to access the container data without the performance penalty a bind mount would have; however the three separate Docker commands deter me a bit since if any of these fails (for whatever reason) I manually have to copy the artifact or clean up the container.</p>