ng服务在Docker容器中不起作用 [英] ng serve not working in Docker container

查看:170
本文介绍了ng服务在Docker容器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Docker Compose配置,在这里我只需创建一个NodeJS容器并安装Angular内置CLI.

docker-compose up -d之后,我可以使用docker-compose run node bash在容器内进行SSH. ng new可以正常工作,但ng serve似乎不起作用.它已正确启动,控制台中没有错误.但是,如果我访问localhost(并且我将端口4200映射到80),则不会加载任何内容.

我想念什么吗?

解决方案

在您的Dockerfile中,您缺少公开行,例如:

EXPOSE 4200

尝试将其放置在docker文件中的最后一个RUN命令之前.

此行公开了容器本身的端口(在这种情况下为4200),因此来自compose的映射起作用(80:4200).

Compose就是这样做的:将80从主机转发到容器中的4200.但是它并不知道或不在乎4200是否正在被收听. dockerfile中的Expose可确保何时构建映像,以便为将来运行的容器公开此端口,以便您的ng服务可以收听它.


解决方案

因此要使用docker-compose run获得所需的功能,请使用publish发布端口.由于run不使用docker-compose.yml中的映射,因此将忽略它​​们.因此,请像这样使用它:

docker-compose run --publish 80:4200 node bash

然后创建有角度的应用程序,然后按您的方式启动它.


测试示例以供将来参考

cd tmp(或任何可写文件夹)

ng new myProject

cd myProject

ng serve --host 0.0.0.0(-主机0.0.0.0侦听容器中的所有接口)

然后在浏览器中,转到localhost,当端口4200被发布并通过我上面显示的publish命令绑定到主机端口80时,您现在应该看到有角度的欢迎页面.

每次遇到端口转发问题时,如果打开一个新终端,将另一个终端保持在执行原始run command的位置并运行docker ps,您将在端口"列中看到以下内容:

0.0.0.0:80->4200/tcp,这意味着端口80上的主机已成功转发到端口4200上的容器.

如果看到类似4200/tcp而不是->的部分,则表示没有发布任何映射或端口.

I have this Docker Compose configuration where I simply create a NodeJS container and install Angular CLI inside.

After a docker-compose up -d, I'm able to SSH inside the container with docker-compose run node bash. ng new works perfectly but ng serve does not seem to work. It's launched correctly, no error in the console. But, if I visit localhost (ad I mapped port 4200 to 80), nothing loads.

Am I missing something?

解决方案

In your Dockerfile you are missing the Expose line such as:

EXPOSE 4200

Try placing it before your last RUN command in the docker file.

This line exposes the port in the container itself (4200 in this case) so the mapping from compose works (80:4200).

Compose just does this: forward 80 from the host to 4200 in the container. But it doesn't know or care if the 4200 is actually being listened to. The Expose in the dockerfile makes sure when the image is built, to expose this port for the future running containers, so your ng serve can listen to it.


Resolution

So to get what you want with docker-compose run, use publish to publish the ports. As run doesn't use the mappings from your docker-compose.yml, it ignores them. So use it like this:

docker-compose run --publish 80:4200 node bash

Then create the angular app and start it up as you were doing.


Test Example For Future Reference

cd tmp (or any writable folder)

ng new myProject

cd myProject

ng serve --host 0.0.0.0 (--host 0.0.0.0 to listen to all the interfaces from the container)

Then in your browser, go to localhost and you should see the angular welcome page now as the port 4200 is published and bound to the host port 80 through the publish command as I showed above.

Everytime you have port forwarding issues, if you open a new terminal keeping the other terminal where you executed the original run command and run docker ps you will see this in the Ports column:

0.0.0.0:80->4200/tcp which means that your host on port 80 is forwarding to your container in port 4200 successfully.

If you see something like 4200/tcp and not the -> part, that means there is no mappings or ports published.

这篇关于ng服务在Docker容器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆