主机网络上的Nginx Docker容器不提供Angular应用

时间:2018-10-23 15:41:01

标签: angular docker nginx docker-compose

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

这是我的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不提供应用程序吗?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

似乎我正确编写了Dockerfile,nginx.conf和docker-compose.yml。 问题出在无法编译的角度应用程序中,因此 RUN npm run build失败。

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

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