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?

  • Trying to install Mezzanine via Docker
  • How to setup docker to use cache from registry on every build step
  • restcomm docker image installation on windows 8.1
  • Will docker-compose allow mapping a port to two ports or do I need an ambassador?
  • How to ssh from centos 6.4 into a docker container without a password?
  • How to run docker image in windows [closed]
  • 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

  • Mounting cgroups inside a docker container
  • docker, MYSQL_ROOT_PASSWORD do not work
  • How to set up Cassandra Docker cluster in Marathon with BRIDGE network?
  • Docker installation issues on CentOS 7.1
  • Copy folder with wildcard from docker container to host
  • Linking many containers in 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.