简体   繁体   English

多级构建上的 Docker COPY 不起作用

[英]Docker COPY on multistage build doens't working

I'm using multistage build to compile an react app.我正在使用多阶段构建来编译反应应用程序。 Primarily I use a base node image, after I use a nginx image to expose to the port 80.在我使用 nginx 图像暴露到端口 80 之后,我主要使用基本节点图像。

I'm attempting to COPY the build files from builder stage to the /usr/share/nginx/html folder, but gets nothing.我正在尝试将构建文件从构建器阶段复制到 /usr/share/nginx/html 文件夹,但一无所获。

Building the image alone it's working, with docker-compose does nothing.单独构建图像它正在工作,docker-compose 什么都不做。

I attempted to open an issue on the docker repo but is closed, therefore open on the moby engine also is not a option when the podman(that's use moby engine) works well, except the compose that spawn a command as not sudoers and the build fail on the node image download.我试图在 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

On the ls command I see the build, but when I'm attempt to connect to localhost a get the forbidden error, and when attempt to access the index.html I'm getting the 404.在 ls 命令上,我看到了构建,但是当我尝试连接到 localhost 时,出现了禁止错误,并且当尝试访问 index.html 时,我得到了 404。

I entered on the the html folder using bash and verify using the namei -l.我使用 bash 进入 html 文件夹并使用 namei -l 进行验证。 and there's no file at all.而且根本没有文件。

What could I be doing wrong?我可能做错了什么? I've searched a ton of questions and don't get the answer or any direction, execpt the fact of add a slash of the folder to be copied.我搜索了很多问题,但没有得到答案或任何方向,除了添加要复制的文件夹的斜线这一事实。

Currently I'm using a RHEL.目前我正在使用 RHEL。 So, needs sudoers permission to run docker commands.因此,需要 sudoers 权限才能运行 docker 命令。 Maybe it's the problem?也许是问题所在? On podman-compose I need to spawn an sudo docker-compose build command, and it run another build commands as non root.在 podman-compose 上,我需要生成一个 sudo docker-compose 构建命令,它以非 root 身份运行另一个构建命令。

Therefore, this not occurs on docker-ce.因此,这不会发生在 docker-ce 上。

First of all, ensure you have a correct nginx config in the file nginx.conf .首先,确保在 nginx.conf 文件中有正确的nginx.conf配置。 eg:例如:

server {
listen 80;
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
  }
}

Second, it's not necessary run these lines:其次,没有必要运行这些行:

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

Finally your Dockerfile should look like this最后你的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM