nodejs
怎么获取Dockerfile
配置的EXPOSE
首先,你对 expose 理解有误。
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.
EXPOSE 并不真正暴露端口,而是起到一个文档的作用,告诉用户你打算用哪个端口。如果用户自己配置了别的端口,比如 nginx 配置里 listen 了 81,那你写 EXPOSE 就是没啥用的。
真正暴露端口,需要在 docker run
命令行里用 -p
指定,或docker-compose
里ports
下指定。
假设你的服务也是一个 web 服务,你可以接受环境变量SVC_PORT
作为监听的端口,使用你镜像的人就可以用docker run -e SVC_PORT=5000 -p 80:5000 your_image:latest
来把本机的 80 转发到容器的 5000 上。反过来说你也可以不接受端口配置,你给一个默认监听的端口号,用户自己去-p
绑定到宿主机的端口上就行。
10 回答11.1k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
4 回答2.4k 阅读✓ 已解决
应该是不能,
EXPOSE
不是做这个用的如果确实需要获取这个端口值,就再把这个端口值设置到
ENV
中吧