简体   繁体   中英

docker(Mac/Windows): how to access host machine service from the container?

I can access the host machine's services from inside a docker container using --network="host" option in docker run command. This works well on Ubuntu. The same command, though, isn't working on Mac OS Sierra(10.12.4) and Windows 10.

My use case is that I have a local mongoDB server running on mongodb://127.0.0.1:27017. I spin up my container using the following command:

docker run -p 3002:3002 --network="host" image-name

Dockerfile:

FROM node:7.5

RUN npm install -g pm2

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

EXPOSE 3002

ENV NODE_ENV local

CMD pm2 start --no-daemon server.js

I have tried using --net="host" option both on Mac and Windows. It doesn't work. Please help.

The macOS and (Windows OS) cannot run Docker natively so Docker for Mac actually installs a very small Hxyve linux VM on your mac. Your docker containers run inside this VM and they will look like they are running under your mac, well most of the time. When you specify --network host your docker containers will share the same network interface as the Hxyve VM, not your mac. So what's running on the loopback (localhost) network on your mac is not reachable to the container.

You need to make your mongo db accessible by, for instance, running it on the bridge network accessible to the Hxyve VM instead of 127.0.0.1. It's typically in the 192.168.65.X subnet.

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