dockerdocker-daemondocker-exec

Can I Run Docker Exec from an external VM?


I have a group of docker containers running on a host (172.16.0.1). Because of restrictions of the size of the host running the docker containers, I'm trying to set up an auto-test framework on a different host (172.16.0.2). I need my auto-test framework to be able to access the docker containers. I've looked over the docker documentation and I don't see anything that says how to do this.

Is it possible to run a docker exec and point it to the docker host? I was hoping to do something like the following but there isn't an option to specify the host.:

docker exec -h 172.16.0.1 -it my_container bash

Should I be using a different command?

Thank you!


Solution

  • Not sure why there is need of doing docker exec remotely. But anyways it is achievable.

    You need to make sure your docker daemon on your host where your containers are running is listening on a socket.

    Something like this:

    # Running docker daemon which listens on tcp socket
    $ sudo dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
    

    Now interact with the docker daemon remotely from external VM using:

    $ docker -H tcp://<machine-ip>:2375 exec -it my-container bash
    OR
    $ export DOCKER_HOST="tcp://<machine-ip>:2375"
    $ docker exec -it my-container bash
    

    Note: Exposing docker socket publicly in your network has some serious security risks. Although there are other ways to expose it on encrypted HTTPS socket or over the ssh protocol.

    Please go through these docs carefully, before attempting anything:

    https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-socket-option

    https://docs.docker.com/engine/security/https/