Boot2Docker: can't get ports forwarding to work
Asked Answered
M

3

3

I'm playing with boot2docker (docker 1.6) on windows 8.1. I wanted to make myself machine container to play with ruby and I want to be able to connect to rails server from my windows host. To start with small steps first I want to connect to my container from my boot2docker VM. I attach my docker file below, it builds without a problem and I can run a container from it. I do it like so:

docker run -it -p 3000:3000 3564860f7afd /bin/bash

Then in this container I say:

cd ~/myapp && bundle exec rails server -d

And to see if everything is working I do:

~/myapp$ sudo apt-get install wget && wget localhost:3000

and I get http 500, which is ok, I just wanted to check if server is running. Then I exit using ctrl+p, ctrl+q. But then on boot2docker machine I do agin

wget localhost:3000

and get

Connecting to localhost:3000 (127.0.0.1:3000)
wget: error getting response: Connection reset by peer

So it seems like port 3000 is not correctly forwarded to boot2docker VM. What have I done wrong? What did I miss? I googled extensively and tried couple of things like explicitly exposing port from dockerfile of or adding -P switch to run, but I always end up the same way - it's not working.

Any help will be greatly appreciated.

UPDATE 02.05.2015

I have also tried things described in comment from Markus W Mahlberg and reponse from VonC. My VM configuration seems to be ok, I also checked in GUI of VirtualBox and it seems fine. Some more info: When I start

boot2docker ssh -vnNTL 3000:localhost:3000

and then open localhost:3000 on my windows host I see in trace logs in boot2docker console, they look like this:

debug1: channel 1: free: direct-tcpip: listening port 3000 for localhost port 3000, connect from 127.0.0.1 port 50512 to 127.0.0.1 port 3000, nchannels 3

Chrome tells me that the response was empty. From checking the logs on container I know that request never got to it.

End of update

Update 03.05.2015

I thing that my problem have not so much to do with boot2docker or docker as with my computer configuration. I've been over my docker/boot2docker configuration so many times, that it is rather unlikely that I've made a mistake there.

Desperately I've reinstalled boot2docker and VirtualBox, still no effects. Any ideas how to debug what can be wrong with my configuration? Only other idea I have is to try doing the same on another machine. But even if this works my original problem is no less annoying.

End of update

Here is my dockerfile:

FROM ubuntu
MAINTAINER anonymous <[email protected]>
LABEL Description="Ruby container"
# based on https://gorails.com/setup/ubuntu/14.10
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
    && groupadd anonymous \
    && useradd anonymous -m -g anonymous -g sudo
ENV HOME /home/anonymous
USER anonymous

RUN git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
RUN echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
RUN echo 'eval "$(rbenv init -)"' >> ~/.bashrc
RUN exec $SHELL

RUN git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
RUN echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
RUN exec $SHELL
RUN git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

ENV PATH "$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin:$PATH"

RUN rbenv install 2.2.1
RUN rbenv global 2.2.1

ENV PATH "$HOME/.rbenv/shims:$PATH"

RUN echo 'gem: --no-ri --no-rdoc' > ~/.gemrc
RUN gem install bundler

RUN git config --global color.ui true
RUN git config --global user.name "mindriven"
RUN git config --global user.email "[email protected]"
RUN ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -C "[email protected]"

RUN sudo apt-get -qy install software-properties-common python-software-properties
RUN sudo add-apt-repository ppa:chris-lea/node.js
RUN sudo apt-get -y install nodejs

RUN gem install rails -v 4.2.0
RUN ~/.rbenv/bin/rbenv rehash
RUN rails -v

RUN sudo apt-get -qy install mysql-server mysql-client
RUN sudo apt-get install libmysqlclient-dev
RUN rails new ~/myapp -d mysql
RUN sudo /etc/init.d/mysql start && cd ~/myapp && rake db:create
Middendorf answered 1/5, 2015 at 19:3 Comment(2)
The ports are only exposed to the VM running the docker daemon. See acaird.github.io/computers/2014/11/16/… for details.Trappings
@MarkusWMahlberg thanks for the link, but I did all of that before. There is still a chance, that I have done something wrong, but I have no idea what. I see what seems correct configuration in VM GUI after setting ports forwarding.Middendorf
A
4

See Boot2docker workarounds:

You can use VBoxManage.exe commands to open those ports on the boot2docker VM level, in order for your actual VM host to access them.
By default, only the port 2222 is open, for boot2docker ssh to work and open an interactive ssh boot2docker session.
Just make sure VirtualBox is in your PATH.

  • VBoxManage modifyvm: works when the boot2docker VM isn't started yet, or after a boot2docker stop,
  • VBoxManage controlvm: works when the boot2docker VM is running, after a boot2docker start.

Let's say your Docker container exposes the port 8000 and you want access it from your other computers on your LAN. You can do it temporarily, using ssh:

Run following command (and keep it open):

$ boot2docker ssh -vnNTL 8000:localhost:8000

or you can set up a permanent VirtualBox NAT Port forwarding:

$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";

If the vm is already running, you should run this other command:

$ VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port8000,tcp,,8000,,8000";

Now you can access your container from your host machine under

localhost:8000

That way, you don't have to mess around with the VirtualBox GUI, selecting the computer called boot2docker-vm from the list on the left, choosing Settings from the Machine menu (or press Command-S on a Mac), selecting the Network icon at the top, and finally clicking the Port Forwarding button.

Autoxidation answered 2/5, 2015 at 4:44 Comment(4)
Thanks for responding. What you sum up in your response is more or less what I have tried so far. When I start 'boot2docker ssh -vnNTL 3000:localhost:3000' and then open localhost:3000 on my windows host I see in console trace logs like: 'debug1: channel 1: free: direct-tcpip: listening port 3000 for localhost port 3000, connect from 127.0.0.1 port 50512 to 127.0.0.1 port 3000, nchannels 3'. Chrome tells me that response was empty. From checking the logs on container I know that request never got to it. Is there any way to debug this?Middendorf
@Middendorf try a boot2docker stop and the modifyvm command with the right port. Then a simple boot2docker start.Autoxidation
I did it (just now again, which is like 7th time in total). I check if the configuration was added correctly using VirtualBox GUI, it was. No effect. I think that I set my boot2docker correctly, but there seems to be something on my machine that does not allow me to see the effects. Any ideas what should I check?Middendorf
@Middendorf not sure: if the container maps its port for the Linux host and the VM opens the same port... it should work. If it does not, some other factor on the host prevents the access (like for example a firewall, or some network settings)Autoxidation
E
1

boot2docker on Windows (and OSX) is running a VirtualBox VM with Linux in it. By default it exposes only the ports necessary to ssh into the VM. You'll need to modify the VM to get it to expose more ports.

Adding ports to the VM is more about configuring VirtualBox and less about boot2docker (it is a property of the VM, not the software running inside it). Please see the VirtualBox documentation for "port forwarding" and other network configuration. https://www.virtualbox.org/manual/ch06.html

Evolution answered 1/5, 2015 at 23:4 Comment(0)
M
0

Yes you need to open the ports in the Virtualbox machines:

enter image description here

Melissiamelita answered 6/11, 2018 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.