Boot2Docker:无法让端口转发工作 [英] Boot2Docker: can't get ports forwarding to work

查看:22
本文介绍了Boot2Docker:无法让端口转发工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 8.1 上使用 boot2docker (docker 1.6).我想让自己的机器容器与 ruby​​ 一起玩,我希望能够从我的 Windows 主机连接到 rails 服务器.首先从小步骤开始,我想从我的 boot2docker VM 连接到我的容器.我在下面附上了我的 docker 文件,它构建起来没有问题,我可以从中运行一个容器.我是这样做的:

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

然后在这个容器中我说:

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

然后看看是否一切正常,我会这样做:

~/myapp$ sudo apt-get install wget &&wget 本地主机:3000

我得到了 http 500,没关系,我只是想检查服务器是否正在运行.然后我使用 ctrl+p、ctrl+q 退出.但是然后在 boot2docker 机器上我做 agin

wget 本地主机:3000

并得到

连接到 localhost:3000 (127.0.0.1:3000)wget:获取响应时出错:对等方重置连接

所以似乎端口 3000 没有正确转发到 boot2docker VM.我做错了什么?我错过了什么?我在谷歌上广泛搜索并尝试了一些事情,例如从 dockerfile 显式公开端口或添加 -P 开关以运行,但我总是以同样的方式结束 - 它不起作用.

任何帮助将不胜感激.

更新 02.05.2015

我也尝试过 Markus W Mahlberg 的评论和 VonC 的回复中描述的事情.我的 VM 配置似乎没问题,我还检查了 VirtualBox 的 GUI,看起来还不错.更多信息:当我开始时

boot2docker ssh -vnNTL 3000:localhost:3000

然后在我的 Windows 主机上打开 localhost:3000 我在 boot2docker 控制台的跟踪日志中看到,它们看起来像这样:

debug1: channel 1: free: direct-tcpip: 监听 localhost 3000 端口的 3000 端口,从 127.0.0.1 端口 50512 连接到 127.0.0.1 端口 3000,nchannels 3

Chrome 告诉我响应为空.通过检查容器上的日志,我知道该请求从未到达.

更新结束

2015 年 5 月 3 日更新

我认为我的问题与 boot2docker 或 docker 没有太大关系,而与我的计算机配置有关.我已经多次检查我的 docker/boot2docker 配置,所以我不太可能在那里犯错.

我拼命地重新安装了 boot2docker 和 VirtualBox,仍然没有效果.任何想法如何调试我的配置可能有什么问题?我唯一的想法是尝试在另一台机器上做同样的事情.但即使这行得通,我原来的问题也同样令人讨厌.

更新结束

这是我的 dockerfile:

来自 ubuntuMAINTAINER 匿名 <anonymous@localhost.com>标签说明="Ruby 容器"# 基于 https://gorails.com/setup/ubuntu/14.10运行 apt-get 更新运行 apt-get -y 升级运行 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-devRUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >>/etc/sudoers &&groupadd 匿名&&用户添加匿名 -m -g 匿名 -g sudoENV HOME/home/anonymous匿名用户运行 git clone https://github.com/sstephenson/rbenv.git ~/.rbenv运行 echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >>~/.bashrcRUN echo 'eval "$(rbenv init -)"' >>~/.bashrc运行执行 $SHELL运行 git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build运行 echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >>~/.bashrc运行执行 $SHELL运行 git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash环境路径$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin:$PATH"运行 rbenv 安装 2.2.1运行 rbenv 全局 2.2.1环境路径$HOME/.rbenv/shims:$PATH"运行 echo 'gem: --no-ri --no-rdoc' >~/.gemrc运行 gem 安装捆绑器运行 git config --global color.ui true运行 git config --global user.name "mindriven"运行 git config --global user.email "3dcreator.pl@gmail.com"运行 ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -C "3dcreator.pl@gmail.com"运行 sudo apt-get -qy install software-properties-common python-software-properties运行 sudo add-apt-repository ppa:chris-lea/node.js运行 sudo apt-get -y install nodejs运行 gem install rails -v 4.2.0运行 ~/.rbenv/bin/rbenv rehash运行导轨 -v运行 sudo apt-get -qy install mysql-server mysql-client运行 sudo apt-get install libmysqlclient-devRUN rails new ~/myapp -d mysql运行 sudo/etc/init.d/mysql start &&cd ~/myapp &&耙数据库:创建

解决方案

参见 Boot2docker 解决方法:

您可以使用 VBoxManage.exe 命令在 boot2docker VM 级别打开这些端口,以便您的实际 VM 主机访问它们.
默认情况下,只有端口 2222 处于打开状态,以便 boot2docker ssh 工作并打开交互式 ssh boot2docker 会话.
只需确保 VirtualBox 在您的 PATH 中.

  • VBoxManage modifyvm:在 boot2docker VM 尚未启动时或在 boot2docker stop 后工作,
  • VBoxManage controlvm:在 boot2docker VM 运行时工作,在 boot2docker start 之后.
<块引用>

假设您的 Docker 容器公开了端口 8000,并且您希望从 LAN 上的其他计算机访问它.您可以使用 ssh 临时执行此操作:

运行以下命令(并保持打开状态):

$ boot2docker ssh -vnNTL 8000:localhost:8000

<块引用>

或者你可以设置一个永久的 VirtualBox NAT 端口转发:

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

<块引用>

如果虚拟机已经在运行,你应该运行另一个命令:

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

<块引用>

现在您可以从您的主机访问您的容器

本地主机:8000

这样,您就不必弄乱 VirtualBox GUI,从左侧列表中选择名为 boot2docker-vm 的计算机,然后选择 SettingsMachine 菜单(或在 Mac 上按 Command-S),选择顶部的 Network 图标,最后点击Port Forwarding按钮.

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 <anonymous@localhost.com>
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 "3dcreator.pl@gmail.com"
RUN ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -C "3dcreator.pl@gmail.com"

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

解决方案

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.

这篇关于Boot2Docker:无法让端口转发工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆