Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行docker的Python脚本_Python_Python 2.7_Docker_Docker Compose_Dockerpy - Fatal编程技术网

运行docker的Python脚本

运行docker的Python脚本,python,python-2.7,docker,docker-compose,dockerpy,Python,Python 2.7,Docker,Docker Compose,Dockerpy,python脚本的流程: 我想从python脚本运行docker映像 在运行docker映像之后,我需要执行一个shell脚本,该脚本在docker容器中创建一个tar文件 我需要将tar文件从docker容器复制到主机 然后python脚本应该继续在主机上执行一些东西 使用docker py模块,我能够做到以下几点: pip install docker-py docker version Client: Version: 1.12.1 A

python脚本的流程:

  • 我想从python脚本运行docker映像
  • 在运行docker映像之后,我需要执行一个shell脚本,该脚本在docker容器中创建一个tar文件
  • 我需要将tar文件从docker容器复制到主机
  • 然后python脚本应该继续在主机上执行一些东西
使用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

我看到docker映像和容器在主机上运行,但现在如果我想在docker容器中运行shell脚本,我该怎么做呢?之后,我还需要将tar从容器复制到主机。有人能建议怎么做吗?

这可能是您想要的命令序列(借用):


查看docker py的等效命令。

如果要在容器中运行脚本,应创建一个包含该脚本的
Dockerfile
。示例可能如下所示:

FROM ubuntu14.04_64:latest
COPY ./script.sh /code/script.sh
CMD /code/script.sh -o /target/output.tar.gz
#!/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
然后,您的python脚本将如下所示:

FROM ubuntu14.04_64:latest
COPY ./script.sh /code/script.sh
CMD /code/script.sh -o /target/output.tar.gz
#!/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

我已经阅读了文档,但找不到如何使用python从容器运行shell脚本并将tar从容器复制到主机。这里不需要使用exec,只需将CMD设置为要运行的脚本即可。感谢您的建议,但我需要从主机将目录装载到容器,因此我不能使用“CMD”要在构建docker时运行脚本,我需要在运行docker时运行脚本。我尝试在Dockerfile中设置entrypoint,我在Dockerfile中更改了workdir,即使我的脚本与workdir位于同一路径,我也会遇到错误:没有这样的文件或目录。如果我能够通过将脚本作为Dockerfile中的入口点来运行脚本,我将能够在容器中生成tar文件。如何复制使用python脚本在容器中生成的tar文件?
CMD
的工作原理类似于
ENTRYPOINT
,它在容器启动时运行,而不是在构建期间运行。不要担心试图复制文件。使用“主机绑定装载”(又称绑定卷),以便容器中的
/target
路径实际上是主机上的一个目录。我在我的CMD中添加了“CMD./mydir/myshell.sh/repo”,其中“/repo”从主机装载到容器。我运行docker,但无法看到tar文件是从上面的CMD创建的。这个脚本大约需要3分钟来生成tar,我是否需要在Dockerfile中包含任何其他命令?