Skip to content

Question: Ulimit with services.create ? #3343

Open

Description

@adb014

I'm trying to migrate some of my code to docker swarm and even though I can do

docker service create --ulimit nofile=66535 alpine sleep infinity

and I can do

import docker
_client = docker.from_env()
_container = _client.containers.run(image="alpine", command="sleep infinity", ulimits=[docker.types.Ulimit(name="nofile", soft=65535, hard=65535)], detach=True)

or even

import docker

_client= docker.from_env()
_service = _client.services.create(image="alpine", command="sleep infinity")

I however have no way of passing the ulimit value to the created service. Any ideas ?

Activity

  1. changed the title [-]Question: Ulimit with service.create ?[/-] [+]Question: Ulimit with services.create ?[/+] on Jun 24, 2025
  2. adb014 commented on Jun 25, 2025

    @adb014
    Author

    Ok to answer my own question, this can be done with the low level API. Looking at the docker REST API for the ServiceCreate command, the json payload includes a TaskTemplate, that includes a ContainerSpec that includes a Ulimits value. However looking at the corresponding code for ContainerSpec in dockerpy there is no capablity to include ulimits. However, as ContainerSpec is just a DictType, the following works

    import docker
    _client = docker.APIClient()
    _cont = docker.types.ContainerSpec(image="alpine", command="sleep infinity")
    _cont['Ulimits'] = [docker.types.Ulimit(name = 'nofile', hard = 65535, soft = 65535)]
    _client.service_create(docker.types.TaskTemplate(_cont))
    

    though its a little ugly. It would be nice to patch the dockerpy ContainerSpec with

            if ulimits is not None:
                if not isinstance(ulimits, list):
                    raise TypeError('ulimits must be a list')
    
                self['Ulimits'] = ulimits
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions