【问题标题】:How to get ng serve to work with host name on a VirtualBox Docker set up如何让 ng serve 在 VirtualBox Docker 设置上使用主机名
【发布时间】:2017-06-29 21:21:19
【问题描述】:

我的设置:

Windows 10 运行虚拟机,Ubuntu 16.04 运行 docker 容器,@angular/CLI:1.1.3 节点:6.11.0 从中调用 ng serve 以运行 Angular 4 应用程序

在我的 Windows 机器主机文件中,我的主机名 local.angular 指向 Vbox 的 192.168.56.101,我的虚拟机网络设置是“仅主机适配器”

当运行 ng serve 时,它​​会声明 NG Live Development Server 正在监听 localhost:4200,在 http://localhost:4200 上打开浏览器

但是在我的浏览器上 localhost:4200 不能访问 docker 的 localhost

我尝试过 ng serve --host 0.0.0.0 和其他解决方案,但都不起作用

【问题讨论】:

    标签: angularjs windows docker virtualbox angular-cli


    【解决方案1】:

    通过添加 "host": "[ip address]" 来配置 angular.cli.json "default" 配置,其中 ip 地址是 docker 容器的 'localhost'。

    这通常是 127.0.0.1,但是通过 docker inspect [container-id] 命令或从容器 ifconfig(apt-get net-tools在 ubuntu 上)现在使用 ng serve 并在 172.17.0.2:4200 打开浏览器会显示无效的主机头。添加标志 --disable-host-check 像 "ng server --disable-host-check" 显示页面

    【讨论】:

    • 在我的情况下,它在我明确传递容器 IP 之后才起作用,即ng serve --host 172.22.0.2。所以我对任何配置文件都没有任何更改。
    • 我需要从环境变量中动态更改它。 @alex_edev 这不起作用它说--host is not registered with the serve command
    • 确实如此(刚刚在 Angular CLI:1.7.3 上尝试过)。提交 ng serve --help 以了解有关 ng serve 命令的 --host 标志的更多信息。
    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2015-04-26
    • 2016-10-12
    • 2020-04-24
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多