debugging containerised python web app

I have made this first docker container, and it works as per the Dockerfile.

FROM python:3.5-slim

RUN apt-get update && \
    apt-get -y install gcc mono-mcs && \
        apt-get -y install vim && \
        apt-get -y install nano && \
            rm -rf /var/lib/apt/lists/*

RUN mkdir -p /statics/js

VOLUME ["/statics/"]

WORKDIR /statics/js

COPY requirements.txt /opt/requirements.txt

RUN pip install -r /opt/requirements.txt

EXPOSE 8080

CMD ["python", "/statics/js/app.py"]

after running this command:

  • Understanding Dockerfile CMD/ENTRYPOINT
  • deploy containers on swarm cluster using compose file without using docker machine
  • Run an ecosystem in docker
  • Docker 1.9 overlay network - access from host
  • Is it possible to run a command when stopping a Docker container?
  • Automate iOS application with Appium in Docker
  • docker run -it -p 8080:8080 -v
    ~/Development/my-Docker-builds/pythonReact/statics/:/statics/ -d
    ciasto/pythonreact:v2

    and when I open the page localhost:8080 i get error:

    A server error occurred.  Please contact the administrator.
    

    but if I run this application normally, i.e. not containerised directly on my host machine: it works fine.

    So I want to know what is causing server error. How do I debug a python app that runs via container to know what is causing it to not work. or what am I doing wrong.

  • Design approach for hosting multiple microservices on the same host [closed]
  • HTTPS server in Docker container
  • docker deploy won't publish port in swarm
  • Compile Zlib Statically for PHP 5.6
  • How can I prevent url re-writing in EventStore using Docker?
  • Options to use latest docker on centos 6?
  • 3 Solutions collect form web for “debugging containerised python web app”

    Mainly, this:

    config.paths['static_files'] = 'statics'
    

    Should be:

    config.paths['static_files'] = '/statics'
    

    I’ve got your application up and running with your ‘Hello World’

    Did these changes:

    1) The mentioned config.paths['static_files'] = '/statics'

    2) This Dockerfile (removed VOLUME)

    FROM python:3.5-slim
    
    RUN apt-get update && \
        apt-get -y install gcc mono-mcs && \
        apt-get -y install vim && \
        apt-get -y install nano && \
            rm -rf /var/lib/apt/lists/*
    
    COPY requirements.txt /opt/requirements.txt
    
    RUN pip install -r /opt/requirements.txt
    
    COPY ./statics/ /statics/
    COPY app.py /app/app.py
    WORKDIR /statics/js
    
    EXPOSE 8080
    
    CMD ["python", "/app/app.py"]
    

    3) Moved the non-static app.py to a proper place: root of the project.

    4) Run with: docker build . -t pyapp, then docker run -p 8080:8080 -it pyapp

    You should see Serving on port 8080... from terminal output. And Hello World in browser.

    I’ve forked your Github project and did a pull-request.


    Edit:

    If you need make changes when you develop, run the container with a volume to override the app that is packed in the image. For example:

    docker run -v ./static/js/:/static/js -p 8080:8080 -it pyapp
    

    You can have as many volumes as you want, but the app is already packed in the image and ready to push somewhere.

    You can use pdb to debug Python code in CLI. To achieve this, you just have to import pdb and call pdb.set_trace() where you would like to have a breakpoint in your Python code. Basically you have to insert the following line where you want a breakpoint:

    import pdb; pdb.set_trace()
    

    Then you have to run your Python code interactively.

    You could do that by running bash interactively in your container with

    docker run -it -p 8080:8080 -v ~/Development/my-Docker-builds/pythonReact/statics/:/statics/ ciasto/pythonreact:v2 /bin/bash
    

    and then running manually your app with

    root@5910f24d0d8a:/statics/js# python /statics/js/app.py
    

    When the code will reach the breakpoint, it will pause and a prompt will be shown where you can type commands to inspect your execution.
    For more detail about the available commands, you can give a look at the pdb commands documentation.


    Also, I noted that you are building your image using the python:3.5-slim base image which is a (very) light Python image which does not include all is commonly included in a Python distribution.

    From the Python images page:

    This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run python. Unless you are working in an environment where only the python image will be deployed and you have space constraints, we highly recommend using the default image of this repository.

    Maybe using the standard python:3.5 image instead would solve your issue.

    As a quick tip for debugging containerized applications. If your application is failing with container crashed/stopped. Just launch the container image with CMD/ENTRYPOINT as /bin/bash then manually start the application once you have the container shell you can debug the application as per normal Linux system. CMD is straightforward to override as per ENTRYPOINT just use --entrypoint flag with docker run command.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.