Docker 多阶段构建上的 COPY 不起作用
Docker COPY on multistage build doens't working
我正在使用多阶段构建来编译 React 应用程序。
我主要使用基本节点图像,在使用 nginx 图像暴露到端口 80 之后。
我正在尝试将构建文件从构建器阶段复制到 /usr/share/nginx/html 文件夹,但什么也得不到。
单独构建图像它正在工作,使用 docker-compose 什么都不做。
我试图在 docker 存储库上打开一个问题但已关闭,因此当 podman(使用 moby 引擎)运行良好时,在 moby 引擎上打开也不是一个选项,除了 compose产生一个命令而不是 sudoers 并且构建在节点图像下载时失败。
FROM node:slim as build
WORKDIR /usr/app
COPY package.json .
COPY public public
COPY src src
RUN npm install
RUN npm run build
COPY build .
RUN rm -rf src
RUN rm -rf build
FROM nginx:latest
COPY --from=build /usr/app /usr/share/nginx/html
# RUN chown www-data:www-data ./usr/share/nginx/html/*
EXPOSE 80
在 ls 命令上我看到了构建,但是当我尝试连接到本地主机时出现了禁止的错误,当我尝试访问 index.html 时我得到了 404。
我使用 bash 进入 html 文件夹并使用 namei -l 进行验证。而且根本没有文件。
我做错了什么?我已经搜索了很多问题,但没有得到答案或任何方向,除了添加要复制的文件夹的斜线之外。
目前我正在使用 RHEL。因此,需要 运行 docker 命令的 sudoers 权限。也许这是问题所在?在 podman-compose 上,我需要生成一个 sudo docker-compose build 命令,它 运行 另一个非 root 用户的构建命令。
因此,这不会发生在 docker-ce。
首先,确保文件 nginx.conf
中的 nginx 配置正确。例如:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
其次,没有必要运行这些行:
COPY build . # you should call directly the build folder, because there isn't any other files of build stage
RUN rm -rf src #after finish build step, this image will no longer exist.
RUN rm -rf build
RUN ls
最后你的 Dockerfile 应该是这样的
FROM node:slim as build
WORKDIR /usr/app
COPY package.json .
COPY public public
COPY src src
RUN npm install
RUN npm run build
FROM nginx:latest
COPY --from=build /usr/app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d
# if you are using react router
# you need to overwrite the default nginx configurations
# remove default nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 80
我正在使用多阶段构建来编译 React 应用程序。 我主要使用基本节点图像,在使用 nginx 图像暴露到端口 80 之后。
我正在尝试将构建文件从构建器阶段复制到 /usr/share/nginx/html 文件夹,但什么也得不到。
单独构建图像它正在工作,使用 docker-compose 什么都不做。
我试图在 docker 存储库上打开一个问题但已关闭,因此当 podman(使用 moby 引擎)运行良好时,在 moby 引擎上打开也不是一个选项,除了 compose产生一个命令而不是 sudoers 并且构建在节点图像下载时失败。
FROM node:slim as build
WORKDIR /usr/app
COPY package.json .
COPY public public
COPY src src
RUN npm install
RUN npm run build
COPY build .
RUN rm -rf src
RUN rm -rf build
FROM nginx:latest
COPY --from=build /usr/app /usr/share/nginx/html
# RUN chown www-data:www-data ./usr/share/nginx/html/*
EXPOSE 80
在 ls 命令上我看到了构建,但是当我尝试连接到本地主机时出现了禁止的错误,当我尝试访问 index.html 时我得到了 404。
我使用 bash 进入 html 文件夹并使用 namei -l 进行验证。而且根本没有文件。
我做错了什么?我已经搜索了很多问题,但没有得到答案或任何方向,除了添加要复制的文件夹的斜线之外。
目前我正在使用 RHEL。因此,需要 运行 docker 命令的 sudoers 权限。也许这是问题所在?在 podman-compose 上,我需要生成一个 sudo docker-compose build 命令,它 运行 另一个非 root 用户的构建命令。
因此,这不会发生在 docker-ce。
首先,确保文件 nginx.conf
中的 nginx 配置正确。例如:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
其次,没有必要运行这些行:
COPY build . # you should call directly the build folder, because there isn't any other files of build stage
RUN rm -rf src #after finish build step, this image will no longer exist.
RUN rm -rf build
RUN ls
最后你的 Dockerfile 应该是这样的
FROM node:slim as build
WORKDIR /usr/app
COPY package.json .
COPY public public
COPY src src
RUN npm install
RUN npm run build
FROM nginx:latest
COPY --from=build /usr/app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d
# if you are using react router
# you need to overwrite the default nginx configurations
# remove default nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 80