无法构建 运行 一个 bottle.py 应用

Can't manage to build and run a bottle.py app

我一直在尝试为 运行 带有 bottle 框架的应用程序设置一个容器。阅读我能找到的关于它的所有内容,但即便如此我也做不到。这是我所做的:

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 我创建了容器。
然后通过 运行ning 命令 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

谁能给我指出正确的方向?

问题出在这一行:

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 引擎位于本地主机)

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