Search code examples
dockerdocker-machine

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.


Solution

  • 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)