Search code examples
python-3.xdockerdockerfiletornado

Dockefile Not running on port


I have this python file:

#!/usr/bin/python
import tornado.web
import tornado.ioloop


class RequestHandler(tornado.web.RequestHandler):
     def initialize(self):
        with open("Desktop/ControlCode/BI.yaml") as f:
            self.write(f.read())

class TEST(RequestHandler):
    def get(self):
        self.write("hahah")

class ONE(RequestHandler):
    
    async def post(self):
        self.write("hoo")

if __name__ == "__main__":
    app = tornado.web.Application([
        (r"/", TEST),
        (r"/one", ONE)
    ])

    app.listen(8888)
    print("I'm listening on port 8888")
    tornado.ioloop.IOLoop.current().start()

And this dockerfile:

FROM python:3

ADD test.py /

RUN pip3 install tornado


EXPOSE 8888

CMD ["python", "./test.py"]

When I'm running docker run -it -d -p 8888:8888 52c097beb9b4, an image is created but when I go to localhost:8888/ it is not connecting to it.

Note: If I remove with open(file) it is working. Any idea what I can do?


Solution

  • The script inside the Docker container doesn't have access to Desktop/ControlCode/BI.yaml on your host, so I assume an OSError is raised since the file can't be found. Copy the file when you build the Dockerfile and try to access it from within the container. Beware of paths though...

    Dockerfile:

    FROM python:3
    
    ADD test.py /
    COPY Desktop/ControlCode/BI.yaml /
    ...
    

    And from your script:

    class RequestHandler(tornado.web.RequestHandler):
     def initialize(self):
        with open("/BI.yaml") as f:
            self.write(f.read())