Skip to content

Instantly share code, notes, and snippets.

@MoeweX
Created July 2, 2020 14:11
Show Gist options
  • Save MoeweX/98551a4e72307f5a74ce6d2bebad5580 to your computer and use it in GitHub Desktop.
Save MoeweX/98551a4e72307f5a74ce6d2bebad5580 to your computer and use it in GitHub Desktop.
This Gist serves the purpose of better understanding different aspects of docker.

Docker Resource Limits

This Gist serves the purpose of better understanding different aspects of docker.

Based on ideas and code from https://dzone.com/articles/docker-container-resource-management-cpu-ram-and-i

# clean up preparation
docker stop $(docker ps -a -q)
docker rm -f $(docker ps -a -q)

Memory

# run container with memory limit
docker container run -d --memory=20m --memory-swap=20m --name myPython python:3-alpine sleep 3600
# open container shell
docker exec -it myPython /bin/sh
# enter python3 environment
python3
# allocate up to 100mb of memory through string concatenation
longstring = []
for x in range(100):
    len(longstring)
    longstring.append('1' * 10**6)

# this program will get killed, when memory limit reached
# stop and remove container
docker container stop myPython
docker container prune -f

Conclusion:

  • --memory controls how much memory the container has; without setting --memory-swap, the container has the same amount of swap available (so double of what is configured with --memory)
  • setting --memory-swap to the same value as --memory disables swap

CPU

docker container run -d --name cputest1 --cpus=1.0 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=1000 | md5sum'
docker logs cputest1
docker container run -d --name cputest2 --cpus=0.5 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=1000 | md5sum'
docker logs cputest2

# stop and remove container
docker container stop cputest1 cputest2
docker container prune -f

Conclusion:

  • --cpus is well suited to control cpu performance (in the microbenchmark, 0.5 requires almost exactly twice as long as 1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment