在Docker - 服务器连接问题中部署最小瓶子应用程序 [英] Deploying a minimal flask app in docker - server connection issues

查看:161
本文介绍了在Docker - 服务器连接问题中部署最小瓶子应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,只有依赖是烧瓶,它在docker外运行良好,并绑定到默认端口 5000 。以下是完整的来源:

 从烧瓶导入Flask 

app = Flask(__ name__)
app.debug = True

@ app.route('/')
def main():
return'hi'

if __name__ =='__main__':
app.run()

问题是,我在Docker中部署了这个服务器,但服务器正在运行,但是从容器外部无法访问。



以下是我的Docker文件。图像是ubuntu安装烧瓶。 tar只包含上面列出的 index.py

 #Dockerfile 
从dreen / flask
维护者dreen
WORKDIR / srv

#获取源
运行mkdir -p / srv
COPY perfektimprezy.tar .gz /srv/perfektimprezy.tar.gz
运行tar x -f perfektimprezy.tar.gz
运行rm perfektimprezy.tar.gz

#运行服务器
EXPOSE 5000
CMD [python,index.py]

我正在部署的步骤



$> sudo docker build -t perfektimprezy。



据我所知,以上运行正常,图像中的tar的内容 / SRV 。现在,让我们在容器中启动服务器:

  $> sudo码头运行-i -p 5000:5000 -d perfektimprezy 
1c50b67d45b1a4feade72276394811c8399b1b95692e0914ee72b103ff54c769

运行?

  $> sudo docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c50b67d45b1 perfektimprezy:最新的python index.py5秒前上传5秒0.0.0.0:5000->5000/tcp loving_wozniak

$> sudo docker日志1c50b67d45b1
*运行于http://127.0.0.1:5000/(按CTRL + C退出)
*重新启动stat

是的,似乎烧瓶服务器正在运行。这里是奇怪的地方。让我们向服务器发出请求:

  $>卷曲127.0.0.1:5000 -v 
*重建URL为:127.0.0.1:5000/
* DNS缓存中未找到主机名
*尝试127.0.0.1 ...
*连接到127.0.0.1(127.0.0.1)端口5000(#0)
> GET / HTTP / 1.1
>用户代理:curl / 7.35.0
>主持人:127.0.0.1:5000
>接受:* / *
>
*从服务器
*的空答复*连接#0到主机127.0.0.1左未完成
curl:(52)从服务器
的空答复

空回复...但是进程是否运行?

  $> sudo码头顶部1c50b67d45b1 
UID PID PPID C STIME TTY TIME CMD
根2084 812 0 10:26? 00:00:00 python index.py
root 2117 2084 0 10:26? 00:00:00 / usr / bin / python index.py

现在让我们进入服务器检查...

  $> sudo docker exec -it 1c50b67d45b1 bash 
root @ 1c50b67d45b1:/ srv#netstat -an
活动的Internet连接(服务器和已建立)
Proto Recv-Q Send-Q本地地址Foreign Address State
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:47677 127.0.0.1:5000 TIME_WAIT
活动的UNIX域套接字(服务器和已建立)
Proto RefCnt标志类型状态I节点路径
root @ 1c50b67d45b1:/ srv#curl -I 127.0.0.1:5000
HTTP / 1.0 200 OK
内容类型:text / html ; charset = utf-8
内容长度:5447
服务器:Werkzeug / 0.10.4 Python / 2.7.6
日期:星期二,2015年5月19日12:18:14 GMT

很好...但不是从外面:(我做错了什么?

解决方案

问题是你只绑定到localhost界面,你应该绑定到 0.0.0.0 如果你想要从外部访问容器,如果你更改:

 如果__name__ =='__main__':
app.run()

to



$ _ code>如果__name__ =='__main__':
app.run(host ='0.0.0.0')

它应该工作。


I have an app who's only dependency is flask, which runs fine outside docker and binds to the default port 5000. Here is the full source:

from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route('/')
def main():
    return 'hi'

if __name__ == '__main__':
    app.run()

The problem is that when I deploy this in docker, the server is running but is unreachable from outside the container.

Below is my Dockerfile. The image is ubuntu with flask installed. The tar just contains the index.py listed above;

# Dockerfile
FROM dreen/flask
MAINTAINER dreen
WORKDIR /srv

# Get source
RUN mkdir -p /srv
COPY perfektimprezy.tar.gz /srv/perfektimprezy.tar.gz
RUN tar x -f perfektimprezy.tar.gz
RUN rm perfektimprezy.tar.gz

# Run server
EXPOSE 5000
CMD ["python", "index.py"]

Here are the steps I am doing to deploy

$> sudo docker build -t perfektimprezy .

As far as I know the above runs fine, the image has the contents of the tar in /srv. Now, let's start the server in a container:

$> sudo docker run -i -p 5000:5000 -d perfektimprezy
1c50b67d45b1a4feade72276394811c8399b1b95692e0914ee72b103ff54c769

Is it actually running?

$> sudo docker ps
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                    NAMES
1c50b67d45b1        perfektimprezy:latest   "python index.py"   5 seconds ago       Up 5 seconds        0.0.0.0:5000->5000/tcp   loving_wozniak

$> sudo docker logs 1c50b67d45b1
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat

Yep, seems like the flask server is running. Here is where it gets weird. Lets make a request to the server:

 $> curl 127.0.0.1:5000 -v
 * Rebuilt URL to: 127.0.0.1:5000/
 * Hostname was NOT found in DNS cache
 *   Trying 127.0.0.1...
 * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
 > GET / HTTP/1.1
 > User-Agent: curl/7.35.0
 > Host: 127.0.0.1:5000
 > Accept: */*
 >
 * Empty reply from server
 * Connection #0 to host 127.0.0.1 left intact
 curl: (52) Empty reply from server

Empty reply... But is the process running?

$> sudo docker top 1c50b67d45b1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2084                812                 0                   10:26               ?                   00:00:00            python index.py
root                2117                2084                0                   10:26               ?                   00:00:00            /usr/bin/python index.py

Now let's ssh into the server and check...

$> sudo docker exec -it 1c50b67d45b1 bash
root@1c50b67d45b1:/srv# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:47677         127.0.0.1:5000          TIME_WAIT
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
root@1c50b67d45b1:/srv# curl -I 127.0.0.1:5000
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 5447
Server: Werkzeug/0.10.4 Python/2.7.6
Date: Tue, 19 May 2015 12:18:14 GMT

It's fine... but not from the outside :( What am I doing wrong?

解决方案

The problem is you are only binding to the localhost interface, you should be binding to 0.0.0.0 if you want the container to be accessible from outside. If you change:

if __name__ == '__main__':
    app.run()

to

if __name__ == '__main__':
    app.run(host='0.0.0.0')

It should work.

这篇关于在Docker - 服务器连接问题中部署最小瓶子应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆