Docker构建:来自下一个层的内容是什么?

时间:2017-01-26 17:07:34

标签: docker dockerfile

我正在尝试Dockerize PyroCMS,这就是我的Dockerfile的样子:

FROM reynierpm/docker-centos7-supervisord:latest
ENV TERM=xterm \
    PATH="/root/.composer/vendor/bin:${PATH}" \
    COMPOSER_ALLOW_SUPERUSER=1 \
    COMPOSER_ALLOW_XDEBUG=1 \
    COMPOSER_DISABLE_XDEBUG_WARN=1 \
    COMPOSER_HOME="/root/.composer" \
    COMPOSER_CACHE_DIR="/root/.composer/cache"

RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
                   https://rpms.remirepo.net/enterprise/remi-release-7.rpm
RUN yum install -y  \
        yum-utils \
        git \
        zip \
        unzip \
        wget \
        nano \
        php71-php-fpm \
        php71-php-cli \
        php71-php-common \
        php71-php-gd \
        php71-php-intl \
        php71-php-json \
        php71-php-mbstring \
        php71-php-mcrypt \
        php71-php-mysqlnd \
        php71-php-pdo \
        php71-php-pear \
        php71-php-xml \
        php71-php-pecl-xdebug \
        php71-pecl-apcu \
        php71-php-pecl-apfd \
        php71-php-pecl-memcache \
        php71-php-pecl-memcached \
        php71-php-pecl-mongodb \
        php71-php-pecl-redis \
        php71-php-pecl-request \
        php71-php-pecl-uploadprogress \
        php71-php-pecl-xattr \
        php71-php-pecl-zip && \
        yum clean all && rm -rf /tmp/yum*
RUN ln -sfF /opt/remi/php71/enable /etc/profile.d/php71-paths.sh && \
    ln -sfF /opt/remi/php71/root/usr/bin/{pear,pecl,phar,php,php-cgi,phpize} /usr/local/bin/. && \
    mv -f /etc/opt/remi/php71/php.ini /etc/php.ini && \
    ln -sfF /etc/php.ini /etc/opt/remi/php71/php.ini && \
    rm -rf /etc/php.d && \
    mv /etc/opt/remi/php71/php.d /etc/. && \
    ln -sfF /etc/php.d /etc/opt/remi/php71/php.d

RUN /bin/sh -c "mkdir -p /data/www" && \
    /bin/sh -c "mkdir -p /data/php/{tmp,sessions,xdebug,logs,uploads}" && \
    rm -rf /data/www/*
COPY container-files /
WORKDIR /data/www
RUN sh /usr/local/bin/composer-install.sh
RUN git clone --branch 3.2 https://github.com/pyrocms/pyrocms.git .
COPY .env .
RUN pwd && ls -la
RUN php artisan install --ready
RUN chmod +x /config/bootstrap.sh
EXPOSE 9005

这是一个在制品,所以不要为优化等问题而烦恼,因为此时我并不关心它。

为了构建我正在运行的图像docker build -t reynierpm/pyrocms .,它会顺利进行到达此步骤:

RUN php artisan install --ready

失败时出现以下错误:

Step 14/16 : RUN php artisan install --ready
 ---> Running in ffbddd585ef1
Could not open input file: artisan
The command '/bin/sh -c php artisan install --ready' returned a non-zero code: 1

我不确定我做错了什么,但这是我到目前为止所做的:

  • WORKDIR /data/www将从/data/www开始我的所有工作,除非我将其更改为另一个我在任何时候都没有做的目录
  • RUN git clone --branch 3.2 https://github.com/pyrocms/pyrocms.git .正确克隆了存储库分支,因为在命令显示以下输出后立即执行ls -la

    drwxr-xr-x 16 root root 4096 Jan 26 16:50 .
    drwxr-xr-x  3 root root 4096 Jan 26 16:50 ..
    drwxr-xr-x  8 root root 4096 Jan 26 16:50 .git
    -rw-r--r--  1 root root   12 Jan 26 16:50 .gitattributes
    -rw-r--r--  1 root root   97 Jan 26 16:50 .gitignore
    -rw-r--r--  1 root root 2254 Jan 26 16:50 CONTRIBUTING.md
    -rwxr-xr-x  1 root root  556 Jan 26 16:50 Gulpfile.js
    -rw-r--r--  1 root root 1088 Jan 26 16:50 LICENSE.md
    -rw-r--r--  1 root root  236 Jan 26 16:50 README.md
    drwxr-xr-x  2 root root 4096 Jan 26 16:50 addons
    drwxr-xr-x  3 root root 4096 Jan 26 16:50 app
    -rwxr-xr-x  1 root root 1646 Jan 26 16:50 artisan
    drwxr-xr-x  3 root root 4096 Jan 26 16:50 bootstrap
    -rw-r--r--  1 root root 1525 Jan 26 16:50 build.xml
    -rw-r--r--  1 root root 3708 Jan 26 16:50 composer.json
    drwxr-xr-x  2 root root 4096 Jan 26 16:50 config
    drwxr-xr-x  2 root root 4096 Jan 26 16:50 core
    drwxr-xr-x  5 root root 4096 Jan 26 16:50 database
    drwxr-xr-x  3 root root 4096 Jan 26 16:50 docs
    -rwxr-xr-x  1 root root  400 Jan 26 16:50 package.json
    -rwxr-xr-x  1 root root 1397 Jan 26 16:50 phpunit.xml
    drwxr-xr-x  5 root root 4096 Jan 26 16:50 public
    drwxr-xr-x  6 root root 4096 Jan 26 16:50 resources
    drwxr-xr-x  2 root root 4096 Jan 26 16:50 routes
    -rwxr-xr-x  1 root root  560 Jan 26 16:50 server.php
    drwxr-xr-x  8 root root 4096 Jan 26 16:50 storage
    drwxr-xr-x  2 root root 4096 Jan 26 16:50 tests
    drwxr-xr-x  3 root root 4096 Jan 26 16:50 tmp
    
  • COPY .env .会将文件.env复制到/data/www(但如果语法错误已经尝试COPY .env /data/www/.env

在执行上述步骤后,我应该最终获得克隆内容以及.dev的内容,但这只是奇怪的,因为我最终只得到了这个:

total 12
drwxr-xr-x 2 root root 4096 Jan 26 16:54 .
drwxr-xr-x 3 root root 4096 Jan 26 16:54 ..
-rw-rw-r-- 1 root root  337 Jan 26 15:39 .env

当然,这会使构建失败,因为文件artisan不再存在。

为什么呢?不应该在文件之间保留文件吗?有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我刚刚在我的计算机上提取了reynierpm/docker-centos7-supervisord:latest图片,该图片中的/data位置就是一个卷。

$ docker inspect reynierpm/docker-centos7-supervisord:latest
...
        "Volumes": {
            "/data": {}
        },
...

构建中的每个步骤都作为容器运行。由于/data是图像指定的卷,因此每个步骤在/ data处都有一个新的空白匿名卷。构建步骤完成后,对该容器的写时复制系统的更改将作为下一层提交。由于对/data的任何更改都在卷中而不在COW文件系统中,因此它们不包含在提交中。

要解决此问题,请确保将文件添加或复制到不是您正在使用的基本影像中的卷的位置。