How do I retrieve the IP of a windows host from a docker container? - windows

I am trying to obtain the true IP address of a windows host running a docker container, not the ip address of the bridge. Windows system is using WSL
The following works on a linux host, but not on a windows host.
Create a simple container:-
docker run --rm -d --network host --name test alpine tail -f /dev/null
connect into the container:-
docker exec -it test /bin/sh
run ifconfig
On Linux I can see the true host IP address listed (10.0.0.2)
On windows the host ip address isnt present, but other adapters are.

Related

"docker run -p" assigned the localhost IP but did not effect on Macos

I use the following command to create a container on MacOS,my docker version is "Docker for Mac",
docker run -itd --name dns-mysql1 --network=host -p 192.168.43.178:53:53 brilliance/dns-mysql:latest
but when it starts,it does not affect,the mapped port and IP address has changed,
As follows:
but it does work on Ubuntu or other Linux system. I want to know why.

default docker-machine ip [duplicate]

I just migrated to using Docker for Mac, from previously using Docker Toolbox with virtualbox for OSX.
I used to get the machine IP address with $(docker-machine ip default).
Is there a reliable way to get the Hyperkit IP address?
Thanks!
In opposition to Docker toolbox, Docker for Windows and Docker for Mac are designed to give you the feeling that Docker is running directly on your OS, so they use lightweight virtual machines running under lightweight hypervisors (instead of VirtualBox) handled directly by the docker executable.
Hence you won't see them with docker-machine and you won't see another IP address than localhost.
Docker for Windows relies on the HyperV hypervisor which allows a network connection to tcp://localhost:2375.
Docker for Mac relies on the xhyve hypervisor, the way it's implemented only provides a connection through the socket unix:///var/run/docker.sock.
Workaround
To provide a TCP connection for Docker for Mac:
Install socat. With brew:
brew install socat
Run this socat command to forward TCP requests to the socket
socat TCP-LISTEN:2375,reuseaddr,fork,bind=localhost UNIX-CONNECT:/var/run/docker.sock
Map what you want on tcp://localhost:2375
Up to you to run the socat command on startup, if necessary.
This was for instance useful to me to associate the Webstorm nodeJS debugger to a nodeJS container (since at the time of writing, docker debugging is supported by Webstorm docker integration plugin, but not through unix sockets).
Documentation on Docker for Mac limitations
https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds
There is no docker0 bridge on macOS
Because of the way networking is implemented in Docker for Mac, you cannot see a docker0 interface in macOS. This interface is actually within HyperKit.
You could use docker image for socat which starts every time you start 'docker for mac'
docker run -d --restart=always -p 2376:2375 -v
/var/run/docker.sock:/var/run/docker.sock bobrik/socat
TCP4-LISTEN:2375,fork,reuseaddr UNIX-CONNECT:/var/run/docker.sock
Find your docker API ip address:
ifconfig | grep 'inet 192'| awk '{ print $2}'
There's no need for working with the xhyve VM's IP address directly like you would with docker-machine. All port mappings are directly mapped to localhost.
$ docker run -d -p 8080:80 nginx:latest
$ curl localhost:8080
Also see the official documentation:
When you run a container with the -p argument, for example: $ docker run -p 80:80 -d nginx Docker for Mac will make the container port available at localhost.
My current solution is to create the containers using Docker Machine (A linux VM which is available under another IP address) and route all the traffic of the containers to the docker machine VM.
sudo route -n add -net 172.18.0.0/16 192.168.99.100
You can get the network range of your docker containers using docker inspect and the IP address of your docker machine VM using docker-machine ip
Another workaround is to use sudo ifconfig lo0 alias 172.17.0.1 so you can still use the same static IP address (if your Linux-based colleagues or bash scripts insist on using that).

Docker port mapping is failing for host network mode

Mac running Docker Version 17.12.0-ce-mac55 (23011) here.
I have a very bizarre situation with Docker that I absolutely cannot explain!
I have a Dockerized web service that runs perfectly fine outside of Docker, running off of port 9200 (so: http://localhost:9200)
I can also run several other images locally (nginx, Oracle DB) and I can access them via localhost:80 and localhost:1521 respectively
When I run the container for my Dockerized service, I see (via docker logs <containerId>) the service startup without any errors whatsoever
Despite the fact that the container is running without any errors, I absolutely cannot connect to it from my Mac host via localhost:9200
The exact steps to reproduce are:
Clone this repo
Build the image via ./gradlew clean build && docker build -t locationservice .
Run the container via docker run -it -p 9200:9200 -d --net="host" --name locationservice locationservice
If you use docker ps to obtain the <containerId>, then you can keep hitting docker logs <containerId> until you see it has started up without errors
On my machine, when I try to curl against localhost:9200, I get "connection refused" errors (see below)
curl error is:
curl -X GET http://localhost:9200/bupo
curl: (7) Failed to connect to localhost port 9200: Connection refused
Some things I have ruled out:
localhost is absolutely resolveable from the host because we're running in host network mode and I have no problem connecting to nginx (port 80) and Oracle (port 1521) containers
The app is starting up and if you look at the logs you'll see it is starting up listening on 9200
Any ideas what the problem could be?!
Docker for Mac runs in a VM. --net=host refers to the Linux VM hosts network stack not OSX. There is no direct network path from OSX to the Docker VM other than mapped ports.
Mapped ports (docker run -p Y:N) in Docker for Mac are a little special, in addition to the user space proxy that runs on the Docker host normally, Docker for Mac also launches a user space proxy on OSX to listen on the same port and forward connections into the VM. The OSX process isn't started when using --net=host (and the Linux one isn't either of course).
→ docker run --name nc --rm --net=host -dp 9200:9200 busybox nc -lk -p 9201 -e echo hey
→ docker inspect nc --format '{{ json .NetworkSettings.Ports }}'
{}
→ sudo lsof -Pni | grep 9200
→
Then without --net=host
→ docker run --name nc --rm -dp 9200:9200 busybox nc -lk -p 9201 -e echo hey
→ docker inspect nc --format '{{ json .NetworkSettings.Ports }}'
{"9200/tcp":[{"HostIp":"0.0.0.0","HostPort":"9200"}]}
→ sudo lsof -Pni | grep 9200
vpnkit 42658 matt 28u IPv4 0x57f79853269b81bf 0t0 TCP *:9200 (LISTEN)
vpnkit 42658 matt 29u IPv6 0x57f798532765ca9f 0t0 TCP [::1]:9200 (LISTEN)
If your app requires --net=host then I would use Vagrant/Virtualbox to spin up a VM with a "Host Only" adapter. This means there is a direct network path that you can access from OSX on the VM. Here's the Vagrantfile I use.
Docker for Mac does not support host network mode very well: https://github.com/docker/for-mac/issues/1031
So at this moment the solution is to use default bridge mode.

How to pass host IP as environment variable when running a docker container on OS X

I have a docker image and when I run it I need to pass the host machine IP address as an environment variable. So I need something like this:
docker run --rm -it -e HOST_IP=<?????> -p 8000:8000 image
I am using Docker on OS X. Basically this image is running a service that I want to connect to my local PostgreSQL server. The service reads the server host IP from an environment variable.
How do I get the IP of the host machine for docker to use?
If I use local machine address 192.168.99.1 (from ifconfig), psycopg2 complains:
psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "192.168.99.100", user "postgres", database "database", SSL off
The address 192.168.99.100 mentioned in the error is the IP of my docker-machine.
How can I get the correct IP?
I'm not sure you could visit the host IP inside the docker container. Because I believe they are inside different network.
Instead of visit pgSQL on host, you should run pgSQL in another docker container, then use docker-compose.yml to connect the two docker containers, so they could link to each other, and visit each other by IP.
More details on: https://docs.docker.com/compose/compose-file/

SSH Setup on Docker Container

I have installed the ssh-server using
sudo apt-get install openssh-server on my ubuntu:latest Docker container running on Mac OSX Yosemite. I got the IP address of the container using boot2docker ip. Using the Network Utility I can verify that port 22 is open on that IP. However, I cannot ssh into that container's filesystem. I did not explicity specify that port 22 should be exported when starting the container with docker start -i CONTAINER_NAME. The command ssh -v localhost succeeds on the terminal of the container but when I try to do it from my Mac's terminal, it says:
Connection closed by x.x.x.x
I am copying the contents of sshd_config here:
http://collabedit.com/a76d6
With boot2docker ip you get the IP of Boot2Docker VM, not the IP of your ssh container. To connect from your Mac's terminal you should expose the port 22 of your container, and then you can connect using the Boot2Docker VM IP from your Mac session. I.E.:
docker run -d -p 2222:22 CONTAINER_NAME and then connect through ssh using BOOT2DOCKER_IP and 2222 port.

Resources