Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect externally to a docker container running CherryPy server

I have a simple CherryPy server running on a Docker container, and I cannot access it externally. I run it using docker run -p 8181:8181 image-name. It starts up just fine and I see the normal server log. If I try a curl (curl localhost:8181) from inside the container, I get the "Hello World" response. If I try it from my machine I get curl: (52) Empty reply from server. What am I doing wrong? Is there any configuration I missed?

Server code:

import cherrypy

class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello World!"

cherrypy.quickstart(HelloWorld(), '/', {'global': {'server.socket_port': 8181}})

Dockerfile:

FROM python:2.7.13

RUN mkdir -p /opt/server

WORKDIR /opt/server
ADD . /opt/server
VOLUME /opt/server

RUN apt-get update \
&&  apt-get install -y \
    python-setuptools \
    libopenblas-dev \
    gfortran \
    vim \
    curl \
    telnet \
    wget \
&&  pip install --upgrade pip \
&&  pip install -r requirements.txt

EXPOSE 8181

CMD python server.py

requirements.txt

bottle==0.12.13
daemon
cherrypy==8.9.1
ipython==5
lockfile
numpy
psutil
python-daemon
python-dateutil
rocket
scikit-learn==0.18.1
scipy
matplotlib

PS: Maybe relevant to the question I'm using the Docker client with version 17.03.1-ce-mac12, and I'm on a Mac ;)

EDIT: Added requirements.txt

like image 501
Guilherme Avatar asked Dec 23 '22 18:12

Guilherme


1 Answers

I'm not a cherrypy expert. However, it looks like you have to designate a host as well, to make sure you're listening to the right incoming address. This works for me:

import cherrypy

class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello World!"

cherrypy.quickstart(HelloWorld(), '/', {'global': {'server.socket_host':'0.0.0.0','server.socket_port': 8181}})
like image 114
Mano Marks Avatar answered Dec 28 '22 09:12

Mano Marks