Within MacOS, I have created 2 docker machines, say, dev1 and dev2.
In one terminal running $docker-machine active shows dev1 as an active docker-machine and in the other, dev2. Now I want to switch to dev2 in the 1st terminal (without stopping/removing etc. dev1) so that I'll have dev2 in both.
How do I do this? Thanks!
run command in your terminal eval $(docker-machine env [machine-name])
Run docker-machine ls to get available machines list
So I have been researching on this for some time and what I found is that I have to run $eval "$(docker-machine env dev2)" in Terminal 1.
You can do this with the docker-machine env command. For example:
$ eval "$(docker-machine env <machine-name>)"
This will set environment variables that the Docker client will read which specify the TLS settings. Note that you will need to do that every time you open a new tab or restart your machine.
To see what will be set, run docker-machine env
$ docker-machine env <machine-name>
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.16.62.130:2376"
export DOCKER_CERT_PATH="/Users/<your username>/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
Related
Environment variables set outside the shell, through ~/.bash_profile or ~/.bashrc do not appear to docker, or env, despite being accessible in the shell.
Bash_profile contains the line TEST_ENV_VAR=123, after restarting terminal, the variable can be accessed through $TEST_ENV_VAR, however docker and env cannot access this environment variable.
Henrys-MacBook-Pro:~ henry$ echo $TEST_ENV_VAR
123
Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR ubuntu env | grep TEST_ENV_VAR
Henrys-MacBook-Pro:~ henry$ env | grep TEST_ENV_VAR
And yet the terminal can access it, and even pass it into docker:
Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR=${TEST_ENV_VAR} ubuntu env | grep TEST_ENV_VAR
TEST_ENV_VAR=123
And the issue isn't an issue with environment variable in general, as variables set in the terminal work as expected:
Henrys-MacBook-Pro:~ henry$ export TEST_ENV_VAR=1234
Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR ubuntu env | grep TEST_ENV_VAR
TEST_ENV_VAR=1234
I'm running macOS Mojave, 10.14.5, classic terminal, docker 19.03.4, the output of ls -al ~:
-rw-r--r-- 1 henry staff 455 Nov 12 11:50 .bash_profile
docker doesn't actually start a container. Instead, it sends a message to the Docker engine (running in a separate environment unrelated to the environment in which docker is executed) requesting that it start a container for you. As such, the new container won't inherit any of variables in your current shell environment.
With your TEST_ENV_VAR=${TEST_ENV_VAR} attempt, you are explicitly telling docker to create an environment variable named TEST_ENV_VAR, with the value produced by expanding TEST_ENV_VAR now, in the new container, rather than trying to inherit the variable from the appropriate environment.
Even ignoring that, you aren't actually creating an environment variable with TEST_ENV_VAR=123; you've only created an ordinary shell variable. For env to see it, you need to first export it.
$ TEST_ENV_VAR=123
$ env | grep TEST_ENV_VAR
$ export TEST_ENV_VAR
$ env | grep TEST_ENV_VAR
TEST_ENV_VAR=123
The .bash_profile is for your user. Docker is running in its own environment (as opposed to running in a subshell spawned by your user). Export only sends information down into child shells. You need to make the variable available at a higher level.
/etc/environment
Be careful in there.
Alternatively, you may look into asking Docker to make these changes itself.
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
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?
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]
I am running Docker Machine on Mac.
docker-machine version 0.6.0, build e27fb87
In my shell I have done
export DOCKER_HOST=tcp://docker.local:2375
export DOCKER_TLS_VERIFY=0
Restarted the machine
When I do
docker-machine env
These do not seem to have been set. I am using the ZSH shell, could this be an issue?
docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/ciaran/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
When you run docker-machine env it will show the variables that are needed in order to connect to the default machine. It has nothing to do with the variables in the current shell, and will not set any variables in your shell.
To see the current docker variables in your shell, you can run
$ env | grep DOCKER
If you want to set your shell ENV variables to the ones in docker-machine env, you will need to either copy and paste the output of docker-machine env, or eval the output like this.
$ eval $(docker-machine env <machine name>)
That will set the variables in your shell. This command is actually given to you when you run docker-machine env look at the end of the output.
To confirm it worked, check the shell again.
$ env | grep DOCKER
On OSX, when running a docker container and mounting the current directory, the expression $(pwd) does not get expanded:
bash
docker run -v $(pwd):/var/mydir image
On the container, /var/mydir remains empty.
I have installed bash-completion via homebrew, and the docker-specific bash completion also.
I see examples of using $(pwd) on OSX all over the place, and yet I cannot make it work.
Please note that the current directory is under /Users, e.g. /Users/joe/projects/demo
Try to test it first:
path=$(pwd); echo ${path}
If that work, then you can add the docker command:
path=$(pwd); docker run -v ${path}:/var/mydir image
Also, if you are executing that command from the host shell (Mac), instead of the docker machine shell (through a docker-machine ssh session), make sure you have executed eval "$(docker-machine env <machinename>)" first.
Using ${PWD}:
docker run -v ${PWD}:/src -w /src golang:1.17 go mod download && go run main.go
I think that command redirected $ docker commands to the docker machine. Now all my docker commands are giving me an error FATA[0000] Couldn't read ca cert... follwed by the path to the docker-machine I created. How can I fix my shell?
What you are looking for is:
eval "$(docker-machine env -u)"
It will unset the DOCKER_* variables.
For the record, here's the output of docker-machine env -u:
unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
You could also restart your shell. This will drop the variables that minkube docker-env exports.
I can see that this is an old post but if someone else runs into this issue, who is new to docker like me this can help. By typing:
eval $(docker-machine env nameOfVm)
you are setting your current shell to use docker in that docker-machine. You can check if you type docker-machine ls
that under active tab, that status is changed from - to * for that machine. You can also check which machine is active by running docker-machine active.
If you want to undo eval, just run:
eval $(docker-machine env -u)
and it will unset variables for active machine (You don't have to specify the name of the machine). This is all under macOS but I think it should be same on linux as well.
You can read more about this here:
Docker documentation: docker-machine env
I had been searching for an answer to this for quite awhile. Shortly after posting the question on stackoverflow I realized typing in to the terminal the export commands docker gives on startup resolved my issue.
To connect the Docker client to the Docker daemon, please set:
export DOCKER_HOST=tcp:// some IP address
export DOCKER_CERT_PATH= some file path
export DOCKER_TLS_VERIFY=1
All you have to do is run
docker-machine env machine-name
Then, copy and run the last segment in the output to set or remove the env variables.
Which looks like this in Windows :
eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env machine-name)
If it's set already, docker adds a "-u" at the tail to make the task easy.
like this in Windows :
eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env -u)
That's all.