首页 > 解决方案 > 通过 minikube 构建 docker 多阶段映像时出错:找不到文件

问题描述

我有这个为 Angular App 编写的 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

本地码头工人 v19.03.5

minikube 的 docker 也是 v19.03.5

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

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

标签: dockerdockerfileminikube

解决方案


好的,我发现了问题:它与我的 Dockerfile 或 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 上的内存不足导致它对我的npm build. 没有构建,没有生成的文件夹,因此出现 COPY 错误。使用 npm 时没有显示错误。切换到 Yarn 并显示 SIGKILL 错误。


推荐阅读