简体   繁体   English

使用 docker-py 执行 shell 命令

[英]Executing shell command using docker-py

I'm trying to run a shell command with docker-py on an already-running container, but get an error:我正在尝试在已经运行的容器上使用 docker-py 运行 shell 命令,但出现错误:

exec: "export": executable file not found in $PATH

here's how I wrote the script:这是我编写脚本的方式:

exe = client.exec_create(container=my_container, cmd='export MYENV=1')
res = client.exec_start(exec_id=exe)

so my question is how can I run a shell command (inside the container) using docker-py?所以我的问题是如何使用 docker-py 运行 shell 命令(在容器内)?

You did it quite right.你做得很对。 But you confused shell commands with linux executables.但是您将 shell 命令与 linux 可执行文件混淆了。 exec_create and and exec_start are all about running executables. exec_createexec_start都是关于运行可执行文件的。 Like for example bash.例如 bash。 export in your example is a shell command.您的示例中的export是一个 shell 命令。 You can only use it in a shell like bash running inside the container.您只能在容器内运行的 shell(如 bash)中使用它。

Additionally what you are trying to achieve (setting a environment variable) is not going to work.此外,您试图实现的目标(设置环境变量)不会起作用。 As soon as your exec finishes (where you set the env var) the exec process will finish and its environment is been torn down.一旦您的 exec 完成(您设置 env var 的位置),exec 进程将完成并且其环境被拆除。

You can only create global container environment variables upon creation of a container.您只能在创建容器时创建全局容器环境变量。 If you want to change the env vars, you have to tear down the container and recreate it with your new vars.如果要更改环境变量,则必须拆除容器并使用新变量重新创建它。 As you probably know, all data in the container is lost upon a remove unless you use volumes to store your data.您可能知道,除非您使用卷来存储数据,否则容器中的所有数据都会在删除时丢失。 Reconnect the volumes on container creation.在创建容器时重新连接卷。

That said your example was nearly correct.那就是说你的例子几乎是正确的。 This should work and create an empty /somefile.这应该可以工作并创建一个空的/somefile。

exe = client.exec_create(container=my_container, cmd=['touch', '/somefile'])
res = client.exec_start(exec_id=exe)

To execute shell commands, use this example.要执行 shell 命令,请使用此示例。 It calls sh and tells it to run the interpreter on the given command string (-c)它调用 sh 并告诉它在给定的命令字符串上运行解释器 (-c)

exe = client.exec_create(container=my_container, 
                         cmd=['/bin/sh', '-c', 'touch /somefile && mv /somefile /bla'])
res = client.exec_start(exec_id=exe)

For actually , when execute cmd docker exec in docker container export MYENV=1 .实际上,在 docker 容器export MYENV=1中执行 cmd docker docker exec时。 It will fail and report this error它将失败并报告此错误

exec: "export": executable file not found in $PATH

Because export is a shell builtin, could run the cmd in shell.因为 export 是内置的 shell,所以可以在 shell 中运行 cmd。

whereis export
type export

can not find export in /usr/bin/ or somewhere else./usr/bin/或其他地方找不到export

There is some ways to pass through this problem.有一些方法可以解决这个问题。

case1: use -c parameter case1:使用-c参数

/bin/bash -c 'export MYENV=1 ; /bin/bash'

case2: append export cmds to a rcfile, then use this file. case2:将导出命令附加到 rcfile,然后使用此文件。

echo "exprot MYENV=1" >> <some_file_path> ; /bin/bash --rcfile <some_file_path>

case3: open a terminal, then enter the cmds to export env parameters , then open a new terminal, the env parameters will work. case3:打开一个终端,然后输入cmds导出env参数,然后打开一个新的终端,env参数就可以了。

/bin/bash
exprot MYENV=1
/bin/bash # open a new terminal

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

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