docker context
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
PARAMETER | DESCRIPTION |
---|---|
context_name |
name of the context to create
TYPE:
|
default_stack_orchestrator |
Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)
TYPE:
|
description |
Description of the context
TYPE:
|
docker |
Set the docker endpoint, you can use a dict of a class to
specify the options. The class is
TYPE:
|
from_ |
Create context from a named context
TYPE:
|
kubernetes |
Set the kubernetes endpoint. You can use a dict or a class to specify the options. The class
is
TYPE:
|
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
PARAMETER | DESCRIPTION |
---|---|
x |
One or more contexts, empty list means no-op.
TYPE:
|
force |
Force the removal of this context
TYPE:
|
update
update()
Not yet implemented
use
use(context)
Set the default context
PARAMETER | DESCRIPTION |
---|---|
context |
The context to set as default
TYPE:
|