【问题标题】:Nginx docker container on host network don't serve angular app主机网络上的 Nginx docker 容器不提供 Angular 应用程序
【发布时间】:2018-10-23 15:41:01
【问题描述】:

我的目标是使用 nginx 在容器中运行 angular app。 我希望带有应用程序的容器位于我的主机网络中。

这是我的 Dockerfile:

# Stage 1
FROM node:10.11.0-alpine as node

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build

# Stage 2
FROM nginx:1.14-alpine

COPY --from=node /usr/src/app/dist/gui /usr/share/nginx/html

COPY ./nginx.conf /etc/nginx/conf.d/default.conf

nginx.conf:

server {
  listen 80;
  server_name 127.0.0.1;
  location / {
    root /usr/share/nginx/html;
    index index.html index.html;
    try_files $uri $uri/ /index.html =404;
  }

  location /api {
    proxy_pass http://localhost:8080/api;
  }
}

还有我的 docker-compose.yml 文件:

version: "3.5"

services:
  gui:
    container_name: gui
    network_mode: "host"
    build:
        context: gui

$ docker-compose up 启动容器。 但是 localhost:80 上没有任何内容。

问题出在哪里? Nginx 不服务应用?

【问题讨论】:

  • 你有没有在 docker-compose.yml 中映射 80 端口? “什么都没有”是什么意思?
  • 如果使用“主机”模式作为网络模式,则不需要映射任何端口

标签: angular docker nginx docker-compose


【解决方案1】:

【讨论】:

  • 容器启动,但 nginx 仍然不为应用程序服务。这些是我尝试访问localhost:80后的容器日志:gui | 127.0.0.1 - - [24/Oct/2018:08:55:36 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" "-"
    此外,浏览器开发工具显示以下错误:
    ``` ERROR Error: Uncaught (in promise): Error: Runtime compiler is not加载错误:未加载运行时编译器```
【解决方案2】:

看来我正确地编写了 Dockerfile、nginx.conf 和 docker-compose.yml。 问题出在 Angular 应用程序中,无法编译,所以 RUN npm run build 失败了。

使用npm start 在本地运行应用程序并没有发现问题,因为实时重新加载模式处于活动状态。只有完全编译无法完成。

@Ntwobike 感谢您的尝试。命令CMD ["nginx", "-g", "daemon off;"] 也是不必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2019-09-22
    • 1970-01-01
    • 2016-02-11
    • 2016-02-27
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多