简体   繁体   English

如何将容器上的端口映射到主机?

[英]How do I map a port on a container to the host?

I'm trying to run a container described by the following Dockerfile: 我正在尝试运行以下Dockerfile描述的容器:

FROM node:11.4.0

RUN npm install -g sh
RUN npm install -g json-server

WORKDIR /data
VOLUME /data
COPY db.json /data

CMD json-server --watch db.json --port 3001

and specifying the listening port by running: 并通过运行以下命令指定侦听端口:

 docker run -it -p 3001:3001 abelalejandro/json-server:final

The container seems to be running fine and json-server is telling me it is serving my requests on port 3001 yet I can't get any joy when browsing http://localhost:3001 该容器似乎运行良好,并且json-server告诉我它正在端口3001上处理我的请求,但浏览http:// localhost:3001时我无法获得任何喜悦

在此处输入图片说明

Am I missing something on publishing/exposing ports? 我在发布/公开端口上缺少什么吗?

It is binding to localhost instead of 0.0.0.0 (any host). 它绑定到localhost而不是0.0.0.0 (任何主机)。

You can change that by setting: 您可以通过设置来更改它:

CMD json-server --watch db.json --host 0.0.0.0 --port 3001

I'm assuming you are using https://github.com/typicode/json-server . 我假设您正在使用https://github.com/typicode/json-server

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

相关问题 如何从Container向主机公开端口? - How do I expose port from Container to host? 我如何从Docker容器中知道主机的映射端口? - How do I know mapped port of host from docker container? 如何将容器上的端口公开给本地主机并将持久数据绑定到容器? - How do I both expose the port on a container to the local host AND bind persistent data to the container? 每个暴露的端口都重定向到 8080,我如何将 map 容器端口重定向到 8080 以外的任何其他端口? - Every exposed port is redirecting to 8080, how do I map container port to any other rather than 8080? 我如何访问在Docker容器(由Rancher管理)中运行的MySQL,而不在主机上发布端口? - How do I access MySQL running in a docker container (managed by Rancher) without publishing the port on the host? 如何进行从本地主机到docker容器的端口转发 - how to do port forwarding from local host to docker container 如何将容器“指向”8080 端口? - How do I “point” container to port 8080? 如何将Docker容器端口映射到其他号码-但不在主机上 - How can I map a Docker containers port to a different number - but not on the host 如何将 docker 容器端口暴露给另一个容器 - How do I expose a docker container port to another container 如何在Docker中将端口从主机暴露给容器? - How to expose port from host to container in Docker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM