title: Getting java.lang.OutOfMemoryError thrown at me when running Spark inside Docker
body: <p>I'm trying to run a Spark instance within Docker and am frequently getting this exception thrown:</p>
  
  <pre><code>16/10/30 23:20:26 ERROR SparkUncaughtExceptionHandler: Uncaught exception in thread Thread[Executor task launch worker-1,5,main]
  java.lang.OutOfMemoryError: unable to create new native thread
  </code></pre>
  
  <p>I'm using this Docker image <a href="https://github.com/sequenceiq/docker-spark" rel="nofollow">https://github.com/sequenceiq/docker-spark</a>.</p>
  
  <p>My ulimits seem ok inside the container:</p>
  
  <pre><code>bash-4.1# ulimit -a
  core file size          (blocks, -c) unlimited
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 29747
  max locked memory       (kbytes, -l) 64
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 1048576
  pipe size            (512 bytes, -p) 8
  POSIX message queues     (bytes, -q) 819200
  real-time priority              (-r) 0
  stack size              (kbytes, -s) 8192
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 1048576
  virtual memory          (kbytes, -v) unlimited
  file locks                      (-x) unlimited
  </code></pre>
  
  <p>They also look good outside the container, on the host:</p>
  
  <pre><code>kane@thinkpad ~&gt; ulimit -a
  core file size          (blocks, -c) unlimited
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 29747
  max locked memory       (kbytes, -l) 64
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 1024
  pipe size            (512 bytes, -p) 8
  POSIX message queues     (bytes, -q) 819200
  real-time priority              (-r) 0
  stack size              (kbytes, -s) 8192
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 29747
  virtual memory          (kbytes, -v) unlimited
  file locks                      (-x) unlimited
  </code></pre>
  
  <p>My Googling told me that systemd can limit the tasks and cause this issue, but I've got my task limit set to infinity:</p>
  
  <pre><code>kane@thinkpad ~&gt; grep TasksMax /usr/lib/systemd/system/docker.service 
  20:TasksMax=infinity
  
  kane@thinkpad ~&gt; systemctl status docker
  ● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2016-10-31 08:22:39 AWST; 3h 14min ago
       Docs: http://docs.docker.com
   Main PID: 1107 (docker-current)
      Tasks: 56
     Memory: 34.9M
        CPU: 30.292s
  </code></pre>
  
  <p>Any ideas? My Spark code is simply reading from a Kafka instance (running in a separate Docker container) and doing a basic map/reduce. Nothing fancy.</p>