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

问题描述:

我一直在试图设置一个容器来运行瓶框架的应用程序。阅读我能找到的所有信息,但即使如此,我也无法做到。下面是我所做的:无法管理建立和运行bottle.py应用程序

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

任何人都可以指向正确的方向吗?

问题是这一行:

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

它暴露它“localhost”的insde容器正在运行的代码。您可以使用Python库netifaces得到容器外部接口,如果你想,但我建议你设置0.0.0.0host像:

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

然后,你将能够访问​​(asuming您的码头工人发动机处于本地主机)

编辑:介意你之前的容器可能仍然在8080/tcp上侦听。先移除或停止先前的容器。

+0

这工作感谢罗伯托! –