简体   繁体   中英

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

I'm trying to run a container described by the following 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

在此处输入图片说明

Am I missing something on publishing/exposing ports?

It is binding to localhost instead of 0.0.0.0 (any host).

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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