简体   繁体   中英

Running Docker containers on Azure using docker-py

I can find all of the ingredients for what I want to do, but I'm not sure if I can put them together.

Ultimately, I want a Python process to be able to create and manage Docker instances running on Azure.

This link shows that you can use the Docker API to fire up instances on Azure: https://docs.docker.com/engine/context/aci-integration/ . It's Beta, but I've been able to run my own container on Azure after logging in, using something like this:

docker --context myacicontext run hello-world

The second half of my problem is to call this from docker-py. The vanilla usage of docker-py is nice and striaght-forward, but I can't find any reference to the flag "--context" in the docker-py docs ( https://docker-py.readthedocs.io/en/stable/ ).

Is there a way for configuring docker-py such that it provides a --context?


EDIT:

Thanks to @CharlesXu pointing me in the right direction, I have now found that the following docker-py command does have an effect:

docker.context.ContextAPI.set_current_context("myacicontext")

This changes the default context used by the docker cmd line interface, so

C:\Users\MikeSadler>docker ps -a

will subsequently list the containers running in Azure, and not locally.

However , the docker.from_env().containers.list(all=all) command stubbornly continues to return the local containers. This is true even if you restart the Python session and create a new client from a completely fresh start.


CONCLUSION:

Having spoken with the Docker developers, as of October 2020 docker-py officially does not support Cloud connections. They are currently using GRPC protocols to manage Cloud runs, and this may be incorporated into docker-py in the future.

I'm afraid there is no way to do things like the command docker --context myacicontext run hello-world does. And there is also no parameter like --context in the SDK. As I know, you can set the current context use the SDK like this:

import docker
docker.context.config.write_context_name_to_docker_config('context_name')

But when you use the code:

client = docker.from_env()
client.containers.run('xxx')

Then it will set the context into default . It means you cannot run the containers into ACI. I think it may be a bug that needs to be fixed. I'm not very very sure, but that's it right now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM