【问题标题】:Can't manage to build and run a bottle.py app无法构建和运行 bottle.py 应用程序
【发布时间】:2018-02-03 17:06:01
【问题描述】:

我一直在尝试设置一个容器来运行带有瓶子框架的应用程序。阅读我能找到的所有内容,但即使这样我也做不到。这是我所做的:

Dockerfile:

# Use an official Python runtime as a parent image
FROM python:2.7

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 8080

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

app.py:

import os
from bottle import route, run, template

@route('/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)

requirements.txt

bottle  

通过运行命令 docker build -t testapp 我创建了容器。
然后通过运行命令docker run -p 8080:8080 testapp 我得到这个终端输出:

Bottle v0.12.13 server starting up (using WSGIRefServer())... Listening on http://localhost:8080/ Hit Ctrl-C to quit.

但是当我转到localhost:8080/testing 时,我得到localhost refused connection

谁能指出我正确的方向?

【问题讨论】:

    标签: python bottle docker-build


    【解决方案1】:

    问题是这一行:

    run(host='localhost', port=8080)
    

    它在运行代码的容器中为“localhost”公开它。你可以使用python库netifaces来获取容器外部接口,但我建议你将0.0.0.0设置为host,比如:

    run(host='0.0.0.0', port=8080)
    

    然后您将能够访问http://localhost:8080/(假设您的 docker 引擎位于 localhost)

    编辑:请注意您以前的容器可能仍在侦听 8080/tcp。先移除或停止之前的容器。

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 2018-11-03
      相关资源
      最近更新 更多