Search code examples
amazon-web-servicesdockeramazon-elastic-beanstalkamazon-ecs

AWS Elastic beanstalk: how to set ulimit when using docker images


I am using docker environment in an Elastic beanstalk cluster but having trouble with open files limit. I verified that on the host my open files limit is 65535, but in the docker container the soft limit is 1024 and hard limit is 4096. I'd like to increase these limits inside the container, but when I tried to do that manually I got error even with root:

root@4020d4faf5fc:/# ulimit -n 20000
bash: ulimit: open files: cannot modify limit: Operation not permitted

A similar thread also shares some ideas but seems like those are related to increasing limit of the host vs container.


Solution

  • You would need the SYS_RESOURCE Linux capability to set ulimit from within the container, which would typically be specified using the --cap-add flag with docker run.

    With Elastic Beanstalk this can be accomplished in the following ways:

    1. If you are already using docker-compose, then add it to your compose file as usual (under services.<your service> key)
      ulimits:
          nofile:
              soft: 20000
              hard: 20000
      
    2. If you use Dockerrun.aws.json version 1 for single-container Docker environments, see Task Definition Resource Limits:
      {
          "AWSEBDockerrunVersion": "1",
          .
          .
          .
          "ulimits": [
              {
                  "name": "nofile",
                  "softLimit": 20000,
                  "hardLimit": 20000
              }
          ]
      }
      
    3. If you use Dockerrun.aws.json version 2 for multi-container Docker environments, this gist may be useful
      {
         "AWSEBDockerrunVersion": "2",
         "containerDefinitions": [ 
             {
                 .
                 .
                 .
                 "ulimits": [ 
                     { 
                         "hardLimit": 20000,
                         "name": "nofile",
                         "softLimit": 20000
                     }
                 ]
             }
         ]
      }
      

    See also the Elastic Beanstalk Docker docs.