任何人都可以解释为什么这不起作用:
# docker run -ti --rm golang echo $GOPATH
# docker run -ti --rm golang echo \$GOPATH
$GOPATH
当它起作用时:
# docker run -ti --rm golang bash -c "echo \$GOPATH"
/go
答案 0 :(得分:2)
1)
docker run -ti golang echo $GOPATH /Users/me/go
--rm
我删除了docker inspect container-id
标志以便能够检查容器。然后,我做了"Env": [
"no_proxy=*.local, 169.254/16",
"PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOLANG_VERSION=1.8.1",
"GOPATH=/go"
],
"Cmd": [
"echo",
"/Users/me/go"
],
:
$GOPATH
docker
在容器启动之前展开。在调用docker run -ti --rm golang echo \$GOPATH
命令之前,它已被shell扩展,确实。
2)
docker run -ti golang echo \$GOPATH $GOPATH
docker inspect container-id
"Env": [
"no_proxy=*.local, 169.254/16",
"PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOLANG_VERSION=1.8.1",
"GOPATH=/go"
],
"Cmd": [
"echo",
"$GOPATH"
],
:
$GOPATH
此处$
是一个文字字符串,并且不会被shell展开,因为您转义echo
,然后docker run -ti --rm golang bash -c "echo \$GOPATH"
收到文字var名称而不是其值
3)
docker run -ti golang bash -c "echo \$GOPATH" /go
docker inspect container-id
"Env": [
"no_proxy=*.local, 169.254/16",
"PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOLANG_VERSION=1.8.1",
"GOPATH=/go"
],
"Cmd": [
"bash",
"-c",
"echo $GOPATH"
],
:
\$
在这里,由于bash
,你的shell运行的docker也没有变量扩展,但是还有另一个shell会捕获它并扩展它:golang
会这样做但是在容器里面这个时间,其中GOPATH由docker提供为hibernate.hbm2ddl.auto
图像中定义的环境变量(GOPATH = / go)。请参阅定义的Dockerfile。