Creates a new context. Returns: (Context): a Context object. Raises: :py:class:`docker.errors.MissingContextParameter` If a context name is not provided. :py:class:`docker.errors.ContextAlreadyExists` If a context wi
(
cls, name, orchestrator=None, host=None, tls_cfg=None,
default_namespace=None, skip_tls_verify=False)
| 21 | |
| 22 | @classmethod |
| 23 | def create_context( |
| 24 | cls, name, orchestrator=None, host=None, tls_cfg=None, |
| 25 | default_namespace=None, skip_tls_verify=False): |
| 26 | """Creates a new context. |
| 27 | Returns: |
| 28 | (Context): a Context object. |
| 29 | Raises: |
| 30 | :py:class:`docker.errors.MissingContextParameter` |
| 31 | If a context name is not provided. |
| 32 | :py:class:`docker.errors.ContextAlreadyExists` |
| 33 | If a context with the name already exists. |
| 34 | :py:class:`docker.errors.ContextException` |
| 35 | If name is default. |
| 36 | |
| 37 | Example: |
| 38 | |
| 39 | >>> from docker.context import ContextAPI |
| 40 | >>> ctx = ContextAPI.create_context(name='test') |
| 41 | >>> print(ctx.Metadata) |
| 42 | { |
| 43 | "Name": "test", |
| 44 | "Metadata": {}, |
| 45 | "Endpoints": { |
| 46 | "docker": { |
| 47 | "Host": "unix:///var/run/docker.sock", |
| 48 | "SkipTLSVerify": false |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | """ |
| 53 | if not name: |
| 54 | raise errors.MissingContextParameter("name") |
| 55 | if name == "default": |
| 56 | raise errors.ContextException( |
| 57 | '"default" is a reserved context name') |
| 58 | ctx = Context.load_context(name) |
| 59 | if ctx: |
| 60 | raise errors.ContextAlreadyExists(name) |
| 61 | endpoint = "docker" |
| 62 | if orchestrator and orchestrator != "swarm": |
| 63 | endpoint = orchestrator |
| 64 | ctx = Context(name, orchestrator) |
| 65 | ctx.set_endpoint( |
| 66 | endpoint, host, tls_cfg, |
| 67 | skip_tls_verify=skip_tls_verify, |
| 68 | def_namespace=default_namespace) |
| 69 | ctx.save() |
| 70 | return ctx |
| 71 | |
| 72 | @classmethod |
| 73 | def get_context(cls, name=None): |