├── Dockerfile ├── add-latency.sh ├── Dockerfile-toxiproxy-configurer ├── time-curl.sh ├── docker-compose.yml └── README.md /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.2 2 | 3 | RUN apk update 4 | RUN apk upgrade 5 | RUN apk add curl bash 6 | CMD /timecurl/time-curl.sh $TARGET_URL 7 | -------------------------------------------------------------------------------- /add-latency.sh: -------------------------------------------------------------------------------- 1 | docker-compose run mytoxiproxy-configurer curl -s -XPOST -d '{"type" : "latency", "attributes" : {"latency" : 2000}}' http://mytoxiproxy:8474/proxies/mynginx/toxics 2 | -------------------------------------------------------------------------------- /Dockerfile-toxiproxy-configurer: -------------------------------------------------------------------------------- 1 | FROM alpine:3.2 2 | 3 | RUN apk update 4 | RUN apk upgrade 5 | RUN apk add curl bash 6 | CMD curl -s -XPOST -d '{"name" : "mynginx", "listen" : "mytoxiproxy:22220", "upstream" : "mynginx:80"}' http://mytoxiproxy:8474/proxies 7 | -------------------------------------------------------------------------------- /time-curl.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -z "$1" ]; then 4 | echo "Usage: $0 " 5 | exit 1 6 | fi 7 | 8 | while true 9 | do 10 | curl -sL -w "%{time_total} %{http_code} %{url_effective}\\n" "$1" -o /dev/null 11 | sleep 1 12 | done 13 | 14 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | mynginx: 4 | image: nginx 5 | timecurl: 6 | build: . 7 | volumes: 8 | - .:/timecurl 9 | environment: 10 | - TARGET_URL=http://mytoxiproxy:22220 11 | depends_on: 12 | - mytoxiproxy-configurer 13 | mytoxiproxy: 14 | image: shopify/toxiproxy 15 | expose: 16 | - "22220" 17 | mytoxiproxy-configurer: 18 | build: 19 | context: . 20 | dockerfile: Dockerfile-toxiproxy-configurer 21 | depends_on: 22 | - mytoxiproxy 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # toxiproxy-docker-compose-example 2 | 3 | This is a simple demonstration of one way to use [toxiproxy](https://github.com/Shopify/toxiproxy) with [docker-compose](https://docs.docker.com/compose/). 4 | 5 | ## Why? 6 | 7 | I wanted to test the behavior of an application when one of its dependencies - a database, a remote http API, etc. - is slow, or otherwise unreliable. 8 | 9 | Toxiproxy seems like a perfect tool for the job but I couldn't quite get it to work with docker-compose, so I created this bare-bones project. 10 | 11 | It might be possible to do this more simply, but it's the first thing I got working. 12 | 13 | ## Overview 14 | 15 | Four services are defined in docker-compose.yml: 16 | 17 | - 'timecurl' : repeatedly sends requests to ngnix via toxiproxy, and prints out the elapsed time, response code, and url. This is a stand-in for your application, the thing that will be sending requests to the thing that you want to put behind toxiproxy. 18 | - 'mynginx' : runs a plain vanilla nginx. This is a stand-in for the thing that you want to put behind toxiproxy. 19 | - 'mytoxiproxy' : runs toxiproxy. 20 | - 'mytoxiproxy-configurer' : Configures the toxiproxy instance. It defines a single "proxy", to the nginx. 21 | 22 | ## Prerequisites 23 | 24 | - docker-compose 25 | 26 | ### How to use - straight proxy, no added latency 27 | 28 | First, let's verify that everything works as expected with plain old straight-through proxying. 29 | 30 | Start all the containers in detached mode. 31 | 32 | ``` 33 | docker-compose up -d 34 | ``` 35 | 36 | You should see it create one network and four containers: 37 | 38 | ``` 39 | Creating network "toxiproxydockercomposeexample_default" with the default driver 40 | Creating toxiproxydockercomposeexample_mynginx_1 41 | Creating toxiproxydockercomposeexample_mytoxiproxy_1 42 | Creating toxiproxydockercomposeexample_mytoxiproxy-configurer_1 43 | Creating toxiproxydockercomposeexample_timecurl_1 44 | ``` 45 | 46 | Now check the 'timecurl' container logs: 47 | 48 | ``` 49 | docker logs toxiproxydockercomposeexample_timecurl_1 50 | ``` 51 | 52 | You should see lines like the following: 53 | 54 | ``` 55 | 0.009 200 http://mytoxiproxy:22220/ 56 | ``` 57 | 58 | This is the output from the 'time-curl.sh' script. 59 | 60 | From the above we can confirm that: 61 | 62 | - the URL shows that it is hitting nginx via toxiproxy, 63 | - the response code is 200, and 64 | - the response time is 0.009 seconds. 65 | 66 | ### Add latency 67 | 68 | Execute the following script to add 2 seconds latency to the proxy: 69 | 70 | ``` 71 | ./add-latency.sh 72 | ``` 73 | 74 | Now check the 'timecurl' logs again, and you should see that the requests are taking ~2 seconds. 75 | 76 | 77 | --------------------------------------------------------------------------------