Q: Docker limit memory usage is not working When running PHP docker using this command:
docker run -m 50M -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.0-cli php your-script.php

Where your-script.php has the following content:
<?php
    for ($i = 0; $i > -1; $i++)
        echo $i;
?>

The container keeps running even though memory usage has reached the limit 50M.
Before running docker run command, executing the following command on host machine:
$ watch "free -m"

Returned:
             total   used       free     shared    buffers     cached
Mem:           995        634        360          0        116        191

During the docker run command is running I can see the value in free column down to below 200M but the script still keeps running.

A: Your code is only counting, I won't never reach the limit. Try creating objects or something like that.
Another thing, measuring the consumed memory with free is very inexact. Due to the OS uses memory for cache. Try top and hit E and e to see the values in MB.
Regards
