Docker 和 Angular 生成阶段 `file does not exist` 错误在构建阶段的COPY命令

huangapple go评论71阅读模式
英文:

Docker and Angular prod `file does not exist` Error on stage build COPY command

问题

我不明白为什么Docker无法在容器中获取我的Angular构建文件夹。
你能看到以帮助我吗?

如果我使用Docker Compose命令构建,我会遇到这个错误。
以下是构建我的镜像并启动容器直到出错的所有步骤。

警告:您正在使用的Docker引擎正在以Swarm模式运行。

Compose不使用Swarm模式将服务部署到Swarm中的多个节点。所有容器将安排在当前节点上。

要在整个Swarm中部署应用程序,请使用`docker stack deploy`
构建 linking-front
将构建上下文发送到Docker守护程序 425.6MB
步骤1/9:FROM node:16.19.0 AS build
---> b22f8aab05da
步骤2/9:WORKDIR /usr/src/app
---> 正在使用缓存
---> 5e2431455b65
步骤3/9:COPY package.json package-lock.json ./
---> 正在使用缓存
---> 11d677269b0e
步骤4/9:RUN npm install
---> 正在使用缓存
---> b5544be9159b
步骤5/9:COPY . .
---> 正在使用缓存
---> 3403bfda57ca
步骤6/9:RUN npm run build
---> 正在使用缓存
---> ae8e7960ac33
步骤7/9:FROM nginx:1.23.3-alpine
---> 2bc7edbc3cf2
步骤8/9:COPY nginx.conf /etc/nginx/nginx.conf
---> 正在使用缓存
---> beca38c7be94
步骤9/9:COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
COPY 失败:stat usr/src/app/dist/linkingEducationSecurity-front: 文件不存在
错误:服务'linking-front'构建失败:构建失败
### 阶段1:构建 ###
FROM node:16.19.0 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build

### 阶段2:运行 ###
FROM nginx:1.23.3-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html

我还使用了Docker Compose。

version: '3.9'

services:

  linking-front:
    build: ./linkingEducationSecurity-front/
    ports:
      - "8080:80"
    volumes:
      - type: bind
        source: ./linkingEducationSecurity-front/src/
        target: /app/src
英文:

i do not understand why docker cannot get my angular build folder in container.
Can you see that to help me?

Docker 和 Angular 生成阶段 `file does not exist` 错误在构建阶段的COPY命令

If i build with docker compose command i have this error.
Below are all the steps to build my image and launch my container until the error.

WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Building linking-front
Sending build context to Docker daemon  425.6MB
Step 1/9 : FROM node:16.19.0 AS build
 ---> b22f8aab05da
Step 2/9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 5e2431455b65
Step 3/9 : COPY package.json package-lock.json ./
 ---> Using cache
 ---> 11d677269b0e
Step 4/9 : RUN npm install
 ---> Using cache
 ---> b5544be9159b
Step 5/9 : COPY . .
 ---> Using cache
 ---> 3403bfda57ca
Step 6/9 : RUN npm run build
 ---> Using cache
 ---> ae8e7960ac33
Step 7/9 : FROM nginx:1.23.3-alpine
 ---> 2bc7edbc3cf2
Step 8/9 : COPY nginx.conf /etc/nginx/nginx.conf
 ---> Using cache
 ---> beca38c7be94
Step 9/9 : COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
COPY failed: stat usr/src/app/dist/linkingEducationSecurity-front: file does not exist
ERROR: Service 'linking-front' failed to build : Build failed
### STAGE 1: Build ###
FROM node:16.19.0 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build

### STAGE 2: Run ###
FROM nginx:1.23.3-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html

I use also docker-compose

version: '3.9'

services:

  linking-front:
    build: ./linkingEducationSecurity-front/
    ports:
      - "8080:80"
    volumes:
      - type: bind
        source: ./linkingEducationSecurity-front/src/
        target: /app/src

答案1

得分: 1

由于您在评论中尝试执行 RUN cd dist && ls,导致您得到了以下输出:

Step 7/10 : RUN cd dist && ls  ---> Running in e8f002e82f3a linking-education-security-front

步骤和 Dockerfile 部分是完美的。从构建文件夹中复制的 COPY 命令拼写错误。

请将这行代码更新为:

COPY --from=build /usr/src/app/dist/linking-education-security-front /usr/share/nginx/html

尝试重新构建,这可能会起作用。

英文:

since in the comments you tried to do RUN cd dist && ls which gave you this output :

Step 7/10 : RUN cd dist && ls  ---> Running in e8f002e82f3a linking-education-security-front

The steps and dockerfile are perfect. the COPY command from build folder is missing its spell

update this line :

COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html

to this :

COPY --from=build /usr/src/app/dist/linking-education-security-front /usr/share/nginx/html

and try rebuilding , this might work.

huangapple
  • 本文由 发表于 2023年2月18日 07:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定