通过 minikube 构建 docker 多阶段图像时出错:找不到文件

Error when building docker multi-stage image through minikube : file not found

我有为 Angular 应用程序编写的这个 Dockerfile :

#### Stage 1: Build the angular application
FROM node:12.4.0-alpine as build

# Configure the main working directory inside the docker image.
# This is the base directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
WORKDIR /app

# Copy the package.json as well as the package-lock.json and install
# the dependencies. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY package.json package-lock.json ./
RUN npm ci
RUN npm install -g @angular/cli

# Copy the main application
COPY . ./

# Build the application
RUN npm run build:prod

#### Stage 2: Serve the Angular application from Nginx
FROM nginx:1.17.0-alpine

RUN rm -rf /usr/share/nginx/html/*

# Copy the angular build from Stage 1
COPY --from=build /app/dist/MyAngularApp /usr/share/nginx/html

# Copy our custom nginx config
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY nginx/status.conf /etc/nginx/conf.d/status.conf

# Expose port 80 to the Docker host, so we can access it
# from the outside.
EXPOSE 80
# Expose port 8081 to the Docker host, for nginx status pages
EXPOSE 8081

ENTRYPOINT ["nginx","-g","daemon off;"]

我可以在我的机器上用 docker build -t my-angular-app:v1 .

构建这个镜像

但是如果我先 eval $(minikube docker-env) 使用 minikube 的 docker 守护进程而不是我的本地守护进程并执行相同的命令:docker build -t my-angular-app:v1 .(按照 README 中的说明)

我收到以下错误(没有这样的文件或目录):

Step 10/15 : COPY --from=build /app/dist/MyAngularApp /usr/share/nginx/html
COPY failed: stat /var/lib/docker/overlay2/2972c4790d70a2e0c98895855e5bde894a97ebab8c5a8b2efab5100c3f8c6fbb/merged/app/dist/MyAngularApp: no such file or directory

确实,VM中没有文件夹/var/lib/docker/overlay2/2972c4...。

我错过了什么?

我正在使用 minikube v1.6.2 和 virtualbox v5.2.34

本地 docker v19.03.5

minikube 的 docker 也是 v19.03.5

FIY minikube docker-env 设置以下环境变量:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/home/myUser/.minikube/certs"

如评论中所述,这是由于在 运行 minikube VM 主机上找不到构建映像所需的文件造成的。

将您的文件获取到 minikube VM 您可以使用命令 minikube ssh,然后使用 scp 或任何其他 bash 命令将文件复制到需要的位置。

好的,我发现了问题:它与我的 Docker 文件或 minikube 中丢失的文件无关。

最后的TLDR。

所以问题是 Docker 没有找到在第一阶段创建的 /app/dist/MyAngularApp 文件夹。

此文件夹应该由命令 npm run build:prod 创建。

用我的机器构建时,它可以工作。通过 minikube 构建时,不会创建该文件夹。没有错误或警告。命令运行...但什么也没有。

有人建议使用 Yarn 而不是 Npm,看看它是否显示某种错误或警告。宾果!

yarn run build:prod 显示:error Command failed with signal "SIGKILL".

哈哈!构建占用过多资源并被终止。

我在 minikube 上增加了内存:

minikube delete;
minikube start --memory 4096

现在可以正常使用了!

我不知道为什么 Yarn 显示错误而 Npm 不显示,即使使用了 --verbose。

TLDR; minikube 上的内存不足导致它发出 SIGKILL 我的 npm build。没有构建,没有结果文件夹,因此出现 COPY 错误。使用 npm 时没有显示错误。切换到 Yarn 并显示 SIGKILL 错误。