2

我不确定这一点,但我注意到在使用通配符复制文件时 docker 层缓存会中断,请参阅以下构建上下文:

在此处输入图像描述

问题是,当修改Kernel.php(添加空格,任何东西......)并docker build再次运行时,“复制作曲家。*”之后的图层每次都会再次运行,即使没有修改作曲家数据......

第一次运行

=> [1/6] FROM docker.io/library/composer               
=> [2/6] RUN set -eux;     composer global require hira
=> [3/6] COPY composer.* ./                            
=> [4/6] RUN set -eux;     composer install         --i
=> [5/6] COPY app/ /app/app/                           
=> [6/6] RUN set -eux;     composer dump-autoload ...

现在编辑Kernel.php并再次运行:

=> [1/6] FROM docker.io/library/composer              
=> CACHED [internal] helper image for file operations 
=> CACHED [2/6] RUN set -eux;     composer global requ
=> [3/6] COPY composer.* ./                           
=> [4/6] RUN set -eux;     composer install         --
=> [5/6] COPY app/ /app/app/                          
=> [6/6] RUN set -eux;     composer dump-autoload  ..

可以通过以下更改来修复此行为:

COPY composer.json ./
COPY composer.lock ./

代替

COPY composer* ./

但困扰我的是为什么会这样?我已经为此浪费了几个小时:/

码头工人版本

➜  composer docker version
Client:
 Version:           19.03.8-ce
 API version:       1.40
 Go version:        go1.14
 Git commit:        afacb8b7f0
 Built:             Mon Mar 16 22:23:09 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          19.03.8-ce
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.14
  Git commit:       afacb8b7f0
  Built:            Mon Mar 16 22:22:53 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.3.m
  GitCommit:        d76c121f76a5fc8a462dc64594aea72fe18e1178.m
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

以下是文件:

作曲家.json

{
    "name": "test/docker_composer_cached",
    "authors": [
        {
            "name": "Kitensei"
        }
    ],
    "require": {
        "moc/math": "dev-master"
    },
    "minimum-stability": "dev"
}

Dockerfile

# syntax=docker/dockerfile:1.0-experimental
FROM composer

COPY composer.* ./

RUN set -eux; \
    composer install \
        --ignore-platform-reqs \
        --no-autoloader \
        --no-dev \
        --no-interaction \
        --no-scripts \
        --no-suggest \
        --no-plugins \
        --no-progress \
        --prefer-dist \
    ; \
    find vendor -name ".git" -type d | xargs rm -rf

COPY app/ /app/app/

RUN set -eux; \
    composer dump-autoload \
        --classmap-authoritative \
        --optimize \
        --no-scripts \
        --no-dev


应用\内核.php

<?php

namespace App;

class Kernel {

}
4

0 回答 0