-1

I have a flask script which I try to execute it via docker run command. Following command i am doing

docker run -dit -v /media/sf_MY_WINDOWS/GitRepo/:/ext/GitRepo -p 5000:5000 "isbhatt/prefixman:v1" /ext/docker/vm_scripts/db_loader.sh

and db_loader.sh file contains

/usr/local/bin/python2.7 /ext/SDSNG/src/prefix_manager/manage.py runserver --host 0.0.0.0 &

but when I do curl localhost:5000 Connection refused. If I go into container and run the stuff and do curl localhost:5000 it works in container.. What is wrong here?

Output of netstat -tln on container

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:5000                0.0.0.0:*                   LISTEN  
Ishan Bhatt
  • 9,287
  • 6
  • 23
  • 44
  • BTW, if you want to deploy your app in Docker without having to learn, install and configure uWSGI, Nginx and Supervisord (to get the best performance and robustness), you may want to check this image: https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/ – tiangolo Feb 20 '16 at 16:02

2 Answers2

1

If inside your container your script is binding to 127.0.0.1, then the port forwarding normally provided using -p will not work. Ensure that inside the container your service is listening on 0.0.0.0.

You can check on what address your service is listening by running netstat -tln inside the container:

container# netstat -tln

If you see this:

tcp        0      0 127.0.0.1:5000         0.0.0.0:*               LISTEN

Then that's your problem. You want to see:

tcp        0      0 0.0.0.0:5000           0.0.0.0:*               LISTEN
larsks
  • 277,717
  • 41
  • 399
  • 399
  • @Iarsks You are right I can see 127.0.0.1:5000. What should I do to overcome it? – Ishan Bhatt Aug 26 '15 at 13:59
  • According to [these docs](https://flask-runner.readthedocs.org/en/latest/) you probably need to add `--host 0.0.0.0` to your `runserver` command. – larsks Aug 26 '15 at 14:02
  • @Iarsks Nope, In container I am able to do it, but from docker run I am still unable to connect on 5000 port. – Ishan Bhatt Aug 26 '15 at 14:20
  • Can you update your question to show (a) your modified `db_loader.sh` with the `--host` argument, and (b) the output of `netstat -tln` inside the container after it starts? – larsks Aug 26 '15 at 14:21
  • @Isrsks See the edited question. – Ishan Bhatt Aug 27 '15 at 07:03
0

Resolved it. By removing & at the end of manage.py runserver --host 0.0.0.0 That & kept the process in the background and if no process is running in foreground the container would stop even if you have provided -d option. And @Iarsks , Yup the --host has to be 0.0.0.0 .

Ishan Bhatt
  • 9,287
  • 6
  • 23
  • 44