Allow a container running via docker-machine to connect with Mysql or XDEBUG port on parent OSX system without using an OSX DHCP assigned ip address? - docker

I've got the following setup:
OSX running MySQL listening on all network adaptors at port 3306
XDEBUG enabled IDE listening on port 9000 on the base OSX system.
docker-machine host running on the OSX system with the host ip 192.168.99.100
A debian based docker container with a mysql client running on the docker host and HHVM running with xdebug looking to connect to some lucky remote host on port 9000.
The ip addresses change frequently on the OSX system due to being assigned via DHCP, so I want the docker container to be able to somehow be able to hit the mysql server regardless of what IP the native OSX network adaptors get assigned (without manually updating it). Also, I need a stable ip I can provide my HHVM server.ini file a remotes host for Xdebug.
With running a base system of linux this isn't an issue as the docker host and the actual native machine running docker are one-and-the-same. Also, there are several ways for a container to learn of the host's ip so the issue isn't hitting the docker host.
However, in OSX running docker-machine, the host ain't the native OSX system, but instead is a VM running in virtual box (assuming you're using the vb driver, and who the sam hill blazes isn't?).
The only thing I could think of was to port forward request on 3306 to the docker-machine host (192.168.99.100 which never changes) to the OSX's port 3306. Then have the container hit the docker-machine host for Mysql requests. IF this works, I could rinse and repeat for any port I port I need to link like xdebug on port 9000.
Does anyone know how to accomplish this or have another suggestion?

Figured a way out without needing to make any changes that provides a consistent ip to connect to on the base OSX system. Docker machine sets things up in such a way to make this possible.
Docker machine creates a virtualbox VM with 2 network adaptors, one set up as host-only, the other set as NAT. Don't know why it creates 2, but
The host-only adaptor provides the OSX an ip of 192.168.99.1 and the various VM's using it get addresses starting with 192.168.99.100. However, inside the VM network, you can't use the address 192.168.99.1 to hit ports on the parent OSX system (not sure why, but guessing host only intends to be only communication between the VMs).
The NAT network adaptor is set so the OSX get's the ip 10.0.2.2 and the VM get's 10.0.2.15. With a NAT, you can route to the OSX system at 10.0.2.2 from both the docker host VM and containers running on the host.
Since this 10.0.2.2 address for the OSX machine doesn't change (unless you screw with the virtual box networking settings) bingo, got what I need.

Related

Accessing Docker running on Host machine from VMWare Workstation VM

I have the following setup:
Windows 10 Host (Hyper-V enabled)
Docker Desktop installed on host
VMWare Workstation Pro (16)
Windows 10 VM - Docker CLI installed on vm
The Windows 10 VM is used as a dev environment, with project-specific stuff on there.
I also use the host as a development machine for other projects - so want to be able to use docker on both.
What I'd like to do is access the docker engine running on the host, from my VM
By access docker, I mean use the docker cli to run containers, build images etc... setting DOCKER_HOST or something like that?
Is this possible? Or any other way?
So far, I've set my VM to use NAT networking and tried:
docker -H tcp://192.168.126.2:2375 images
Which returns
error during connect: Get http://192.168.126.2:2375/v1.40/images/json: dial tcp 192.168.126.2:2375: connectex: No connection could be made because the target machine actively refused it.
192.168.126.2 is the ip of the default gateway, from within the VM (so - my host?)
On the host machine, if I do docker -H tcp://0.0.0.0:2375 images I get the expected result.
On the host machine, I've also set:
"hosts": ["tcp://0.0.0.0:2375"],
within the docker engine config:
so what i would do and usually am doing is in VMware Workstation in Network editor I connect VMs to a bridge and select my main line that provides connectivity whether it is an Ethernet port or Wifi and associate it to lets say VMnet0. Then in VM settings I assign that VM's NIC to VMnet0 and that is how my VM and my host are on same LAN.
I would not use NAT.

Docker in virtualbox on ubuntu

I have win 10 as an operating system, I have installed virtualbox where ubuntu is installed and docker with its containers is installed in ubuntu.
I set the virtualbox network in bridge, and in DHCP I assigned an IP that I can easily reach from the win 10 chrome browser (outside of virtualbox). The problem is that I cannot access the docker container where a webserver runs in loalhost, I can access it without problems inside the virtualbox and externally I can access another webserver in the virtualbox but not the webserver docker! How could I solve it?
thanks for any replies!
It seems that I have solved, I describe here the simple solution that I have adopted.
the VM has an IP assigned in dhcp by the bridge network. (this setting has remained for a second webserver to work) In virtualbox settings I simply enabled a second NAT network from "network-settings" and in "advanced-port forwarding" I only added host port on 80 and guest port on 80, because docker run establishes its ip and its port reachable only on local host (in this case reachable only inside virtualbox).

Run docker container on localhost via VM

I'm new to Docker and Containers, and I'm trying to run a simple asp.net web app in a container but running into issues. My OS is Windows 10 Home, so I have to use the Docker Toolbox, which runs on a VM that only includes a basic Linux OS. When I spin up the container, it seems to start fine, but I can't view the app on the localhost.
$ docker run -p 8342:5000 -it jwarren:project
Hosting environment: Production
Content root path: /app
Now listening on: http://*:5000
Application started. Press Ctrl+C to shut down.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98cc4aed7586 jwarren:project "dotnet run" 8 minutes ago Up 8 minutes 0.0.0.0:8342->5000/tcp naughty_brattain
I've tried several different recommendations that I found on the web, but none have helped so far. However, my knowledge of networking is very limited, so maybe I'm not fully understanding what needs to be done. I've tried accessing it with the default VM machine IP and the container IP. I understand that the port forwarding does not carry over to the container. Any assistance would be great, as this project is due on Tuesday, and this is the last road block before finishing.
I found the following post that was really helpful: How to connect to a docker container from outside the host (same network) [Windows]. Following the steps below worked perfectly:
Open Oracle VM VirtualBox Manager
Select the VM used by Docker
Click Settings -> Network Adapter 1 should (default?) be "Attached
to:NAT"
Click Advanced -> Port Forwarding Add rule: Protocol TCP, Host Port
8080, Guest Port 8080 (leave Host IP and Guest IP empty)
You should now be able to browse to your container via localhost:8080 and your-internal-ip:8080.
Started up the container (Dockerfile EXPOSES 5000):
docker run -p 8080:5000 -it jwarren:project
Was able to connect with http://localhost:8080
There are few things to consider when working with a VM networking.
Virtual Box has 3 types of networking options NAT, Bridged and Host Only.
NAT would allow your VM to access internet through your internet. But won't allow your HOST machine to access the VM
Host Only network will create a network where the VM can reach the host machine and the Host can reach the VM. No internet using this network
Bridged network will allow your VM to assign another IP from your Wifi router or the main network. This IP will allow VM to have net access as well as access to other machines on the network. This will allow even the host machine to reach the IP
Now in most cases when you want to run Docker inside a VM and access that VM using the host machine you want the VM to have both NAT and Host only bridges
Now accessing your app on port 8342 needs few things checked
seliunx, firewalld, ufw are disabled on your VM (or properly configured to allow the port)
Your VM has a host only network or bridged network
iptables -S should not show REJECT rules
Some VMs come pre-configure to only allow port 22 from external network. So you should try access the app on <hostonlyip>:8342 or <bridgedip>:8342.
If you want to test if the app is up or not you can do the following
docker inspect <containerid> | grep IPA
Get the IP from this and run the command
curl http://<containerip>:5000/
This command needs to be execute inside the VM and not on your machine. If this command doesn't work then your container is not listening on 5000. Sometimes app listen to only 127.0.0.1 inside the container. This means they will work only inside the container and not outside. The app inside the container needs to listen to 0.0.0.0
If nothing works you can try an ssh tunnel approach
ssh -L 8342:127.0.0.1:8342 user#<VMIP>
And then you should be able to access the app on localhost:8342

Virtualbox port forwarding with docker

I'm running virtualbox locally and I've used port forwarding like this
0.0.0.0:7000 -> 0.0.0.0:7000
so that I can do
curl http://localhost:7000
from host to vm and be able to communicate with the application running in the vm and listening to port 7000.
Is it possible to make the reverse? I want to set a port forward to be able to
curl http://localhost:6000
from my vm and be able to communicate with the app that runs on host and listens on port 6000.
I'm using NAT.
I already know about bridged network and about using the network IP of my host. I can't use those. All I'm interested in is the above.
Exclaimer:
The reason of the limitations above is because I'm using dinghy with docker and docker-machine. If I change the network to something else than NAT the setup will break. Moreover I can't use something else than localhost since these are the defaults that the apps have and I need them to communicate as if they were running both on host.
Possible options:
Setup an SSH tunnel with ssh -R, see https://unix.stackexchange.com/questions/46235/how-does-reverse-ssh-tunneling-work
Setup Nginx or Apache reverse proxy on the vm to forward traffic to host.
Force the VM to think localhost is your host ip by adding it to the /etc/hosts file (this has the potential risk of breaking other services that may depend on localhost being 127.0.0.1)

Use VirtualBox to access site on host from guest? the host and guest is linux

I'm running VirtualBox on a ubuntu (host), the VM i'm using is fedora (guest). VirtualBox is setup to use the NAT network adapter, and I'm able to get to the internet.
i use Port Forwarding to access ssh and the rails web server
port Forwarding Rules
<NAT>
<DNS pass-domain="true" use-proxy="false" use-host-resolver="false"/>
<Alias logging="false" proxy-only="false" use-same-ports="false"/>
<Forwarding name="Rule 1" proto="1" hostport="5679" guestport="22"/>
<Forwarding name="Rule 2" proto="1" hostport="3080" guestport="3000"/>
</NAT>
now i can access internet on guest machine , and i can logging through ssh
but i cannot access the Rails web server on port 3080
i tried :
localhost:3080
10.0.2.15:3080 #the guest IP
what i can do all i need is SSH and internet connection and open the web site i host on gust machine by Browser in host machine
thanks
Just a stab in the dark here but this might be your issue...
When you provision a new Linux Virtual Machine in VirtualBox, the Network settings for that VM are set to 'NAT'. This results in your new VM being assigned a 10.x.x.x address.
Change this setting to Bridged mode, which will assign your VM an address within the same subnet as your host (most likely 192.168.x.x).
Try to connect to the site running on your host.
If you're still not able to access the site running on localhost, confirm the app isn't blocking connections from your new VM. Add a rule in to your access list (possible iptables) permitting tcp port 80 traffic from your VM's address (found using ifconfig).
Happy hunting!
In your VB you have to set network only with host. It is in preferences->network.
Then you can access your virtual machine via 192.168.56.101 from ubuntu. Port forwarding set on this network card. List it with ifconfig. I recommend to use iptables for port redirects.

Resources