将带有参数作为字符串的命令传递给docker run [英] Passing a command with arguments as a string to docker run

查看:94
本文介绍了将带有参数作为字符串的命令传递给docker run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是如何将带有参数的命令传递给 docker run .问题是 docker run 不会将命令加参数作为单个字符串.需要将它们作为 docker run 的单独的一等参数提供,例如:

The issue I'm facing is how to pass a command with arguments to docker run. The problem is that docker run does not take command plus arguments as a single string. They need to be provided as individual first-class arguments to docker run, such as:

#!/bin/bash
docker run --rm -it myImage bash -c "(cd build && make)"

但是,将命令和参数视为变量的值:

However consider the command and argument as the value of a variable:

#!/bin/bash -x
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage "$DOCKER_COMMAND"

不幸的是,这不起作用,因为 docker run 无法理解替换:

Unfortunately this doesn't work because docker run doesn't understand the substitution:

+ docker run --rm -it myImage 'bash -c "(cd build && make)"'
docker: Error response from daemon: oci runtime error: exec: "bash -c \"(cd build && make)\"": stat bash -c "(cd build && make)": no such file or directory.

稍作更改,删除了 DOCKER_COMMAND 的引号:

A slight change, removing the quotation of DOCKER_COMMAND:

#!/bin/bash -x
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage $DOCKER_COMMAND

结果:

+ docker run --rm -it myImage 'bash -c "(cd build && make)"'
build: -c: line 0: unexpected EOF while looking for matching `"'
build: -c: line 1: syntax error: unexpected end of file

如何从变量扩展字符串,以便将其作为独特的命令和参数传递给脚本内的 docker run ?

How can I expand a string from a variable so that it is passed as a distinct command and arguments to docker run inside a script?

推荐答案

docker run 命令的语法开始,即:

Start with the syntax of the docker run command, which is:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

这意味着如果您运行:

DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage "$DOCKER_COMMAND"

您将整个 $ DOCKER_COMMAND 变量作为 COMMAND 传递.您要让Docker查找与名称 bash -c(cd build&& make)" 匹配的文件,因此它失败就不足为奇了.它与"docker run不了解替换"无关.这都与您的Shell在执行命令行之前解析命令行的方式有关.

You are passing the entirety of the $DOCKER_COMMAND variable as the COMMAND. You are asking Docker to find a file matching the name bash -c "(cd build && make)", so it should be no surprise that it fails. It doesn't have anything to do with "docker run doesn't understand the substitution". This is all related to the way your shell parses command lines before executing them.

当您删除 $ DOCKER_COMMAND 周围的引号时,您最终会这样调用它(我将每个参数放在单独的一行上以使其明显):

When you remove the quotes around $DOCKER_COMMAND, you end up calling it like this (I'm putting each argument on a separate line to make it obvious):

docker
run
--rm
-it
myImage
bash
-c
"(cd
build
&&
make)"

那是行不通的,因为bash会尝试运行脚本(cd ,这应该很明显地导致 EOF在寻找匹配项时的原因' error.Bash的 -c`选项仅接受一个参数,但是由于shell扩展的工作方式,它的值为4.

And that's not going to work, because bash is going to try to run the script "(cd, which should make obvious the reason for the unexpected EOF while looking for matching"'error. Bash's-c` option only takes a single argument, but because of the way shell expansion works it's getting 4.

您可以这样操作:

DOCKER_COMMAND='cd build && make'
docker run --rm -it myImage bash -c "$DOCKER_COMMAND"

(我已删除了命令周围的括号,因为它们对您的使用方式没有任何作用.)

(I've removed the parentheses around your command because they don't do anything the way you're using them.)

这样,您将使用 bash 命令调用 docker run ,并且为bash的 -c 选项指定了一个参数( $ DOCKER_COMMAND 变量的内容).

This way, you're calling docker run with a command of bash, and you're giving bash's -c option a single argument (the contents of the $DOCKER_COMMAND variable).

这篇关于将带有参数作为字符串的命令传递给docker run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆