简体   繁体   English

Dockerfile公开node.js图像的端口

[英]Dockerfile expose port for node.js image

I'm trying to expose port for node.js image but it's doesn't expose the port outside : 我正在尝试为node.js映像公开端口,但它不会暴露外部端口:

FROM node:4-onbuild
ADD . /opt/app

EXPOSE 8000:8000

CMD ["npm", "start"]

only when I declare the port when running the image it's works 只有当我在运行图像时声明端口它才有效

docker run -p=8000:8000 node

Is there something I'm missing? 有什么我想念的吗?

The EXPOSE directive is metadata only. EXPOSE指令仅是元数据 If you check: 如果你检查:

https://docs.docker.com/engine/reference/builder/#expose https://docs.docker.com/engine/reference/builder/#expose

You'll see you still need to manually expose the port. 您将看到仍需要手动公开端口。 The EXPOSE directive is useful for platforms which might want to know what a container expects to expose. EXPOSE指令对于可能想知道容器期望公开的平台非常有用。 For example, on OpenShift if I deployed your container it would suggest I expose port 8000. 例如,在OpenShift上,如果我部署了你的容器,它会建议我暴露端口8000。

If you don't want to manually expose the port, try this: 如果您不想手动公开端口,请尝试以下操作:

docker run --net=host

This will bind the container network controller to the host network . 这会将容器网络控制器绑定到主机网络。 Be aware however this is a security concern, as there is no isolation between the container and the host machine (for TCPIP or UDP). 但请注意,这是一个安全问题,因为容器和主机之间没有隔离(对于TCPIP或UDP)。 This means your container can try to listen to any port on the host! 这意味着您的容器可以尝试侦听主机上的任何端口! Some containers do run like this, for example the consul client: 有些容器会像这样运行,例如consul客户端:

https://hub.docker.com/_/consul/ https://hub.docker.com/_/consul/

Which is a chatty client which talks over lots of ports. 这是一个讨厌很多端口的聊天客户端。 Binding it to the host makes it much easier to quickly run the client, but I would avoid that approach unless absolutely necessary. 将它绑定到主机使得快速运行客户端变得更加容易,但除非绝对必要,否则我会避免使用该方法。 Unfortunately, you'll probably just need to get used to the -p flag! 不幸的是,你可能只需要习惯-p标志!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM