简体   繁体   中英

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. 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. 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. The container should be running for put_archive() command to work.

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. This Python code corresponds pretty much exactly to the 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.

In your example code, each time you call client.containers.run() , you're creating a new container. You should be able to see this if you run docker ps -a (or call client.containers.list(all=True) ). The last two commands are running on the unmodified base image, without the application code copied in.

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