REST不提供Docker python应用程序

2024-04-16 21:12:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我们在python上有一个简单的应用程序:REST controller,返回JSON。你知道吗

在这里: https://github.com/NikaGolybeva/smt

控制器:

#!flask/bin/python
from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
    {
        'id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
        'done': False
    },
    {
        'id': 2,
        'title': u'Learn Python',
        'description': u'Need to find a good Python tutorial on the web',
        'done': False
    }
]

from flask import abort


@app.route('/test', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

@app.route('/test/<int:task_id>', methods=['GET'])
def get_task(task_id):
    task = list(filter(lambda t: t['id'] == task_id, tasks))
    if len(task) == 0:
        abort(404)
    return jsonify({'task': task[0]})



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

Dockerfile文件:

FROM python:3.7
EXPOSE 5000
ADD run.py /
RUN pip install pystrich
RUN pip install flask
CMD [ "python", "./run.py" ]

我们在端口5000上运行docker应用程序:

v-chernyshov@v-chernyshov:~/IdeaProjects/smt$ docker build -t smt .

...

v-chernyshov@v-chernyshov:~/IdeaProjects/smt$ docker run -p 5000:5000 smt
 * Serving Flask app "run" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 309-988-075

等待,当我们试图在http://localhost:5000/test/1上发送请求时,应用程序将返回JSON。但事实上,应用程序是不可检查的。你知道吗

好吧。变型2。你知道吗

将应用程序添加到docker-合成.yml并部署到虚拟服务器上。docker compose如下所示:

services:
  web:
    image: account/front:0.0.1
    restart: on-failure
    ports:
      - 80:3000
  back:
    image: account/back:0.0.3
    restart: on-failure
    ports:
      - 8099:8099
  smth:
    image: nikagolybeva/smth:0.0.1
    restart: on-failure
    ports:
      - 5000:5000

我们又回到了java和smth的python上。我编写了一个方法,试图通过REST每分钟向smtn发送一次请求:

@Scheduled(initialDelay = 1000L, fixedDelay = 60000L)
public void testPython() {
    String url = "http://smth:5000/test/1";
    log.info("testing python, url: {}", url);
    PythonTestDto pythonTestDto = restTemplate.getForObject(url, PythonTestDto.class);
    String result = "";
    try {
        result = new ObjectMapper().writeValueAsString(pythonTestDto);
    } catch (JsonProcessingException e) {
        e.getMessage();
    }
    log.info("response: {}", result);
}

用docker compose运行它。你知道吗

后台消息部署在其端口上(&M):

smth_1_f8c6211ef46d |  * Serving Flask app "run" (lazy loading)
smth_1_f8c6211ef46d |  * Environment: production
smth_1_f8c6211ef46d |    WARNING: This is a development server. Do not use it in a production deployment.
smth_1_f8c6211ef46d |    Use a production WSGI server instead.
smth_1_f8c6211ef46d |  * Debug mode: on
smth_1_f8c6211ef46d |  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
smth_1_f8c6211ef46d |  * Restarting with stat
smth_1_f8c6211ef46d |  * Debugger is active!
smth_1_f8c6211ef46d |  * Debugger PIN: 769-002-661

...

back_1_9622a732ea20 | 2019-09-26 08:16:31.725  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8099 (http) with context path ''

从后面Ping smth:

root@kvm1:/home/xpendence/docker# docker-compose exec back ping smth
PING smth (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.396 ms
64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.142 ms
64 bytes from 172.21.0.3: seq=2 ttl=64 time=0.175 ms
64 bytes from 172.21.0.3: seq=3 ttl=64 time=0.195 ms
64 bytes from 172.21.0.3: seq=4 ttl=64 time=0.113 ms
64 bytes from 172.21.0.3: seq=5 ttl=64 time=0.127 ms
64 bytes from 172.21.0.3: seq=6 ttl=64 time=0.521 ms
^C
--- smth ping statistics ---
7 packets transmitted, 7 packets received, 0% packet loss
round-trip min/avg/max = 0.113/0.238/0.521 ms

没关系,但尝试通过REST发送请求失败:

back_1_9622a732ea20 | org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://smth:5000/test/1": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

拜托,告诉我,怎么了


Tags: dockerrunfromidapptaskbytestime
1条回答
网友
1楼 · 发布于 2024-04-16 21:12:45

这个站点运行在docker上(就像一个虚拟机,它有自己的IP),所以你不能从你的本地机器上访问它。 使用docker machine,您可以从docker machine的IP输出获取VM的IP,然后连接到您获得的IP。你知道吗

顺便说一句,如果我没有错的话,你必须把你的flask应用程序设置为监听0.0.0.0。你知道吗

请检查此链接以获取参考。。。 https://medium.com/@mtngt/docker-flask-a-simple-tutorial-bbcb2f4110b5

相关问题 更多 >