How to use Docker contexts
Docker contexts allow you to connect to docker daemons other than the local one. This is similar to the -H
argument of the Docker command.
Contexts commands allow you to declare, save, list local and remote Docker daemons and Kubernetes endpoints that you have.
An exemple here with python-on-whales:
from python_on_whales import docker, DockerContextConfig
new_context = docker.context.create(
"my_remote_ssh_server",
docker=DockerContextConfig(host="ssh://ubuntu@52.57.163.75"),
description="my server ssh with a lot more power"
)
print(docker.context.list())
# [python_on_whales.Context(name='default', endpoints={'docker': ContextEndpoint(host='unix:///var/run/docker.sock', skip_tls_verify=False)}),
# python_on_whales.Context(name='my_remote_ssh_server', endpoints={'docker': ContextEndpoint(host='ssh://ubuntu@52.57.163.75', skip_tls_verify=False)})]
new_context.use()
# it's the same to use docker.context.use("my_remote_ssh_server") or docker.context.use(new_context)
print(docker.ps()) # will list the containers in the remote server
# [python_on_whales.Container(id=...), python_on_whales.Container(id=...)]
# return to the local docker daemon
docker.context.use("default")
print(docker.ps()) # will list the containers running locally
# [python_on_whales.Container(id=...)]
Note that for this simple use case, it's equivalent to use the -H
option of the Docker client like so:
from python_on_whales import DockerClient
docker = DockerClient(host="ssh://ubuntu@52.57.163.75")
print(docker.ps())
ContextCLI
create
create(context_name, default_stack_orchestrator=None, description=None, from_=None, docker=None, kubernetes=None)
Creates a new context
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context_name |
str
|
name of the context to create |
required |
default_stack_orchestrator |
Optional[str]
|
Default orchestrator for stack operations to use with this context (swarm|kubernetes|all) |
None
|
description |
Optional[str]
|
Description of the context |
None
|
docker |
Union[Dict[str, Any], DockerContextConfig, None]
|
Set the docker endpoint, you can use a dict of a class to
specify the options. The class is |
None
|
from_ |
Optional[ValidContext]
|
Create context from a named context |
None
|
kubernetes |
Union[Dict[str, Any], KubernetesContextConfig, None]
|
Set the kubernetes endpoint. You can use a dict or a class to specify the options. The class
is |
None
|
inspect
inspect(x=None)
Returns the context object. If no argument is provided, returns the current context.
list
list()
List all Docker contexts available
Returns
`List[python_on_whales.Context]`
remove
remove(x, force=False)
Removes one or more contexts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Union[ValidContext, List[ValidContext]]
|
One or more contexts, empty list means no-op. |
required |
force |
bool
|
Force the removal of this context |
False
|
update
update()
Not yet implemented
use
use(context)
Set the default context
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
ValidContext
|
The context to set as default |
required |