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?

  • Dockerfile - Defining an ENV variable with a dynamic value
  • Docker intermittently failing when building image
  • docker registry push error
  • How to call a container, running on a virtual machine, from a Windows browser?
  • Attach and run a command in a docker container
  • syntax error in wercker file when using wercker login command
  • 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

  • Kitchen and Kitchen-docker
  • Can not delete a docker image because repository is missing
  • Docker environmental variables sharing
  • Does Docker contain the Heardbleed exploit?
  • php-fpm error “no input file specified” with Docker
  • Can Kubernetes be used like Docker Compose?
  • 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.