-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docker context create command (#394)
- Loading branch information
1 parent
bc9a3cb
commit e5c0b8f
Showing
4 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## 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: | ||
|
||
```python | ||
from python_on_whales import docker, DockerContextConfig | ||
|
||
new_context = docker.context.create( | ||
"my_remote_ssh_server", | ||
docker=DockerContextConfig(host="ssh://[email protected]"), | ||
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://[email protected]', 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: | ||
```python | ||
from python_on_whales import DockerClient | ||
|
||
docker = DockerClient(host="ssh://[email protected]") | ||
|
||
print(docker.ps()) | ||
``` | ||
|
||
{{autogenerated}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,24 @@ def test_load_json(json_file): | |
# we could do more checks here if needed | ||
|
||
|
||
def test_create_context(): | ||
testname = "testpow" | ||
host = "ssh://[email protected]" | ||
description = "Python on whales testing context" | ||
|
||
all_contexts_before = set(docker.context.list()) | ||
with docker.context.create( | ||
testname, docker=dict(host=host), description=description | ||
) as new_context: | ||
assert new_context.name == testname | ||
assert new_context.endpoints["docker"].host == host | ||
assert new_context.metadata["Description"] == description | ||
|
||
assert new_context not in all_contexts_before | ||
|
||
assert new_context in docker.context.list() | ||
|
||
|
||
def test_inpect(): | ||
default_context = docker.context.inspect() | ||
assert default_context.name == "default" | ||
|