Docker-compose and pdb

Question:

I see that I’m not the first one to ask the question but there was no clear answer to this:

How to use pdb with docker-composer in Python development?

When you ask uncle Google about django docker you get awesome docker-composer examples and tutorials and I have an environment working – I can run docker-compose up and I have a neat developer environment but the PDB is not working (which is very sad).

I can settle with running docker-compose run my-awesome-app python app.py 0.0.0.0:8000 but then I can access my application over http://127.0.0.1:8000 from the host (I can with docker-compose up) and it seems that each time I use run new containers are made like: dir_app_13 and dir_db_4 which I don’t desire at all.

People of good will please aid me.

PS
I’m using pdb++ for that example and a basic docker-compose.yml from this django example. Also I experimented but nothing seems to help me. And I’m using docker-composer 1.3.0rc3 as it has Dockerfile pointing support.

Asked By: McAbra

||

Answers:

Try running your web container with the –service-ports option: docker-compose run --service-ports web

Answered By: Jamey

Use the following steps to attach pdb on any python script.

Step 1. Add the following in your yml file

stdin_open: true
tty: true

This will enable interactive mode and will attach stdin. This is equivalent for -it mode.

Step 2.

docker attach <generated_containerid>

You’ll now get the pdb shell

In my experience, docker-compose up command does not provide an interactive shell, but it starts the printing STDOUT to default read-only shell.

Or if you have specified and mapped logs directory, docker-compose up command will print nothing on the attached shell but it sends output to your mapped logs. So you have to attach the container separately once it is running.

when you do docker-compose up, make it in detached mode via -d like docker-compose up -d, and connect to the container via

docker exec -it your_container_name bash

Answered By: Bhuro

If after the adding of

stdin_open: true
tty: true

you started to get issues similar to that:

        fd = self._input_fileno()
        if fd is not None and fd in ready:
>           return ord(os.read(fd, 1))
E           TypeError: ord() expected a character, but string of length 0 found

You can try to add ENV LC_ALL en_US.UTF-8 at the top of your Docker file

FROM python:3.8.2-slim-buster as build_base
ENV LC_ALL en_US.UTF-8

Answered By: alexche8
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.