set docker-machine variables using a bash script

I have a script like so:

#!/usr/bin/env bash
eval $(docker-machine env default)

The goal is to automate the setting of variables like

  • Unable to start container from jenkins
  • Keeping PostgreSQL data in docker and OSX
  • How to activate docker 1.12 in terminal without Moby the whale app on Mac
  • curl in Docker container fails b/c of ssl
  • Docker Redis CLI Timeout
  • Will docker container auto sync time with the host machine?
  • export DOCKER_TLS_VERIFY
    export DOCKER_HOST
    export DOCKER_CERT_PATH
    export DOCKER_MACHINE_NAME
    

    But when I check afterwards, the variables are not set. This is not the case if I run each export command manually. What am I doing wrong?

  • Docker is changing files ownership and permission, why?
  • docker build fails in jhipster
  • How does Docker allow portable containers if the Kernel libraries change
  • How to access container's web application from host
  • Unable to stop or remove a container in Docker, Permission denied is displayed
  • Docker build gearman not working
  • 2 Solutions collect form web for “set docker-machine variables using a bash script”

    export makes variables available only to the active shell session. If you want them to persist through sessions, you must add them to your bash profile:

    docker-machine env default >> ~/.bash_profile
    

    This way, the variables will be available in all future shell sessions. Make sure to restart the shell after executing the command.

    If you want the environment set in your current shell you need to “source” the script rather than run it.

    When you run a script, the variables will be set in the child bash processes environment and will not exist once that script/process dies.

    $ ./machine.sh
    DOCKER_HOST is tcp://192.168.99.100:2376
    $ echo "[$DOCKER_HOST]"
    []
    

    When you source a script, the variables will be set in your current environment

    $ . machine.sh
    DOCKER_HOST is tcp://192.168.99.100:2376
    $ echo "[$DOCKER_HOST]"
    [tcp://192.168.99.100:2376]
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.