Published on

How to Communicate with the Host from the Container on Docker For Windows

Authors

Today I had to run some npm scripts on a super locked-down Windows server. This server did not allow connections out of or into it meaning doing something as simple as an npm install was not trivial at all.

Luckily this machine had docker installed meaning that if I could bundle a container with all the npm dependencies I needed and run these against a service on this host my problem was then solved. Unfortunately, this version of docker is not the newest, so using host.docker.internal as suggested in this answer was not an option.

After a bit of Googling, I came across this article which solved my problem. First what I did is containerize my node project using a simple dockerfile where I intentionally leave out the entry point so that I can run my npm scripts manually from in the container. My dockerfile looks as follows:

FROM node:10.16.0
WORKDIR /root/deployer
COPY . .
RUN apt update && apt install -y vim
RUN npm install

As you can see I npm install in the container and install vim as I cannot do this from the locked-down machine.

I then built and saved this image using docker save.

I copied this image tar across to the server and loaded it into the server's docker image repo using docker load.

I ran ipconfig in a cmd shell on the server and looked for a section that looked as follows:

...
Ethernet adapter vEthernet (DockerNAT):

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ff79::5032:3f23:c3a3:ef57%9
IPv4 Address. . . . . . . . . . . : 10.0.75.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
...

The portion we need is the value for IPv4 Address.

With this information on hand I then ran the container image in interactive mode, changed the URL of my script to the IPv4 address and ran my scripts. It worked perfectly! The container ran its scripts against a service running on the host using that IP address.

Additional resources: