2

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:

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.

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

3 Answers3

1

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.

Tryph
  • 5,946
  • 28
  • 49
  • ok thanks I will give it a try. I didn't thought I can just jump into the same container and use debugger. – Ciasto piekarz May 28 '17 at 15:39
  • actually I am fully aware of debugger but whats bothering me is tat the app works if I run on host mac OS machine directly but not on Linux container – Ciasto piekarz May 28 '17 at 15:54
  • following your steps: I get this message: `Default renderer not in renders, automatically switching to kajiki app_globals not provided and lib.app_globals.Globals is not available. helpers not provided and lib.helpers is not available.` – Ciasto piekarz May 28 '17 at 16:08
  • you are using the `python:3.5-slim` image as your Dockerfile base. Did you try with the standard `python:3.5`? – Tryph May 28 '17 at 16:17
  • tried didn't helped also tried python 3.4 which is most compatible and tested on with `dukpy` required for rendering javascript. still didn't work, however I am on `Debian 4.9.2-10` with `gcc version 4.9.2` . – Ciasto piekarz May 28 '17 at 16:56
1

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.

Robert
  • 33,429
  • 8
  • 90
  • 94
  • Quick question: why would you pack the whole app inside the container ? – Ciasto piekarz May 28 '17 at 17:57
  • It's one of the main features of docker: pack applications in images and deliver them. If you need to develop and get changes refreshed quickly, you can `docker run` with a volume. I will add it to the answer – Robert May 28 '17 at 18:02
0

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.

Qasim Sarfraz
  • 5,822
  • 2
  • 20
  • 26