2

Here below is a snippet of my docker-compose.yaml where I try to pass an environment variable (i.e. $GOPATH) to my Dockerfile:

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

And here is the actual 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" ]

The problem is that even if GOPATH is defined in my current shell...

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

... docker-compose doesn't see it:

[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.
...

Strangely, environment variable HOME is visible and does work. The only difference between those environment variables is that GOPATH is set in /etc/profile.d/go.sh:

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

Am I missing something?

j3d
  • 8,708
  • 17
  • 76
  • 150

1 Answers1

1

You execute docker-compose with sudo. Your variable is not set, most probably, because the environment variables are not kept when using sudo How to keep environment variables using sudo.

b0gusb
  • 3,160
  • 2
  • 8
  • 25