简体   繁体   English

zsh函数在OSX上设置docker环境

[英]zsh function to set docker environment on OSX

I am trying to create a function in zsh to run the command docker-machine env <machine> for the current machine if there is only one machine running. 我试图在zsh中创建一个函数以在只有一台机器在运行的情况下为当前机器运行命令docker-machine env <machine> I am trying to get it to run the following eval command: 我试图让它运行以下eval命令:

eval "$(docker-machine env default)"

To set the docker environment variables, running docker-machine env default will output the following: 要设置docker环境变量,运行docker-machine env default将输出以下内容:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/chris/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval "$(docker-machine env default)"

So, to set the environment correctly, you need to enter eval "$(docker-machine env default)" 因此,要正确设置环境,您需要输入eval "$(docker-machine env default)"

I have the following function 我有以下功能

dmset() {
    COUNT=`docker-machine ls | grep "Running" | wc -l`
    if [[ "${COUNT// /}" == "1" ]]
    then
        RUNNING_MACHINE=`docker-machine ls | grep "Running" | awk '{print $1;}'`
        COMMAND="\"\$(docker-machine env $RUNNING_MACHINE)\""
        eval $COMMAND
        echo Setting docker environment for $RUNNING_MACHINE
    else
        echo "Unable to set Docker environment"
    fi
}

but this is not working. 但这不起作用。 I get the following error message: 我收到以下错误消息:

(eval):1: no such file or directory: export DOCKER_TLS_VERIFY="1"\nexport DOCKER_HOST="tcp://192.168.99.100:2376"\nexport DOCKER_CERT_PATH="/Users/chris/.docker/machine/machines/default"\nexport DOCKER_MACHINE_NAME="default"\n# Run this command to configure your shell: \n# eval "$(docker-machine env default)"

how can I run the eval command to set the environment via a single script? 如何通过单个脚本运行eval命令设置环境?

Following @Etan Reisner's comment, I changed it to: 按照@Etan Reisner的评论,我将其更改为:

dmset() {
    COUNT=`docker-machine ls | grep "Running" | wc -l`
    if [[ "${COUNT// /}" == "1" ]]
    then
        RUNNING_MACHINE=`docker-machine ls | grep "Running" | awk '{print $1;}'`
        while IFS= read -r line
        do
            if [[ $line == 'export'* ]]
            then
                eval $line
            fi
        done < <(docker-machine env $RUNNING_MACHINE)
        echo "Docker environment set for $RUNNING_MACHINE"
    else
        echo "Unable to set Docker environment"
    fi
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM