Search code examples
pythonbottledocker-build

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


I've been trying to setup a container to run an app with the bottle framework. Read everything I could find about it, but even so I can't do it. Here's what I did:

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  

By running the command docker build -t testapp I create the container.
Then by running the command docker run -p 8080:8080 testapp I get this terminal output:

Bottle v0.12.13 server starting up (using WSGIRefServer())... Listening on http://localhost:8080/ Hit Ctrl-C to quit.

But when I go to localhost:8080/testing I get localhost refused connection.

Can anyone point me to the right direction?


Solution

  • Problem is this line:

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

    It is exposing it for "localhost" insde the container you are running the code. You can use python library netifaces to get container external interface if you want to but I suggest you to set 0.0.0.0 as host like:

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

    Then you will be able to access http://localhost:8080/ (asuming your docker engine is at localhost)

    EDIT: mind your previous container might still be listening on 8080/tcp. Remove or stop previous container first.