Integration in docker-compose


#1

Hello,

I’m trying to launch a mitmproxy container with docker-compose but it always fails even with the tty: true parameter.

My docker-compose.yml is like :

version: '3'
  services:
    mitmproxy:
      image: "mitmproxy/mitmproxy"
      container_name: "mitmproxy"
      ports:
        - 8080:8080
      tty: true

But everytime I launch “docker-compose up -d”, the mitmproxy container crashes instantly.

Here are the logs about the crash :

Traceback (most recent call last):
  File "/usr/bin/mitmproxy", line 11, in <module>
    sys.exit(mitmproxy())
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/main.py", line 140, in mitmproxy
    run(console.master.ConsoleMaster, cmdline.mitmproxy, args)
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/main.py", line 122, in run
    master.run()
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/master.py", line 213, in run
    self.window = window.Window(self)
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/window.py", line 149, in __init__
    WindowStack(master, "flowlist"),
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/window.py", line 57, in __init__
    commands = commands.Commands(master),
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/commands.py", line 131, in __init__
    oh = CommandHelp(master)
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/commands.py", line 107, in __init__
    super().__init__(self.widget(""))
  File "/usr/lib/python3.6/site-packages/mitmproxy/tools/console/commands.py", line 119, in widget
    [urwid.Text(i) for i in textwrap.wrap(txt, cols)]
  File "/usr/lib/python3.6/textwrap.py", line 379, in wrap
    return w.wrap(text)
  File "/usr/lib/python3.6/textwrap.py", line 354, in wrap
    return self._wrap_chunks(chunks)
  File "/usr/lib/python3.6/textwrap.py", line 248, in _wrap_chunks
    raise ValueError("invalid width %r (must be > 0)" % self.width)
ValueError: invalid width 0 (must be > 0)

Is there a solution to that problem ?

Thanks for your answers


#2

Hi,

is there a particular reason why you want to run mitmproxy via docker-compose and not mitmdump? The problem here seems to be that we spawn in a shell with width 0, which we don’t handle properly.


#3

Hi,

I want to run mitmproxy via docker-compose to see HTTP streams directly. Mitmdump doesn’t have the same features (inspecting request and response on the air). Do you have a solution for this ?


#4

No idea, this not really is a mitmproxy issue: https://github.com/moby/moby/issues/33794


#5

I’ve managed to make it mostly work with something like the following:

    mtest:
        image: mitmproxy/mitmproxy
        entrypoint: '/bin/sh -c "stty rows 24 cols 80 && mitmproxy -p8080 --mode reverse:http://thingy:8080/"'
        tty: true
        stdin_open: true

caveats: you’ll need to start it in detached mode (docker-compose up -d ...)

and there doesn’t seem be a compose command for actually attaching to it afterwards, you need to use the docker attach <full_container_name> variant instead.

You can detach without killing the running proxy with ctrl-p,ctrl-q by default (although that appears to be sometimes forcing a stop, possibly it’s getting confused with tty flow control if docker is passing the ctrl-q onto it.

I’d just use docker attach --detach-keys ctrl-x,ctrl-x $CONTAINER instead and find a bind that isn’t taken.


#6

Finally, I’ve found a way to open the tty with stdin.

Just need to declare tty, stdin_open and container_name in the compose file.

Then when you start the containers with docker-compose up -d, the container will stop since there is no tty to attach to.

Then you just need to run docker start -i <container_name> and you will have the tty for mitmproxy with stdin enabled.