angular - Getting Forbidden 403 error with nginx docker container when serving static files
问题描述
I am trying to run my angular-cli project with nginx docker container in development mode. Here is the complete boilerplate code.
To avoid every time rebuilding the complete docker image whenever the source code is changed, I am using volumes in my docker compose file and mapping my dist/{project-name} path with container's usr/share/nginx/html path, and then finally running the Angular project in host machine with ng build --watch
But using volumes seems to be causing file read issues to the nginx and it is unable to read index.html
dev.dockerfile:
FROM nginx:alpine
LABEL author="Saurabh Palatkar"
COPY nginx.conf /etc/nginx/nginx.conf
COPY /dist/ng-docker /usr/share/nginx/html
RUN ls /usr/share/nginx/html
RUN ps aux | grep nginx
RUN chown -R root:root /usr/share/nginx/html/index.html
RUN chmod -R 755 /usr/share/nginx/html
RUN ls -al /usr
RUN chmod o+x /usr
RUN chmod o+x /usr/share
RUN chmod o+x /usr/share/nginx
RUN chmod o+x /usr/share/nginx/html
# RUN ls /usr/share/nginx/html
# RUN ls /etc/nginx
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
docker-compose.dev.yml:
version: "2.0"
services:
angular-nginx-docker:
image: ng-docker-dev
build:
context: .
dockerfile: .docker/dev.dockerfile
environment:
NODE_ENV: development
volumes:
- "/dist/ng-docker:/usr/share/nginx/html"
ports:
- 8080:80
command: [nginx-debug, '-g', 'daemon off;']
However, I am able to serve my Angular static files through nginx docker container in prod mode (The only difference I see is, I am not using volumes) docker-compose.yml:
version: "2.0"
services:
angular-nginx-docker:
image: ng-docker-prod
build:
context: .
dockerfile: .docker/prod.dockerfile
environment:
NODE_ENV: production
ports:
- 80:80
- 443:443
- 9229:9229
command: [nginx, '-g', 'daemon off;']
prod.dockerfile:
FROM node:latest as angular-built
WORKDIR /usr/src/app
RUN npm i -g @angular/cli
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install --silent
COPY . .
RUN ng build --prod --build-optimizer
FROM nginx:alpine
LABEL author="Saurabh Palatkar"
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=angular-built /usr/src/app/dist/ng-docker /usr/share/nginx/html
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
解决方案
使用volumes时,docker会使用宿主系统文件权限模式。我怀疑,您主机上的静态文件(您映射的)没有root读取的权限。
在 dockerfile 中执行chmod
不会执行任何操作,因为在构建期间文件不在其中(卷在容器运行时映射)。
我建议chmod -R o+rx /dist/ng-docker
在主机上运行,以便每个用户(包括 root 用户)都可以阅读它们。如果您不希望这样做,请尝试专门为 root (uid: 0) 设置 ACL,或修改容器以拥有一个用户,该用户将运行 nginx,与uid
您一样guid
。
推荐阅读
- java - 以下哪一项是在 java 中调用用户定义类中存在的方法的更好的编程实践?
- flutter - 滚动时颤动水平列表视图点指示器
- c++ - 当我尝试在 C++ 中对谷物进行序列化时,我遇到了一个奇怪的错误
- java - 如何在 SpringBootTest 中加载不同的 application.properties 文件
- python - 一个 for 循环和 while 循环的问题。结果不是我所期望的
- python - 如何在 python pandas 中显示销售数据的每月变动?
- arduino - 如何阻止 if 语句重复?
- java - Eclipse 选项卡标题未正确显示
- node.js - 如何优雅地结束 Node 中的 HTTP2 通信
- node.js - 错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379 Heroku