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?

  • mount root FS read only with docker-compose
  • Docker running Windows applications
  • How to deploy Rails application on Amazon Elastic Beanstalk on a single container Docker environment
  • How to provision Docker images in Vagrant?
  • Volume binding using docker compose on Windows
  • Find opened sockets in docker container
  • 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

  • Instance only when needed - GCP
  • Docker Architecture for a test server
  • cannot remove docker image, error response from daemon
  • cannot connect: com.github.dockerjava.api.NotFoundException
  • Docker: Swarm worker nodes not finding locally built image
  • How can I create containers at time of instance creation from my project container images
  • 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.