How to switch between active docker machine on window - docker

I am window user and working fine with the docker on default machine. I can build images and run it perfectly. But now I have scenario where I have to run two docker-machine parallel.
I have created new docker machine from following command:
docker-machine create --driver virtualbox NAMEOFNEWMACHINE
Now when I run docker-machine ls I can see there is two docker machine running.
Then I run docker-machine ip so it gives me the IP of default machine so basically I am not able to switch from default to new dev machine on docker.
I have read docker docs & I run commands which they mentioned to switch the machine
eval "$(docker-machine env NAMEOFNEWMACHINE)"
docker-machine env NAMEOFNEWMACHINE
but after running above command it still shows me default machine ip, Therefor I cannot build my image on new machine
I am pretty new to docker so is there anyone who can help me in how to run two docker machine parallel?
Thanks

Just had the same problem on windows 7. Solved it by setting the windows DOCKER_HOST environment variable. Check your machine ip (docker-machine ls) and use the complete ip in the command:
SET DOCKER_HOST=

Related

Decision rule to use docker-machine or not on docker run

When I use docker-machine in a Windows environment (installed with docker-toolbox), every docker run command uses that docker-machine as the docker daemon.
However, when I use docker-machine in a Linux environment, which has native docker daemon installed along with docker-machine, docker run command uses native docker daemon even if there is a running docker-machine instance.
Questions are:
How does docker run command decide which daemon to use?
Are there any method to list running containers on a docker-machine instance?
For the second one, I know I can SSH to the docker-machine instance and query docker ps in it, but I want check it from outside the instance.
Thanks in advance.
The Docker Machine stack works by firing up a VM, and then setting the DOCKER_HOST environment variable to point at it. In particular, it also does the required setup to TLS-encrypt the connection and to set up a TLS client certificate to authenticate the caller. (Without this setup, a remote DOCKER_HOST is extremely dangerous.)
So: docker run and every other Docker command uses the DOCKER_HOST environment variable to decide where to run things. If DOCKER_HOST points at a Docker Machine VM, docker ps will list the containers there; you won’t usually need to docker-machine ssh (though it’s a useful tool when you really need it).
On a native Linux host it’s far easier to just directly use a local Docker daemon. If you do have both a local daemon and a docker-machine VM, you can
# switch to the Docker Machine VM
eval $(docker-machine env default)
# switch back to the host Docker
eval $(docker-machine env -u)

How can I switch between Docker machine and Docker for Mac?

Prior to running
eval $(docker-machine env remote)
I was using docker for mac and had environment context for that. However, after running that command I am now pointing to the remote docker machine. I understand how to switch between different remotes and say a local docker machine, but it is not clear to me how to switch from a docker machine back to the docker for mac engine.
Looks like you can
eval "$(docker-machine env -u)"
which I found from the github issue page.

Not use docker-machine

I used docker with docker-machine ( can access container server by 192.168.99.100 ). I would like not to use docker-machine. so I can directly access my container by localhost (127.0.0.1). I shut down docker-machine (docker-machine stop) and tried to build image and container, but It said 'no daemon'. how should I completely shut down docker-machine and use local docker?
I think what you want is unset all docker-machine environment variables to use you host Docker daemon. This can be achieved with this command.
eval $(docker-machine env -u)
There are two different installs for docker on Mac. Both use a VM running Linux under the covers.
The older method includes docker toolbox and docker machine to manage the VM in virtualbox. When you use docker machine to stop this VM, the docker commands have no host to run on and will error out as you've seen.
The newer install uses xhyve to run the VM and various other tricks to make it appear seamless. This is a completely different install that you download and run from Docker, and it requires your Mac be at least version 10.10.3 with Yosemite.
See this install page for more details: https://store.docker.com/editions/community/docker-ce-desktop-mac?tab=description

docker-machine and vagrant (in regards of mount volumes)

Say I have a virtual box virtual machine provisioned through Vagrant. I then provision it with docker-machine - so far all good: I can docker-machine ssh into the box and list it ok with docker-machine ls.
In the past, when not yet using dokcer-machine, my usual workflow would involve sshing into the virtual box, installing docker and spinning up my containers.
As far as I understand this is not longer needed as I can control docker containers within the virtual box through docker-machine (and docker itself) from outside the virtual box (essentially from my win dev machine).
Question: how can I mount directories from inside the vm into the container when I am running the docker command from outside the container?
Example to further clarify:
1) old approach. ssh into vbox and run
docker run -i -t --net=try-net \
--name XXXX \
-v ${PWD}/xxxx/yyyy.py:/zzzzz/xxxx/yyyy.py \
-d me/image
2) docker-machine approach. I switch the docker-machine env onto the box. Now how do I reference a folder in the vbox from outside the box? Is this even possible?
From my win host in a Linux like shell:
docker run -v /c/x/y/z:/home --name postgres3 -d postgres:9.5
gets me:
c:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Invalid bind mount spec "c:\x\y\z\;C:\Program Files (x86)\Git\home": invalid mode: \Program Files (x86)\Git\home.
If you spin up containers using a docker-toolbox install, the VM's are pre-configured to share the /Users folder from the host into the VM which can then be used by containers.
Since you're doing this manually with your own Vagrant install, you'll need to share the folders yourself. This question should walk you through the steps to share a folder from the parent OS into the VM which can be used by Docker containers you spin up with docker-machine.
Edit: with the parent OS synced the the VM, any containers you run inside the VM will just mount volumes there. Docker-machine isn't really a factor, it's just pointing the docker CLI to the selected docker host. The docker CLI would look like:
docker run -v /path/on/vm:/path/in/container image

What does Docker Quickstart Terminal do?

I just recently started using Docker. I'm able to run my servers, and communicate between them.
What I don't understand is: why do I need to run Docker commands, like $ docker run somerepo/image from the window opened by Docker Quickstart Terminal.
Running it from "regular" Terminal windows returns
$ docker run dockerinaction/hello_world
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'.
I understand that on OS X and Windows, Docker needs to start one virtual machine with Linux 2.6 (in my case), and that all containers are run within that VM.
I thought docker CLI was connecting to a docker deamon that runs inside that VM - and then I don't understand what happens in the Quickstart Terminal.
I don't understand what does happen in the Quickstart Terminal.
You can use a regular terminal, provided by docker-machine env, and do operations similar to osx/mpkg/quickstart.app/Contents/Resources/Scripts/start.sh:
dockerm-machine start dev
eval "$(docker-machine env dev)"
(replace 'dev' with the name of your docker machine. By default, it is named... "default")
Once those environment variables for the Docker client are set, you can execute docker command directly from your shell.
A Quickstart Terminal would set those same variable for you.
You can see what it does by watching the terminal output. In my case it ran bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh' attempting to start the virtual machine. Once it's running it uses the equivalent of eval "$(docker-machine env default)" to set some environment variables so your terminal is ready to access the Docker VM.
If you need more information have a look at that script start.sh.
What does docker quickstart terminal do?
From Docker docs:
It will create and start a VirtualBox VM running Docker Engine, then
configure the command-line environment so that you can talk to it
In other words it sets some environment variables so your terminal is ready to access the Docker VM. - (thanks to #Nauraushaun)

Resources