简体   繁体   English

无法访问在Docker容器中运行的Flask Web应用程序

[英]Not able to access Flask Web application running inside a Docker container

I have the following Dockerfile where I build a docker image that contains ElasticSearch , Kibana4 and gunicorn 我有以下Dockerfile在其中构建包含ElasticSearchKibana4gunicorn

# gunicorn-flask                                                                                                                                                                               
FROM devdb/kibana

MAINTAINER KS <k.s@gmail.com>

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get install -y python python-pip python-virtualenv gunicorn
RUN pip install -U pip 

# Setup flask application
RUN mkdir -p /deploy/app

COPY gunicorn_config.py /deploy/gunicorn_config.py
COPY app /deploy/app
COPY run_gunicorn /etc/service/gunicorn

RUN pip install -r /deploy/app/requirements.txt

WORKDIR /deploy/app

EXPOSE 5000 

The python Flask application and the requirements.txt which contains the dependencies are in a folder named app in the same path as the Dockerfile . python Flask应用程序和包含依赖项的requirements.txt位于名为app的文件夹中,该文件夹与Dockerfile相同。

I also have two files gunicorn_config.py and run_gunicorn in the same path. 我在同一路径中还有两个文件gunicorn_config.pyrun_gunicorn

The contents of gunicorn_config.py is shown below. gunicorn_config.py的内容如下所示。

    bind = "0.0.0.0:5000"
    workers = 2

The contents of run_gunicorn is shown below. run_gunicorn的内容如下所示。

    #!/bin/bash
    cd /deploy/app

I build the docker image using 我使用构建docker镜像

sudo docker build --rm -t es-kibana-gunicorn:test-10-07-15 .

I save the image to a tar file using 我使用以下方式将图像保存到tar文件中

docker save 45a61ddf8d7e > es-kb-gunicorn-13-07-15.tar

where 45a61ddf8d7e is the Image ID. 其中45a61ddf8d7e是映像ID。

I copy it over to another server and I load the image and run the container. 我将其复制到另一台服务器,然后加载图像并运行容器。

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                                                              NAMES
d62a16fcc398        45a61ddf8d7e:latest   "/sbin/my_init"     7 seconds ago       Up 6 seconds        0.0.0.0:5000->5000/tcp, 0.0.0.0:5601->5601/tcp, 0.0.0.0:9200->9200/tcp, 9300/tcp   determined_shockley   

On port 5000 it does look like my Flask web application started by gunicorn is listening. port 5000它看起来确实像是由gunicorn启动的Flask web application正在侦听。

If I try to do a POST to that webserver, I get the following error. 如果我尝试对该网络服务器进行POST ,则会收到以下错误。

Traceback (most recent call last):
  File "update_txid.py", line 27, in <module>
    r = requests.post("http://192.168.132.125:5000", data=json_data,headers=headers)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 108, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 464, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))

It would be great if someone could help me figure out what is wrong here. 如果有人可以帮助我找出问题所在,那就太好了。 I can access both port 5601 Kibana4 and port 9200 'ElasticSearch` 我可以同时访问端口5601 Kibana4和端口9200'ElasticSearch'

If you run this container on top of Boot2Docker, you need to enable the port forwarding from the "boot2docker-vm" that runs inside VirtualBox. 如果在Boot2Docker的顶部运行此容器,则需要从VirtualBox内运行的“ boot2docker-vm”启用端口转发。

This link will help you to do so : Workarounds - Port forwarding 此链接将帮助您做到这一点: 解决方法-端口转发

I had the same problem and tried Alan's workaround, but somehow the VBoxManage commands there failed for me. 我遇到了同样的问题,并尝试了Alan的解决方法,但是以某种方式,那里的VBoxManage命令对我来说失败了。

I finally found this one which worked: 我终于找到了一个有效的方法:

VBoxManage controlvm boot2docker-vm natpf1 "rule_name,tcp,127.0.0.1,5000,,5000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM