无法管理构建和运行bottle.py应用程序

时间:2017-08-25 15:36:34

标签: python bottle docker-build

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

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

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:3)

问题在于这一行:

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

它暴露于&#34; localhost&#34; insde正在运行代码的容器。如果您愿意,可以使用python库netifaces来获取容器外部接口,但我建议您将0.0.0.0设置为host,如:

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

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

编辑:介意你以前的容器可能仍然在监听8080 / tcp。首先删除或停止前一个容器。