<?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: Sean Gilbertson</title>
    <description>The latest articles on DEV Community by Sean Gilbertson (@dreki).</description>
    <link>https://dev.to/dreki</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%2F554078%2F0ae3bace-5be6-48c8-90fc-f2e735ef97ef.jpeg</url>
      <title>DEV Community: Sean Gilbertson</title>
      <link>https://dev.to/dreki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dreki"/>
    <language>en</language>
    <item>
      <title>How to create a Dockerized Node application with separate node_modules for container and host</title>
      <dc:creator>Sean Gilbertson</dc:creator>
      <pubDate>Tue, 05 Jan 2021 18:02:12 +0000</pubDate>
      <link>https://dev.to/dreki/how-to-create-a-dockerized-node-application-with-separate-nodemodules-for-container-and-host-2b5n</link>
      <guid>https://dev.to/dreki/how-to-create-a-dockerized-node-application-with-separate-nodemodules-for-container-and-host-2b5n</guid>
      <description>&lt;p&gt;There's a gotcha with Docker and Node where the &lt;code&gt;node_modules&lt;/code&gt; directory on the host can override the &lt;code&gt;node_modules&lt;/code&gt; in the container. This typically happens in dev environments, where you want your container to receive updates to files from the host as you work on them.&lt;/p&gt;

&lt;p&gt;The way this works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have a Node-based project you'd like to run as a Docker container&lt;/li&gt;
&lt;li&gt;Your &lt;code&gt;Dockerfile&lt;/code&gt; runs &lt;code&gt;npm install&lt;/code&gt; when building the image&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;node_modules&lt;/code&gt; directory (likely a subdirectory of something like &lt;code&gt;/app&lt;/code&gt; in the container) gets filled with all the Node packages your &lt;code&gt;package.json&lt;/code&gt; references&lt;/li&gt;
&lt;li&gt;The rest of your Docker image is built&lt;/li&gt;
&lt;li&gt;You ask Docker to run your image as a container, asking it to link the &lt;code&gt;/app&lt;/code&gt; directory in the container to the directory of the project on the host.&lt;/li&gt;
&lt;li&gt;If there's a &lt;code&gt;node_modules&lt;/code&gt; directory for the project on the host, it obliterates (or replaces, depending on how you feel about this) the &lt;code&gt;/app/node_modules&lt;/code&gt; directory in the container.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Symptoms of this gotcha include weird dependency version mismatches, missing packages, and exclamations such as 'I updated a dependency but the [redacted] container won't install it!'&lt;/p&gt;

&lt;p&gt;If you want to blame something, blame Node for keeping app dependencies in the same directory as the project. There are advantages to this, but there are also disadvantages -- and we're experiencing a big one right here.&lt;/p&gt;

&lt;p&gt;A way to fix this is to have your Docker image hold its &lt;code&gt;node_modules&lt;/code&gt; in a different directory. This is easier said than done; as long as your host has a &lt;code&gt;node_modules&lt;/code&gt; directory that gets synced into &lt;code&gt;/app/node_modules&lt;/code&gt;, Node will always prefer it since it's proximate.&lt;/p&gt;

&lt;p&gt;The way I've accounted for this preference is to force &lt;code&gt;/app/node_modules&lt;/code&gt; to be empty, regardless of what the host's &lt;code&gt;node_modules&lt;/code&gt; directory contains.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;If you're like me, you skipped all the above explanation and scrolled down to this heading. Without further ado, you can fix this issue by updating your project's files with the changes I provide below.&lt;/p&gt;

&lt;p&gt;Required tools (for my fix; possibly my fix could be adapted to use &lt;code&gt;npm&lt;/code&gt;, etc.):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;yarn&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Docker Compose&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You hold your app code in &lt;code&gt;/app&lt;/code&gt; in the container&lt;/li&gt;
&lt;li&gt;Your &lt;code&gt;docker-compose.yml&lt;/code&gt;, &lt;code&gt;Dockerfile&lt;/code&gt;, and &lt;code&gt;package.json&lt;/code&gt; hold all the other things you need to run your app&lt;/li&gt;
&lt;li&gt;You'll use whichever Node Docker base image you want; I just include my current preference as an example.&lt;/li&gt;
&lt;li&gt;Your &lt;code&gt;Dockerfile&lt;/code&gt; has its own bespoke &lt;code&gt;CMD&lt;/code&gt; or &lt;code&gt;ENTRYPOINT&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  docker-compose-yml
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.8"&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-service&lt;/span&gt;
    &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-service&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;  &lt;span class="c1"&gt;# The Dockerfile is in the current directory&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.:/app&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;/app/node_modules&lt;/span&gt;  &lt;span class="c1"&gt;# Ensure `/app/node_modules` is always empty in the container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final line (&lt;code&gt;- /app/node_modules ...&lt;/code&gt;) is the most important. It ensures that &lt;code&gt;/app/node_modules&lt;/code&gt; is always empty in the container, so that Node ignores it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:15.3-alpine3.12&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /  # Important for installing node packages&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json package.json&lt;/span&gt;
&lt;span class="c"&gt;# Install Node dependencies at `/node_modules` in the container.&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-x&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--modules-folder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/node_modules
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PATH=/node_modules/.bin:$PATH&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["/app/scripts/run.sh"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;Now when your container starts up, your app will use &lt;code&gt;/node_modules&lt;/code&gt; to find dependencies.&lt;/p&gt;

&lt;p&gt;You can also run &lt;code&gt;yarn install&lt;/code&gt; and do whatever you want with your &lt;code&gt;node_modules&lt;/code&gt; on the host without worrying about polluting the container's environment. The &lt;code&gt;/app/node_modules&lt;/code&gt; directory in the container will remain empty.&lt;/p&gt;

&lt;p&gt;I've tested this with a Next.js project and it worked great. I hope it works for you. Happy programming! &lt;/p&gt;

</description>
      <category>node</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
