title: Docker multistage build doesn't recognise installed application
body: <pre><code>FROM some-build:latest as build
  
  COPY / /var/www/html
  
  WORKDIR /var/www/html
  RUN cd /var/www/html &amp;&amp; composer install
  
  FROM some-build2:latest as run
  
  COPY --from=build /var/www/html /var/www/html
  
  ENV PATH ${HOME}/local/bin:${PATH}:/home/site/wwwroot
  
  RUN cd /var/www/html &amp;&amp; \
      npm install &amp;&amp; \
      npm run production
  
  ENTRYPOINT ["/bin/init_container.sh"]
  </code></pre>
  
  <p>The image <code>run</code> contains an installed <code>npm</code>. Despite this fact, the <code>npm install</code> return the error: <code>/bin/sh: 1: npm: not found</code></p>
  
  <p>How is this possible? What am I doing wrong?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>As answer to @BMitch 's comment, when I run the <code>RUN</code> image, in the container the node is on the PATH and I can use it. The path is <code>/root/local/bin</code>. I've attached all the Dockerfiles.</p>
  
  <p>I have 3 docker files:<br>
  <strong><em>APP</em></strong><br>
  The one you've already seen before.</p>
  
  <p><strong><em>RUN</em></strong></p>
  
  <pre><code>FROM php:7.2.5-apache
  MAINTAINER Azure App Services Container Images &lt;appsvc-images@microsoft.com&gt;
  
  COPY apache2.conf /bin/
  COPY init_container.sh /bin/
  
  RUN a2enmod rewrite expires include deflate
  
  # install the PHP extensions we need
  RUN apt-get update \
      &amp;&amp; apt-get install -y --no-install-recommends \
       libpng-dev \
       libjpeg-dev \
       libpq-dev \
       libldap2-dev \
       libldb-dev \
       libicu-dev \
       libgmp-dev \
       mysql-client \
       libmagickwand-dev \
       openssh-server vim curl wget tcptraceroute \
      &amp;&amp; chmod 755 /bin/init_container.sh \
      &amp;&amp; echo "root:Docker!" | chpasswd \
      &amp;&amp; echo "cd /home" &gt;&gt; /etc/bash.bashrc \
      &amp;&amp; ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \
      &amp;&amp; ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so \
      &amp;&amp; ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h \
      &amp;&amp; rm -rf /var/lib/apt/lists/* \
      &amp;&amp; pecl install imagick-beta \
      &amp;&amp; docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
      &amp;&amp; docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
      &amp;&amp; docker-php-ext-configure mysqli --with-mysqli=mysqlnd \
      &amp;&amp; docker-php-ext-install gd \
       mysqli \
       opcache \
       pdo \
       pdo_mysql \
       pdo_pgsql \
       pgsql \
       ldap \
       intl \
       gmp \
       zip \
       bcmath \
       mbstring \
       pcntl \
       xml \
       xmlrpc \
      &amp;&amp; docker-php-ext-enable imagick
  
  ###################
  # Installing node #
  ###################
  
  RUN apt-get update -yq &amp;&amp; apt-get upgrade -yq &amp;&amp; \
  apt-get install -yq g++ libssl-dev apache2-utils curl git python make nano
  
  # setting up npm for global installation without sudo
  # http://stackoverflow.com/a/19379795/580268
  RUN MODULES="local" &amp;&amp; \
      echo prefix = ~/$MODULES &gt;&gt; ~/.npmrc &amp;&amp; \
      echo "export PATH=\$HOME/$MODULES/bin:\$PATH" &gt;&gt; ~/.bashrc &amp;&amp; \
      . ~/.bashrc &amp;&amp; \
      mkdir ~/$MODULES &amp;&amp; \
  \
  # install Node.js and npm
  # https://gist.github.com/isaacs/579814#file-node-and-npm-in-30-seconds-sh
      mkdir ~/node-latest-install &amp;&amp; cd $_ &amp;&amp; \
      curl http://nodejs.org/dist/v8.11.3/node-v8.11.3.tar.gz | tar xz --strip-components=1 &amp;&amp; \
      ./configure --prefix=~/$MODULES &amp;&amp; \
      make install &amp;&amp; \
      curl -L https://www.npmjs.org/install.sh | sh
  
  # optional, check locations and packages are correct
  # RUN which node; node -v; which npm; npm -v; \
  #   npm ls -g --depth=0
  
  # Remove unnecessary packages
  # RUN apt-get -yq purge g++ libssl-dev curl git python make nano
  # RUN apt-get -yq autoremove
  
  ###################
  
  RUN   \
     rm -f /var/log/apache2/* \
     &amp;&amp; rmdir /var/lock/apache2 \
     &amp;&amp; rmdir /var/run/apache2 \
     &amp;&amp; rmdir /var/log/apache2 \
     &amp;&amp; chmod 777 /var/log \
     &amp;&amp; chmod 777 /var/run \
     &amp;&amp; chmod 777 /var/lock \
     &amp;&amp; chmod 777 /bin/init_container.sh \
     &amp;&amp; cp /bin/apache2.conf /etc/apache2/apache2.conf \
     &amp;&amp; rm -rf /var/www/html \
     &amp;&amp; rm -rf /var/log/apache2 \
     &amp;&amp; mkdir -p /home/LogFiles \
     &amp;&amp; ln -s /home/LogFiles /var/log/apache2 
  
  
  RUN { \
          echo 'opcache.memory_consumption=128'; \
          echo 'opcache.interned_strings_buffer=8'; \
          echo 'opcache.max_accelerated_files=4000'; \
          echo 'opcache.revalidate_freq=60'; \
          echo 'opcache.fast_shutdown=1'; \
          echo 'opcache.enable_cli=1'; \
      } &gt; /usr/local/etc/php/conf.d/opcache-recommended.ini
  
  RUN { \
          echo 'error_log=/var/log/apache2/php-error.log'; \
          echo 'display_errors=Off'; \
          echo 'log_errors=On'; \
          echo 'display_startup_errors=Off'; \
          echo 'date.timezone=UTC'; \
      } &gt; /usr/local/etc/php/conf.d/php.ini
  
  
  COPY sshd_config /etc/ssh/
  
  EXPOSE 2222 8080
  
  ENV APACHE_RUN_USER www-data
  ENV PHP_VERSION 7.2.5
  
  ENV PORT 8080
  ENV WEBSITE_ROLE_INSTANCE_ID localRoleInstance
  ENV WEBSITE_INSTANCE_ID localInstance
  ENV PATH ${PATH}:/home/site/wwwroot
  
  ENTRYPOINT ["/bin/init_container.sh"]
  </code></pre>
  
  <p><strong><em>BUILD</em></strong></p>
  
  <pre><code>FROM composer:latest as composer
  
  FROM php:7.2.5-apache as apache
  
  COPY --from=composer /usr/bin/composer /usr/bin/composer
  
  RUN apt-get update &amp;&amp; \
  apt-get install git zip unzip -y
  </code></pre>
  
  <hr>
  
  <p>Edit 2:<br>
  It is important that if I remove the <code>RUN npm...</code> commands, then the whole build is a success and the result image contains the npm and I can use it (I've verified by using a container in interactive mode).</p>
  
  <hr>
  
  <p>Edit 3:<br>
  Here's a lot lot simpler solution that can be tried out instantly:</p>
  
  <pre><code>FROM alpine as img1
  RUN echo "$HOME" &gt; $HOME/test.txt
  
  FROM alpine as img2
  RUN cat $HOME/test.txt
  </code></pre>
  
  <p>The result is: <code>cat: can't open '/root/test.txt': No such file or directory</code></p>