docker - 在特定端口上运行 docker 映像
问题描述
我是 Docker 新手。
我试图使用 nginx 基础映像对一个简单的静态网站进行 docker 化。当我运行时,应用程序在本地服务器上运行良好。
docker run -d -P <container-name>
所以,这里的应用程序在某个随机端口上运行,我可以看到我的应用程序正在运行。同时,当我尝试使用以下命令指定端口时:
docker run -d -p 5000:5000 --restart=always --name app mkd63/leo-electricals
localhost:5000 的页面显示无法访问站点。
我的 Dockerfile 是:
FROM nginx:alpine
COPY . /usr/share/nginx/html
EXPOSE 5000
解决方案
By default, the nginx image listens on port 80 inside the container.
Publishing a port creates a port forward from the host into the container. This doesn't modify what port the application is listening on inside the container, so if you forward to an unused port, you won't connect to anything.
Exposing a port in the Dockerfile is documentation by the image creator to those running the image, but doesn't modify container networking or have any control over what the application running inside the container is doing. With docker, the -P
flag uses that documentation to publish every exposed port.
To map port 5000 on the host to nginx listening on port 80 inside the container, use:
docker run -d -p 5000:80 --restart=always --name app mkd63/leo-electric
推荐阅读
- jhipster - 如何编写用于在 Jhipster 测试用例中复制除其 ID 之外的行的测试用例?
- python-3.x - 我的python代码由一个字典组成,代码给出了关键错误:7。谁能帮我理解我做错了什么?
- javascript - 在达到像素宽度后阻止 Textarea 输入
- javascript - 快捷键不起作用 ctrl + shift + numpad1
- c++ - 使用友元函数重载的 C++ 运算符。尝试添加多个对象失败
- javascript - 使用 lodash 对对象进行分组
- xamarin.forms - Microsoft Azure 存储数据移动库以及 xamarin 表单
- ios - 回到 UIViewController 不会触发 segue
- c# - 我可以帮助 C# 编译器推断这种类型吗?
- c - 如何在 CLion 中设置断点条件