Why does echo $VARIABLE, within a /bin/bash -c “…” section of a dockerfile run instruction, return empty on docker build?

Question :

Observing the output (see below) between --- echo home and the --- env, $GOPATH is clearly defined as is $HOME. In addtion, the output of echo $HOME is visible in the between --- start and --- echo home. But, the output of echo $GOPATH between --- env and --- echo gopath is empty. Why?

  • Change “hosts” / “-h” Docker for Windows in daemon.json
  • Docker set ipaddress and start services
  • Multiple websites on one docker engine
  • Docker wait for web app to start (dependency)
  • Running Windows Server Core in Docker Container
  • Kitematic spins up new containers, but none of them showing up in Terminal
  • Details :

    Dockerfile RUN instruction that has issue

    RUN /bin/bash -c "echo '-------------------------------------------- start' ;\
                      echo $HOME ;\
                      echo '-------------------------------------------- echo home' ;\
                      source $HOME/.gvm/scripts/gvm ;\
                      gvm use go1.5.1 ;\
                      env ;\
                      echo '-------------------------------------------- env ' ;\
                      echo $GOPATH ;\
                      echo '-------------------------------------------- echo gopath' ;\
                      "
    

    Outout of docker build step for the above instruction

    Step 5 : RUN /bin/bash -c "echo '-------------------------------------------- start' ;                  echo $HOME ;                  echo '-------------------------------------------- echo home' ;                  source $HOME/.gvm/scripts/gvm ;                  gvm use go1.5.1 ;                  env ;                  echo '-------------------------------------------- env ' ;                  echo $GOPATH ;                  echo '-------------------------------------------- echo gopath' ;                  "
     ---> Running in c079d62aa7eb
    -------------------------------------------- start
    /home/amebel
    -------------------------------------------- echo home
    Now using version go1.5.1
    HOSTNAME=e611e15f9c9d
    GVM_ROOT=/home/amebel/.gvm
    LD_LIBRARY_PATH=/home/amebel/.gvm/pkgsets/go1.5.1/global/overlay/lib:
    PATH=/home/amebel/.gvm/pkgsets/go1.5.1/global/bin:/home/amebel/.gvm/gos/go1.5.1/bin:/home/amebel/.gvm/pkgsets/go1.5.1/global/overlay/bin:/home/amebel/.gvm/bin:/home/amebel/.gvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    GVM_VERSION=1.0.22
    PWD=/home/amebel
    gvm_pkgset_name=global
    SHLVL=1
    HOME=/home/amebel
    GOROOT=/home/amebel/.gvm/gos/go1.5.1
    DYLD_LIBRARY_PATH=/home/amebel/.gvm/pkgsets/go1.5.1/global/overlay/lib:
    gvm_go_name=go1.5.1
    GVM_OVERLAY_PREFIX=/home/amebel/.gvm/pkgsets/go1.5.1/global/overlay
    PKG_CONFIG_PATH=/home/amebel/.gvm/pkgsets/go1.5.1/global/overlay/lib/pkgconfig:
    GOPATH=/home/amebel/.gvm/pkgsets/go1.5.1/global
    GVM_PATH_BACKUP=/home/amebel/.gvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    _=/usr/bin/env
    -------------------------------------------- env 
    
    -------------------------------------------- echo gopath
     ---> db5582b888ab
    Removing intermediate container c079d62aa7eb
    Successfully built db5582b888ab
    ---- Finished build of amebel/golang ----
    

    Thanks in advance

  • Port exposed by Docker not reachable
  • numpy.distutils missing from numpy installation, theano tries to use it
  • Monitor docker containers based on labels assigned
  • Creating non-root user in jupyter dockerfile
  • How to run tests on CentOS 7 with Travis-CI?
  • No module named torch, docker
  • One Solution collect form web for “Why does echo $VARIABLE, within a /bin/bash -c “…” section of a dockerfile run instruction, return empty on docker build?”

    Since your string is in double quotes, the variables in it will be expanded by the original shell that’s running the command, not the shell invoked by bash -c. GOPATH isn’t defined in that original shell.

    Put the argument to bash -c in single quotes instead of double quotes, and it will be passed literally to bash.

    RUN /bin/bash -c 'echo "-------------------------------------------- start" ;\
                      echo $HOME ;\
                      echo "-------------------------------------------- echo home" ;\
                      source $HOME/.gvm/scripts/gvm ;\
                      gvm use go1.5.1 ;\
                      env ;\
                      echo "-------------------------------------------- env " ;\
                      echo $GOPATH ;\
                      echo "-------------------------------------------- echo gopath" ;\
                      '
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.