简体   繁体   English

运行docker的Python脚本

[英]Python script to run docker

Flow of the python script: python脚本的流程:

  • I want to run docker image from python script. 我想从python脚本运行docker镜像。
  • After running docker image, I need to execute a shell script which creates a tar file inside docker container. 运行docker映像后,我需要执行一个shell脚本,该脚本在docker容器内创建一个tar文件。
  • I need to copy that tar file to host machine from docker container. 我需要将该tar文件从Docker容器复制到主机。
  • and then python script should continue with some stuff to be executed on host machine. 然后python脚本应继续执行一些在主机上执行的操作。

Using docker-py module, I was able to do following: 使用docker-py模块,我能够执行以下操作:

 pip install docker-py

    docker version
    Client:
     Version:      1.12.1
     API version:  1.24
     Go version:   go1.6.3
     Git commit:   23cf638
     Built:        Thu Aug 18 05:22:43 2016
     OS/Arch:      linux/amd64

    Server:
     Version:      1.12.1
     API version:  1.24
     Go version:   go1.6.3
     Git commit:   23cf638
     Built:        Thu Aug 18 05:22:43 2016
     OS/Arch:      linux/amd64

    >>> import docker

    >>> c = docker.Client(base_url='unix://var/run/docker.sock',version='1.12',timeout=10)

    >>> c.images()
    [{u'Created': 1476217543, u'Labels': {}, u'VirtualSize': 5712315133, u'ParentId': u'sha256:1ba2be8d70b6ede3b68b1af50759e674345236dd952225fcbfbcc1781f370252', u'RepoTags': [u'ubuntu14.04_64:latest'], u'RepoDigests': None, u'Id': u'sha256:1c8ced0fb34d776adafaed938d41a69e3bab87466beaa8752d49bde0d81230c5', u'Size': 5712315133}]

    >>> ctr = c.create_container('ubuntu14.04_64:latest') 

    >>> c.start(ctr)

    docker  ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    d685766385e7        ubuntu14.04_64:latest              "bash"              16 hours ago        Up 16 hours                             focused_fermi

    docker images
    REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
    ubuntu14.04_64               latest              1c8ced0fb34d        21 hours ago        5.712 GB

I see docker image and container running on host, but now if I want to run shell script inside docker container, how can I do that? 我看到Docker映像和容器在主机上运行,​​但是现在,如果我想在Docker容器中运行shell脚本,该怎么办? after that I need to copy tar from container to host also. 之后,我还需要将tar从容器复制到主机。 Can someone suggest how to do this? 有人可以建议如何做吗?

This is probably the sequence of commands you want (borrowing from this answer ): 这可能是您想要的命令序列(从此答案中借用):

docker create --name tmp -it ubuntu14.04_64:latest
docker start tmp
docker exec tmp tar czvf tmp.tgz etc/os-release
docker stop tmp
docker cp tmp:tmp.tgz tmp.tgz
docker rm tmp

Look through this documentation for the equivalent commands with docker-py. 本文档中查找与docker-py等效的命令。

If you want to run a script in a container you should create a Dockerfile which contains that script. 如果要在容器中运行脚本,则应创建一个包含该脚本的Dockerfile An example might look something like this: 一个例子可能看起来像这样:

FROM ubuntu14.04_64:latest
COPY ./script.sh /code/script.sh
CMD /code/script.sh -o /target/output.tar.gz

Then your python script would look something like this: 然后,您的python脚本将如下所示:

#!/usr/bin/env python
import docker

c = docker.from_env()
c.build('.', image_name)
ctr = c.create_container(image_name, volumes='./target:/target')
c.start(ctr)

# the tarball is now at ./target/output.tar.gz, copy it where you want it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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