Op Koriyuki my Docker-python SDK

Source: Internet
Author: User


Installing the Python SDK for Docker

[[Email protected] ~] #pip Install Docker

here, starting with the simplest run of a container, run a command inside the container "echo" Hello " "World" "

In [1]: Import docker in [2]: client = docker.from_env () in[3]: Print Client.containers.run ("Redis", ["echo", "Hello", "wor LD "]) Hello World

of course you will not be satisfied with these, you want to run your container in the background " Detach "The parameters will meet your needs." . you can get the container's container by the result of starting the container ID

in [+]: container = Client.containers.run ("Registry.cn-hangzhou.aliyuncs.com/forker/redis", Detach=true) in [14]: Print container.id1cb26e2f144b453fc7857518ea4dba39058af5387d602515583cb21d1be4b50b

Now you can manage the list of containers you are running, using the " client.containers.list () "Method gets a list of container objects and traverses the output container ID .

in [+]: For i in Client.containers.list (): ....: Print i.id ....: 1cb26e2f144b453fc7857518ea4dba39058af5387d602 515583cb21d1be4b50b066a454c8d0b0923325448bb72ca792d1bdd26058cb95452a293f8edb3f2d9788a2681f1100c0e01683ef65b15bfd7737e5a27 3b309fb72b7144fb216ea24c2dda2a940052080863c0df07478dfb795fabdf9a6e258f3c651461c161d182ae3f

By traversing the output we know which containers are now running, and we can operate on them, such as turning them off, which is a straightforward operation.

in [+]: For I inclient.containers.list (): Print I.stop () ....: Nonenone

of course most of the cases are not like stopping containers, we are more inclined to observe Docker container information, such as viewing container logs

in [+]: s=client.containers.get (' da2a94005208 ') in []: Print s.logs () time= "2017-02-20t09:11:00z" Level=info msg= " Listening for health checks On0.0.0.0:80/healthcheck "Time=" 2017-02-20t09:11:00z "level=info msg=" connecting to Cattle The event stream. Time= "2017-02-20t09:11:00z" Level=info msg= "subscribing to metadata changes."

Course " s "There are many more ways to try the following:

in [30]: print s.s.attach         s.client          s.diff            s.get_archive    s.kill            s.pause          s.remove          s.restart        s.stats           s.top             s.waits.attach_socket  s.collection     s.exec_run        s.id              s.logs           s.put_archive     s.rename &Nbsp;       s.short_id       s.status         s.unpause       s.attrs           s.commit          s.export         s.id_attribute    s.name           s.reload          s.resize         s.start           s.stop            s.update

Now the basic operation of the container you know, now introduce the basic operation of the image, now let you view the next image list

In [30]: for image in client.images.list ():   ....:      PRINT IMAGE.ID   ....:    SHA256: B12D4D26900A5E71A16EB317CB0D8275514C522C2885FCD8CEFF5469252C644BSHA256: e9fbe11760fd7ad68dbdfe1a58c9a3350452e5c7eebd60a9af47ded5f1e47918sha256 : 24d2df8fa301bfc3043b59e9a874df1338f835f742a0123cfb4f3a6e152b5f88 Of course the "image" method is more than these, more methods are as follows:in [31]:   print image.image.attrs         image.collection     image.id            image.reload         image.short_id      image.tags          image.client         image.history       image.id_attribute  image.save    &Nbsp;      image.tag 

Now that you can see your image, of course you need to pull the new image.

Import dockerclient =docker.from_env () image =client.images.pull ("Alpine") print Image.id We can also submit a container to create a mirrored import Dockerclient =docker.from_env () container =client.run ("Alpine", ["Touch", "/helloworld"],detached=true) Container.wait () Image =container.commit ("HelloWorld") print image.id here just introduces some simple features, if you need more complicated operations can go to the Official SDK documentation to see https:// docker-py.readthedocs.io/en/stable/

This article from "Yun Koriyuki My Journey" blog, reproduced please contact the author!

Op Koriyuki my Docker-python SDK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.