1

I am getting so many difficulties with forwarding ports in docker containers. First I tried to forward ports for an existing container. Then I realize that this is not possible. So I tried with docker run command (create a new container from the docker image).

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

I checked with docker ps command and its showing these ports under the 'Ports' heading. But I used -d (Run container in background) option here. So it worked I guess.

But I really needed a interactive shell and here I want to forward some ports.

Basically I am trying to run a Ruby On Rails application (need ports 3000, 3306 etc to be forwarded) in this container and I installed all the software and rails gem etc and started the server in this interactive shell. But I don't know how to forward the ports for an interactive shell.

I tried the following:

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

Here interactive shell works but ports are not being forwarded.

EDIT: The steps I followed:

   $ 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

Still, when I go to localhost:3000, I can't access my application. I wonder whats the issue with port forwarding in docker interactive shell container. As I mentioned above the 'Forwarded ports shows nothing' when listing the containers. So I doubt the above port forwarding command with -p option not works with interactive shell.

listing the container

Solution: What I done to solve this issue as per the answer from VonC

enter image description here

Abhi
  • 3,361
  • 2
  • 33
  • 38
  • 1
    What do you mean by `But I don't know how to forward the ports for an interactive shell`? Does the second command you write not work as you need? If so, why? – Auzias Mar 10 '16 at 12:14
  • @Auzias I mean -i option here. -it option gives me an terminal to work with the container. But with this option as I mentioned above the ports are not forwarding. I don't know why.I mean After I run 'docker run -p 3000:3000 -p 3001:3001 -p 3306:3306 -p 5432:5432 -t -i /bin/bash', the ports not forwarding. Forwarded ports shows nothing. – Abhi Mar 11 '16 at 07:38

1 Answers1

0

Forwarded ports shows nothing

It might shows nothing within the container in the interactive shell, but those ports are certainly mapped to the Linux host port.

In another host Linux shell, do a docker inspect <yourContainer>: you will see all the ports as mapped to the host, meaning a curl http://localhost:3000 would work.

Note: on PC, you need to port forward this port at the VirtualBox level.
See "Not able to access tomcat application on Docker VM with host(windows) IP while using docker toolbox"

VBoxManage controlvm "default" natpf1 "tcp-port3000,tcp,,3000,,3000"
VBoxManage controlvm "default" natpf1 "udp-port3000,udp,,3000,,3000"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi @VonC I edited the question with all my steps that taken me to this problem. Please check it. I cannot use docker, because of this issue. So thinking to move to other alternative. – Abhi Mar 15 '16 at 06:14
  • The docker inspect , shows "Ports": null, under "NetworkSettings": {.. – Abhi Mar 15 '16 at 06:26
  • }, "NetworkMode": "default", "PortBindings": { "3000/tcp": [ { "HostIp": "", "HostPort": "3000" } ] }, – Abhi Mar 15 '16 at 06:32
  • Try and map 3000 to an higher port like 30300 – VonC Mar 15 '16 at 06:34
  • @Abhi and of course, you need to make sure your app is not "Exited" and remains up. – VonC Mar 15 '16 at 06:55
  • haha, yea. I tried this: localadmin@dell-PC ~ $ docker run -p 30300:3000 -t -i abhilashak/rails-test:version1 /bin/bash root@02de071d71a6:/# cd home/rails/my_project/ root@02de071d71a6:/home/rails/my_project# rails s -b 0.0.0.0 "NetworkMode": "default", "PortBindings": { "3000/tcp": [ { "HostIp": "", "HostPort": "30300" } ] }, – Abhi Mar 15 '16 at 07:04
  • @Abhi actually, try to map the other ports as well on higher ports `-p 30001:3001 -p 30306:3306 -p 30432:5432` – VonC Mar 15 '16 at 07:05
  • and I went to http://localhost:30300 , my local port. But it shows connection refused. Am I doing anything wrong here? – Abhi Mar 15 '16 at 07:08
  • @Abhi if you are using docker through boot2docker, you also need to port-forward port 30300 on your Windows host: see http://stackoverflow.com/a/35646587/6309 – VonC Mar 15 '16 at 07:10
  • I am not using boot2docker .And I am using conEmu terminal. – Abhi Mar 15 '16 at 07:42
  • @Abhi if you are using docker on a PC, you areusing boot2docker and VirtualBox. As mentioned before, port-forward 30300 at the virtualbox level – VonC Mar 15 '16 at 07:54
  • I installed docker by following this https://docs.docker.com/engine/installation/windows/ there is no installation specified for boot2docker. I think its an old/another type of installation. I installed dockerToolBox and set it up. And need to forward ports in virtual box level? – Abhi Mar 15 '16 at 08:20
  • @Abhi Exactly: VirtualBox and boot2docker. Again, port-forward at VirtualBox level: read http://stackoverflow.com/a/35646587/6309 – VonC Mar 15 '16 at 08:22
  • Correct! This solved the issue. Please add this vm port forwarding thing to your answer so that I can accept it. Again thank you for your precious time. – Abhi Mar 15 '16 at 09:31