使用docker sdk for python捕获docker容器中shell脚本的输出到文件

时间:2022-11-02 13:53:08

I have a shell script inside my docker container called test.sh. I would like to pipe the output of this script to a file. I can do this using docker exec command or by logging into the shell (using docker run -it) and running ./test.sh > test.txt. However, I would like to know how the same result can be achieved using the docker sdk for python. This is my code so far:

我的docker容器里面有一个名为test.sh的shell脚本。我想将此脚本的输出传递给一个文件。我可以使用docker exec命令或登录shell(使用docker run -it)并运行./test.sh> test.txt来完成此操作。但是,我想知道如何使用docker sdk for python实现相同的结果。到目前为止这是我的代码:

import docker

client = docker.APIClient(base_url='unix://var/run/docker.sock')

container= client.create_container(
'ubuntu:16.04', '/bin/bash', stdin_open=True, tty=True, working_dir='/home/psr', \
volumes=['/home/psr/data'], \
host_config=client.create_host_config(binds={
    '/home/xxxx/data_generator/data/': {
        'bind': '/home/psr/data',
        'mode': 'rw',
    },

})
)


client.start(container=container.get('Id'))
cmds= './test.sh > test.txt'
exe=client.exec_create(container=container.get('Id'), cmd= cmds, 
stdout=True)
exe_start=client.exec_start(exec_id=exe, stream=True)

for val in exe_start:
    print (val)

I am using the Low-Level API of the docker sdk. In case you know how to achieve the same result as above using the high level API, please let me know.

我正在使用docker sdk的低级API。如果您知道如何使用高级API获得与上述相同的结果,请告诉我们。

1 个解决方案

#1


0  

In case anyone else had the same problem, here is how I solved it. Please let me know in case you have a better solution.

如果其他人有同样的问题,这就是我如何解决它。如果您有更好的解决方案,请告诉我。

import docker

client = docker.APIClient(base_url='unix://var/run/docker.sock')

container= client.create_container(
'ubuntu:16.04', '/bin/bash', stdin_open=True, tty=True, 
working_dir='/home/psr', \
 volumes=['/home/psr/data'], \
 host_config=client.create_host_config(binds={
 '/home/xxxx/data_generator/data/': {
    'bind': '/home/psr/data',
    'mode': 'rw',
},

})
)


client.start(container=container.get('Id'))
cmds= './test.sh'
exe=client.exec_create(container=container.get('Id'), cmd=cmds, 
stdout=True)
exe_start=client.exec_start(exec_id=exe, stream=True)

with open('path_to_host_directory/test.txt', 'wb') as f: # wb: For Binary Output
    for val in exe_start:
        f.write(val)

#1


0  

In case anyone else had the same problem, here is how I solved it. Please let me know in case you have a better solution.

如果其他人有同样的问题,这就是我如何解决它。如果您有更好的解决方案,请告诉我。

import docker

client = docker.APIClient(base_url='unix://var/run/docker.sock')

container= client.create_container(
'ubuntu:16.04', '/bin/bash', stdin_open=True, tty=True, 
working_dir='/home/psr', \
 volumes=['/home/psr/data'], \
 host_config=client.create_host_config(binds={
 '/home/xxxx/data_generator/data/': {
    'bind': '/home/psr/data',
    'mode': 'rw',
},

})
)


client.start(container=container.get('Id'))
cmds= './test.sh'
exe=client.exec_create(container=container.get('Id'), cmd=cmds, 
stdout=True)
exe_start=client.exec_start(exec_id=exe, stream=True)

with open('path_to_host_directory/test.txt', 'wb') as f: # wb: For Binary Output
    for val in exe_start:
        f.write(val)