How to limit unloading from a docker container?

I need to prevent multiple downloads of multi-channel transmission from all of my network bandwidth, but I can limit the use of bandwidth at the process level (this means that slowing down the entire network interface of the network or slowing down this network traffic of the user will not work). Fortunately, loading is containerized using Docker. What can I do to slow down the outgoing traffic of the docker container?

+4
source share
1 answer

Thanks to this question, I realized that you can run tc qdisc add dev eth0 root tbf rate 1mbit latency 50ms burst 10000in a container to set its download speed to 1 megabyte / s.

Here is an example Docker file that demonstrates this by creating a random file and loading it into / dev / null-as-a-service at an approximate download speed of 25 KB / s:

FROM ubuntu

# install dependencies
RUN apt-get update
RUN apt-get install -y iproute curl

# create a large random file to upload
RUN head -c 2M </dev/urandom > /upload.data

# rate-limit the network interface and
# upload the data when docker image is run
RUN echo "#!/bin/bash" >> /upload.sh
RUN echo "tc qdisc add dev eth0 root tbf rate 25kbps latency 50ms burst 2500" >> /upload.sh
RUN echo "curl -d @/upload.data http://devnull-as-a-service.com/dev/null" >> /upload.sh
RUN chmod a+x /upload.sh

ENTRYPOINT exec /upload.sh

Assuming you have this Docker file inside a directory with a name ratelimitthat is in your current working directory, you can run it with:

docker build ratelimit -t ratelimit && docker run --cap-add=NET_ADMIN ratelimit

The option --cap-add=NET_ADMINallows the container permission to change its network interface. You can find the documentation here .

Dockerfile , . iproute tc, curl , . 2 . script, . , , script , , .

Token, 25 /. , Token Bucker, .

Docker , cURL ( , , ).

+6

Source: https://habr.com/ru/post/1659859/


All Articles