title: Install nodejs and npm in Dockerfile
body: <h1>The context</h1>
  
  <p>I have a Dockerfile to create an image that contains an apache webserver. However I also want to build my website using the Dockerfile so that the build process isn't dependent on the developers local environment. Note that the docker container is only going to be used for local development not for production.</p>
  
  <h1>The problem</h1>
  
  <p>I have this Dockerfile:</p>
  
  <pre><code>FROM httpd
  RUN apt-get update -yq
  RUN apt-get -yq install curl gnupg
  RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
  RUN apt-get update -yq
  RUN apt-get install -yq \
          dh-autoreconf=19 \
          ruby=1:2.5.* \
          ruby-dev=1:2.5.* \
          nodejs
  </code></pre>
  
  <p>I build it:</p>
  
  <pre><code>sudo docker build --no-cache .
  </code></pre>
  
  <p>The build completes successfully, here is part of the output:</p>
  
  <pre><code>Step 9/15 : RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
   ---&gt; Running in e6c747221ac0
  ......
  ......
  ......
  Removing intermediate container 5a07dd0b1e01
   ---&gt; 6279003c1e80
  Successfully built 6279003c1e80
  </code></pre>
  
  <p>However, when I run the image in a container using this:</p>
  
  <pre><code>sudo docker container run --rm -it --name=debug 6279003c1e80 /bin/bash
  </code></pre>
  
  <p>Then when doing <code>apt-cache policy</code> inside the container, it doesn't show the repository that should have been added with the curl command. Also when doing <code>apt-cache policy nodejs</code> it shows the old version is installed. </p>
  
  <p>However when I then run the following inside the container:</p>
  
  <pre><code>curl -sL https://deb.nodesource.com/setup_12.x | bash
  apt-cache policy
  apt-cache policy nodejs
  </code></pre>
  
  <p>It shows me the repository is added and it shows the newer nodejs version is available.</p>
  
  <p>So why is it that when using the curl command using <code>RUN</code> inside the docker file it doesn't seem to work, but when doing it manually in the container from a shell then it does work? And how can I get around this problem?</p>
  
  <h1>Updates</h1>
  
  <ul>
  <li>Note that to prevent caching issues I am using the --no-cache flag.</li>
  <li>I also removed all containers and did <code>sudo docker system prune</code> and rebuild the image but without success.</li>
  <li>I tried bundling everything in one RUN command as user "hmm" suggested (as this is best practice for apt commands):</li>
  </ul>
  
  <pre><code>RUN apt-get update -yq \
      &amp;&amp; apt-get -yq install curl gnupg &amp;&amp; \
      &amp;&amp; curl -sL https://deb.nodesource.com/setup_12.x | bash \
      &amp;&amp; apt-get update -yq \
      &amp;&amp; apt-get install -yq \
          dh-autoreconf=19 \
          ruby=1:2.5.* \
          ruby-dev=1:2.5.* \
          nodejs \
      &amp;&amp; rm -rf /var/lib/apt/lists/*
  </code></pre>