首页 > 解决方案 > 在特定端口上运行 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

标签: dockernginxdockerfiledocker-registry

解决方案


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

推荐阅读