Docker多阶段构建无法识别已安装的应用程序

时间:2018-07-26 21:46:16

标签: docker dockerfile docker-for-mac docker-multi-stage-build

FROM some-build:latest as build

COPY / /var/www/html

WORKDIR /var/www/html
RUN cd /var/www/html && 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 && \
    npm install && \
    npm run production

ENTRYPOINT ["/bin/init_container.sh"]

映像run包含已安装的npm。尽管如此,npm install返回错误:/bin/sh: 1: npm: not found

这怎么可能?我在做什么错了?


编辑:

作为对@BMitch注释的答复,当我运行RUN映像时,在容器中,该节点位于PATH上,可以使用它。路径为/root/local/bin。我已经附加了所有Dockerfile。

我有3个docker文件:
APP
您已经见过的那个。

RUN

FROM php:7.2.5-apache
MAINTAINER Azure App Services Container Images <appsvc-images@microsoft.com>

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 \
    && 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 \
    && chmod 755 /bin/init_container.sh \
    && echo "root:Docker!" | chpasswd \
    && echo "cd /home" >> /etc/bash.bashrc \
    && ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \
    && ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so \
    && ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h \
    && rm -rf /var/lib/apt/lists/* \
    && pecl install imagick-beta \
    && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
    && docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
    && docker-php-ext-configure mysqli --with-mysqli=mysqlnd \
    && docker-php-ext-install gd \
     mysqli \
     opcache \
     pdo \
     pdo_mysql \
     pdo_pgsql \
     pgsql \
     ldap \
     intl \
     gmp \
     zip \
     bcmath \
     mbstring \
     pcntl \
     xml \
     xmlrpc \
    && docker-php-ext-enable imagick

###################
# Installing node #
###################

RUN apt-get update -yq && apt-get upgrade -yq && \
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" && \
    echo prefix = ~/$MODULES >> ~/.npmrc && \
    echo "export PATH=\$HOME/$MODULES/bin:\$PATH" >> ~/.bashrc && \
    . ~/.bashrc && \
    mkdir ~/$MODULES && \
\
# install Node.js and npm
# https://gist.github.com/isaacs/579814#file-node-and-npm-in-30-seconds-sh
    mkdir ~/node-latest-install && cd $_ && \
    curl http://nodejs.org/dist/v8.11.3/node-v8.11.3.tar.gz | tar xz --strip-components=1 && \
    ./configure --prefix=~/$MODULES && \
    make install && \
    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/* \
   && rmdir /var/lock/apache2 \
   && rmdir /var/run/apache2 \
   && rmdir /var/log/apache2 \
   && chmod 777 /var/log \
   && chmod 777 /var/run \
   && chmod 777 /var/lock \
   && chmod 777 /bin/init_container.sh \
   && cp /bin/apache2.conf /etc/apache2/apache2.conf \
   && rm -rf /var/www/html \
   && rm -rf /var/log/apache2 \
   && mkdir -p /home/LogFiles \
   && 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'; \
    } > /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'; \
    } > /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"]

已构建

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 && \
apt-get install git zip unzip -y

编辑2:
重要的是,如果我删除了RUN npm...命令,那么整个构建都是成功的,并且结果图像包含npm并且我可以使用它(我已经以交互模式使用容器进行了验证)。


编辑3:
这里有很多更简单的解决方案,可以立即尝试:

FROM alpine as img1
RUN echo "$HOME" > $HOME/test.txt

FROM alpine as img2
RUN cat $HOME/test.txt

结果是:cat: can't open '/root/test.txt': No such file or directory

1 个答案:

答案 0 :(得分:2)

这里有两个问题。 “ php:7.2.5-apache”映像在路径中将没有/ root / local / bin,并且在构建期间未将其添加到路径中。当您以交互方式登录时,npm命令将起作用,这可能是由于对设置环境的Shell登录脚本进行了一些更改。在运行任何npm命令之前,您需要运行这些环境设置脚本,并且这些操作必须在同一RUN命令中完成。为了验证自己,您可以检查.bashrc中是否存在运行npm环境的变量或命令。而且,您可以通过在交互式外壳程序和构建中将PATH值与env命令进行比较来验证环境是否不同,如果这是您的问题,则应该看到两个不同的输出。当我运行您的一部分运行图像时,在.bashrc中看到以下内容:

export PATH=$HOME/local/bin:$PATH

因此,您需要更新Dockerfile中的运行映像行:

ENV PATH /root/local/bin:${PATH}:/home/site/wwwroot

根据您的修改3,这是一个完全不同的问题。您在一个新映像中创建了一个文件,然后返回到该文件不存在的基础映像。如果要在多阶段构建中查看文件,则需要在各阶段之间复制文件,或者将上一个图像用作“来源”。

FROM alpine as img1
RUN echo "$HOME" > $HOME/test.txt

FROM alpine as img2
COPY --from=img1 /root/test.txt /root/test.txt
RUN cat $HOME/test.txt

FROM alpine as img1
RUN echo "$HOME" > $HOME/test.txt

FROM img1 as img2
RUN cat $HOME/test.txt