2017-08-25 42 views
0

我一直在試圖設置一個容器來運行瓶框架的應用程序。閱讀我能找到的所有信息,但即使如此,我也無法做到。下面是我所做的:無法管理建立和運行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

任何人都可以指向正確的方向嗎?

回答

3

問題是這一行:

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

這工作感謝羅伯託! –