我正在尝试替换sh命令之前的变量值。
url="google.com"
docker run --rm -it \
--volume="/home/administrator/tmp:/home/simha:rw" \
someimage /bin/sh -c 'ping "${url}"'FInally我想做
sh -c 'ping google.com'那么,如何在上述中对url值进行细分。
发布于 2022-09-15 17:19:25
由于您有单引号中的值,所以主机shell不会解释它;但是不会将它传递到图像中,因此它也不在容器的环境中。
在这里,我建议删除容器命令上的sh -c包装器,只运行您所指的命令。
# set a shell variable in the host shell
url=https://google.com
# launch the container; have the host shell inject that variable
docker run --rm \
someimage \
curl "$url"如果您确实希望容器内有一个shell来展开变量,则需要安排将变量传递到容器中。一种简单的方法是使用docker run -e选项而不是主机-shell变量。
docker run --rm \
-e url=https://google.com \
someimage \
sh -c 'curl "$url"'或者,没有值的docker run -e name会将环境变量从主机复制到容器中;在shell上下文中,请注意变量必须是export编辑的。
url=https://google.com
export url
docker run --rm \
-e url \
someimage \
sh -c 'curl "$url"'https://stackoverflow.com/questions/73734707
复制相似问题