Skip to content
This repository was archived by the owner on Jan 1, 2021. It is now read-only.
This repository was archived by the owner on Jan 1, 2021. It is now read-only.

$(boot2docker shellinit) frequently fails (OSX) #716

Closed

Description

@skozin

Running

$(boot2docker shellinit)

frequently (but not always) produces this error:

-bash: unset: `DOCKER_HOST=tcp://192.168.59.103:2375': not a valid identifier

As a result, DOCKER_HOST is not getting set to the desired value. The output of boot2docker shellinit is the following:

$ boot2docker shellinit
    export DOCKER_HOST=tcp://192.168.59.103:2375
    unset DOCKER_CERT_PATH
    unset DOCKER_TLS_VERIFY

These commands produce no errors when executed directly (by pasting into the command line). I am not sure why command substitution fails.

boot2docker v1.4.1 (Git commit: 43241cb) is installed on OSX 10.10 with GNU Bash 4.3.30(1)-release (x86_64-apple-darwin14.0.0).

This issue seems to be different from #715, as none of my boot2docker-related paths contain whitespace (and there are no paths at all in the shellinit-generated output).

UPD: I just discovered that boot2docker shellinit sometimes produces commands in a different order:

$ boot2docker shellinit
    unset DOCKER_TLS_VERIFY
    export DOCKER_HOST=tcp://192.168.59.103:2375
    unset DOCKER_CERT_PATH

And sometimes, the export line is the last one. It seems that command substitution fails in these cases:

$(echo "
    unset DOCKER_TLS_VERIFY
    export DOCKER_HOST=tcp://192.168.59.103:2375
    unset DOCKER_CERT_PATH
")
# -bash: unset: `DOCKER_HOST=tcp://192.168.59.103:2375': not a valid identifier

$(echo "
    unset DOCKER_TLS_VERIFY
    unset DOCKER_CERT_PATH
    export DOCKER_HOST=tcp://192.168.59.103:2375
")
# -bash: unset: `DOCKER_HOST=tcp://192.168.59.103:2375': not a valid identifier

$(echo "
    export DOCKER_HOST=tcp://192.168.59.103:2375
    unset DOCKER_TLS_VERIFY
    unset DOCKER_CERT_PATH
")
# no error

Activity

  1. skozin commented on Jan 28, 2015

    @skozin
    Author

    This seems like some oddity with using both unset and export commands inside a command substitution. I've posted a question on StackExchange about this.

    UPD: This is the expected behaviour, as explained in the accepted answer. This can be fixed by replacing $(boot2docker shellinit) with either of these:

    eval "$(boot2docker shellinit)"
    . <(boot2docker shellinit) # process substitution, requires Bash 4

    Using the result of command substitution directly, i.e. $(boot2docker shellinit), will work correctly only when shellinit generates a single command or when all commands are export ones. In all other cases it will not work as intended, even if no error is produced.

    $(echo "
        export DOCKER_HOST=tcp://192.168.59.103:2375
        unset DOCKER_TLS_VERIFY
        unset DOCKER_CERT_PATH
    ")

    is equivalent to

    export DOCKER_HOST=tcp://192.168.59.103:2375 unset DOCKER_TLS_VERIFY unset DOCKER_CERT_PATH

    which exports DOCKER_HOST variable with the new value, and DOCKER_TLS_VERIFY, DOCKER_CERT_PATH and unset variables without changing their values. This is obviously not the desired effect of the original commands.

    $(echo "
        unset DOCKER_TLS_VERIFY
        export DOCKER_HOST=tcp://192.168.59.103:2375
        unset DOCKER_CERT_PATH
    ")

    is equivalent to

    unset DOCKER_TLS_VERIFY export DOCKER_HOST=tcp://192.168.59.103:2375 unset DOCKER_CERT_PATH

    which fails because it tries to unset DOCKER_HOST=tcp://192.168.59.103:2375, which is not a valid variable name.

  2. SvenDowideit commented on Jan 29, 2015

    @SvenDowideit
    Contributor

    oh wow, so its a really easy fix for us to order the commands right

    thankyou for the details!

  3. added this to the v1.5.0 milestone on Jan 29, 2015
  4. skozin commented on Jan 29, 2015

    @skozin
    Author

    @SvenDowideit, unfortunately this cannot be solved by just re-ordering the commands, or any other change in the output of boot2docker shellinit. That's because everything produced by an unquoted command substitution is being splitted and interpreted as one command. See my previous comment for details.

    The solution is to quote command substitution, and then pass the resulting (non-splitted) string to eval:

    eval "$(boot2docker shellinit)"

    This will require no changes in the code, only changes in the documentation.

  5. SvenDowideit commented on Jan 30, 2015

    @SvenDowideit
    Contributor

    oh. I missread the export ... unset ... version, oh boy.

  6. SvenDowideit commented on Jan 30, 2015

    @SvenDowideit
    Contributor

    ok, so

    mini:~ sven$ $(echo "
        export DOCKER_TLS_VERIFY= 
        export DOCKER_HOST=tcp://10.10.10.2:2375
        export DOCKER_CERT_PATH=
    ")
    

    seems to allow the Docker client to correctly not use TLS

    can we replace the unset's with export and get away with it?

  7. skozin commented on Jan 30, 2015

    @skozin
    Author

    Hmm, using empty strings instead of unset commands is a clever idea =)

    These can be distinguished by Bash, for example ${A:def} will not replace $A with def when A is empty (but ${A:-def} will). If Docker client treats null variables the same as unset ones, then it's probably ok.

    I think this will be more correct, though:

    $(echo "export
        DOCKER_TLS_VERIFY='' 
        DOCKER_HOST='tcp://10.10.10.2:2375'
        DOCKER_CERT_PATH=''
    ")

    because it doesn't perform unneeded export of the export variable. Or, better yet:

    $(echo "export DOCKER_TLS_VERIFY='' DOCKER_HOST='tcp://10.10.10.2:2375' DOCKER_CERT_PATH=''")

    This is completely equivalent to the previous snippet, and is easier to understand.

    UPD: According to the Go's os.Getenv() documentation, it doesn't distinguish between non-set and empty variables:

    Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.

    The related code in Docker, as expected, compares results of os.Getenv(...) calls with an empty string, so this approach seems totally safe.

  8. SvenDowideit commented on Jan 30, 2015

    @SvenDowideit
    Contributor

    @tianon should we get this into 1.5.0 too?

  9. skozin commented on Jan 30, 2015

    @skozin
    Author

    I think this is also applicable for docker machine's env command.

  10. SvenDowideit commented on Mar 3, 2015

    @SvenDowideit
    Contributor

    ok, over it, I'm implementing this atm.

  11. skozin commented on Mar 24, 2015

    @skozin
    Author

    Thanks for fixing this! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions