Docker - 无法转发交互式s​​hell的端口

时间:2016-03-10 10:26:27

标签: ruby-on-rails linux shell docker ubuntu-14.04

我在Docker容器中转发端口时遇到了很多困难。首先,我尝试转发现有容器的端口。然后我意识到这是不可能的。所以我尝试使用docker run命令(从docker镜像创建一个新容器)。

 docker run -p 8080:8080 -td <image_id>

我使用docker ps命令检查并在“端口”标题下显示这些端口。但我在这里使用了-d(在后台运行容器)选项。所以我觉得它很有效。

但我真的需要一个交互式shell,在这里我想转发一些端口。

基本上我试图在这个容器中运行一个Ruby On Rails应用程序(需要转发端口3000,3306等),我安装了所有软件和rails gem等,并在这个交互式shell中启动了服务器。但我不知道如何转发交互式s​​hell的端口。

我尝试了以下内容:

 docker run -p 3000:3000  -p 3001:3001 -p 3306:3306 -p 5432:5432 -t -i <image_id> /bin/bash

此处交互式shell工作但端口未被转发。

修改: 我遵循的步骤:

   $ docker run -p 3000:3000  -p 3001:3001 -p 3306:3306 -p 5432:5432 -t -i 5c62899c063f /bin/bash
   root@342cf0dfb5a5:/# ls
   bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run sbin  srv  sys  tmp  usr  var
   root@342cf0dfb5a5:/# cd home/

   root@342cf0dfb5a5:/home# cd rails/
   root@342cf0dfb5a5:/home/rails# ls
   my_project
   root@342cf0dfb5a5:/home/rails# cd my_project/
   root@342cf0dfb5a5:/home/rails/my_project# ls
   Gemfile  Gemfile.lock  README.rdoc  Rakefile  app  bin  config  config.ru  db  lib  log  public  test  tmp  vendor

   root@340cf0dfb5a5:/home/rails/my_project# rails s -b 0.0.0.0

   => Booting WEBrick                                                       
   => Rails 4.2.5 application starting in development on http://0.0.0.0:3000
   => Run `rails server -h` for more startup options                        
   => Ctrl-C to shutdown server                                             
   [2016-03-15 05:57:14] INFO  WEBrick 1.3.1                                
   [2016-03-15 05:57:14] INFO  ruby 2.3.0 (2015-12-25) [x86_64-linux]       
   [2016-03-15 05:57:14] INFO  WEBrick::HTTPServer#start: pid=113 port=3000

但是,当我转到localhost:3000时,我无法访问我的应用程序。我想知道docker interactive shell容器中的端口转发问题是什么。正如我在上面提到的那样,在列出容器时'转发端口没有显示任何内容'。所以我怀疑上面的端口转发命令-p选项不适用于交互式shell。

listing the container

解决方案: 我按照VonC的回答解决了这个问题

enter image description here

1 个答案:

答案 0 :(得分:0)

  

转发端口不显示任何内容

它可能在交互式shell中的容器中没有显示任何内容,但这些端口肯定映射到Linux 主机端口。

在另一个主机Linux shell中,执行var count = 0; // Number of functions being called var funcArray = []; // Array of functions waiting var MAX_REQUESTS = 5; // Max requests var CALL_WAIT = 100; // 100ms function call() { // Check if count doesn't exceeds or if there aren't any functions to call if(count >= MAX_REQUESTS || funcArray.length == 0) // Call call() after 100ms setTimeout(function() { call() }, CALL_WAIT); count++; // Add request to the counter var func = funcArray.pop(); $.ajax(..., function(data) { func(data); // ....... count--; // Substract request to the counter }); } $(function() { call(); // First call to start polling. It will call itself each 100ms }); $(function() { $("div.server").each(function() { funcArray.push(function(data) { alert(data); }); }); }); :您将看到所有端口都映射到主机,这意味着卷曲http://localhost:3000可以正常工作。

注意:在PC上,您需要在VirtualBox级别转发此端口 请参阅“Not able to access tomcat application on Docker VM with host(windows) IP while using docker toolbox

docker inspect <yourContainer>