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.
Solution: What I done to solve this issue as per the answer from VonC