0

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

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"]

应用程序.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)

要求.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

谁能指出我正确的方向?

4

1 回答 1

3

问题是这一行:

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。先移除或停止前一个容器。

于 2017-08-25T15:52:56.600 回答