docker-compose没有看到/var/profile.d中设置的主机环境变量

时间:2018-06-06 21:03:37

标签: docker docker-compose

以下是我docker-compose.yaml的片段,我尝试将环境变量(即$GOPATH)传递给我的Dockerfile

kontrol:
  container_name: 'kontrol'
  build:
    context: '.'
    dockerfile: 'Dockerfile'
    args:
      gopath: '${GOPATH}'
  volumes:
    - '/certs:${HOME}/.kite/certs'
  ports:
    - '6000:6000'
  depends_on:
    - 'etcd'

这是实际的Dockerfile

FROM scratch
ARG gopath
ADD ${gopath}/bin/kontrol /
ENV KONTROL_PORT=6000 \
    KONTROL_USERNAME="gonzo" \
    KONTROL_STORAGE="etcd" \
    KONTROL_MACHINES="http://etcd:2379" \
    KONTROL_KONTROLURL="http://kontrol:6000/kite" \
    KONTROL_PUBLICKEYFILE="/certs/key_pub.pem" \
    KONTROL_PRIVATEKEYFILE="/certs/key.pem" \
    KITE_ENVIRONMENT="dev" \
    KITE_REGION="ch"
CMD [ "/kontrol -initial", "/kontrol" ]

问题是即使我当前的shell中定义了GOPATH ......

[j3d@pc-5]$ echo $GOPATH
/home/j3d/Projects/go

... docker-compose看不到它:

[j3d@pc-5 test]$ sudo docker-compose -f docker-compose.yaml up -d
WARNING: The GOPATH variable is not set. Defaulting to a blank string.
...

奇怪的是,环境变量HOME是可见的并且确实有效。这些环境变量之间的唯一区别是GOPATH中设置了/etc/profile.d/go.sh

export GOROOT="/usr/local/go"
export PATH="$PATH:$GOROOT/bin"

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您使用sudo执行docker-compose。您的变量很可能未设置,因为使用sudo How to keep environment variables using sudo时不会保留环境变量。