Monitoring and intercepting network requests in Docker
I recently encountered a situation where I needed to monitor and intercept network requests within Docker containers. To be honest, I’m not an expert in DevOps or server infrastructure, so learning to set up complex monitoring tools would be painful and time-consuming.
I prefer keeping things simple. I usually use mitmproxy during development. While it’s possible to run mitmproxy
in Docker, it makes the logs messy and isn’t convenient to use in that kind of environment. But then I discovered that mitmproxy
has a web interface called mitmweb
which comes with rich features.
And setting it up in Docker was really easy. If you use Docker Compose , just add it to your docker-compose.yml
file. Here is a complete example:
services:
mitmweb:
image: mitmproxy/mitmproxy:latest
container_name: mitmweb
command: >
mitmweb
--web-host 0.0.0.0
--web-port 8081
-p 8080
--no-web-open-browser
--set web_password=admin123
ports:
- "8080:8080"
- "8081:8081"
networks:
- proxy-network
restart: unless-stopped
test-app:
image: curlimages/curl:latest
container_name: test-app
environment:
# Set proxy to route requests through mitmweb
- HTTP_PROXY=http://mitmweb:8080
- HTTPS_PROXY=http://mitmweb:8080
networks:
# Both containers must be on the same network
- proxy-network
depends_on:
- mitmweb
command: >
sh -c "
echo 'Waiting for mitmweb to start...' &&
sleep 10 &&
echo 'Testing request:' &&
curl -v -k https://httpbin.org/get &&
tail -f /dev/null
"
networks:
proxy-network:
driver: bridge
To access mitmweb
, go to http://localhost:8081
and enter the password (in the example above, it’s admin123
). Tada! Now you can use it to monitor, intercept, or even simulate network requests of your application.