简体   繁体   English

docker-py:Docker Python sdk container.put_archive 不工作

[英]docker-py : Docker Python sdk container.put_archive not working

I have copied a example.py file from local system to container using put_archive under /root/ directory.我已经使用 /root/ 目录下的 put_archive 将 example.py 文件从本地系统复制到容器。 I want to run the file in container.我想在容器中运行文件。 The file is not found in container.在容器中找不到该文件。 Here is my code: What's wrong in my code?这是我的代码:我的代码有什么问题? I expect put_archive should copy a file example.py under /root directory.我希望 put_archive 应该在 /root 目录下复制一个文件 example.py。 The file is not found in container.在容器中找不到该文件。

import docker
import os
import tarfile
import time
import json
from io import BytesIO

client = docker.from_env()
image = client.images.pull("malark79/ibm-cloud-python-sdk")

container = client.containers.run("malark79/ibm-cloud-python-sdk", "pwd", detach=True)
print("Docker logs: " + str(container.logs()))

print(container.id)
print(container.name)
print(container.image)

f = open("example.py", "w") //open local file
with tarfile.open("example.py.tar.gz", "w:gz") as tar:
        tar.add(f.name, arcname=os.path.basename(f.name))
t = open("example.py.tar.gz", "rb")

works = container.put_archive("/root/", data=t)

t.close()
if not works:
        print("Can't create file in container")

container = client.containers.run("malark79/ibm-cloud-python-sdk", "ls -al /root", detach=True)
print("Docker logs: " + str(container.logs())) ### does not display file example.py

container = client.containers.run("malark79/ibm-cloud-python-sdk", "/usr/bin/python3 /root/example.py", detach=True)
print("Docker logs: " + str(container.logs()))  ### [Errno 2] No such file or directory\n"

  

I found the issue after 1 day of working.我在工作 1 天后发现了这个问题。 The container should be running for put_archive() command to work.容器应该正在运行,put_archive() 命令才能工作。

My container was not running, so it did not work.我的容器没有运行,所以它不起作用。

Here is the working code:这是工作代码:

import tarfile
import time
import json
from io import BytesIO

client = docker.from_env()

image = client.images.pull("malark79/ibm-cloud-python-sdk")
for container in client.containers.list():
  print(container.id)
  print(container.name)
  print(container.image)

container = client.containers.get(container.id)

def copy_to(src, dst):
    name, dst = dst.split(':')
    container = client.containers.get(name)

    os.chdir(os.path.dirname(src))
    srcname = os.path.basename(src)
    print("src " + srcname)
    with tarfile.open("vpc-example.tar", 'w') as tar:
        try:
                tar.add(srcname)
        finally:
                tar.close()

    with open('vpc-example.tar', 'rb') as fd:
            ok = container.put_archive(path="/", data=fd)
            if not ok:
                raise Exception('Put file failed')
            else:
                print("no exception")

copy_to("./vpc-example.py", container.name+":/vpc-example.py")

cmd="/usr/bin/python3 /vpc-example.py"

code, str = container.exec_run(cmd)
print(code)
print(str)

If you're trying to copy code into Docker space and run it, a Docker image is a better technical match.如果您尝试将代码复制到 Docker 空间并运行它,则 Docker 图像是更好的技术匹配。 This Python code corresponds pretty much exactly to the Dockerfile:这个 Python 代码几乎完全对应于 Dockerfile:

FROM malark79/ibm-cloud-python-sdk
WORKDIR /root
COPY example.py .
CMD /usr/bin/python3 /root/example.py

Typically I'd run docker build and refer to the built image in my final code, without trying to manually copy the script in. If you need to rebuild the image every time there is a client.images.build() method that can do it.通常我会运行docker build并在我的最终代码中引用构建的图像,而无需尝试手动复制脚本。如果您需要在每次有client.images.build()方法时重建图像可以做它。

In your example code, each time you call client.containers.run() , you're creating a new container.在您的示例代码中,每次调用client.containers.run()时,您都在创建一个新容器。 You should be able to see this if you run docker ps -a (or call client.containers.list(all=True) ).如果您运行docker ps -a (或调用client.containers.list(all=True) ),您应该能够看到这一点。 The last two commands are running on the unmodified base image, without the application code copied in.最后两个命令在未修改的基础映像上运行,没有复制应用程序代码。

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

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