Docker Community Forums

Share and learn in the Docker community.

Invoking containers from a container

docker

(Abrahamjaison) #1

Hey all,

I’m curious to know whether we can invoke docker containers from other docker containers…!
For example, say I have a container DocCont1, and when I run this using
docker run -it DocCont1
I want to start another container, say DocCont2, automatically based on certain conditions, just like calling user defined functions in C/C++/Java programming.
Is this possible? Some insights into this will be helpful!

Thanks…!


(Nathan Le Claire) #2

Only if you have access to a running instance of Docker somehow.

Remember: Access to Docker is root-level access. So, any process (including containers) which have access to run Docker commands, have root-level permissions on whichever host the daemon is running.

That said, you have two options:

  1. Run Docker-in-Docker – see the instructions there for details
  2. Bind-mount the Docker socket at /var/run/docker.sock into the container and communicate with the original Docker daemon using this network socket. So, for instance, you could also bind mount the Docker binary from the host (if it’s statically compiled), and use that:
$ docker run -ti -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):$(which docker) \
    debian \
    docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED                  STATUS                  PORTS               NAMES
fd76985d9dad        debian              "docker ps"         Less than a second ago   Up Less than a second                       nauseous_thompson

In the case of invoking from C++ or Java code, you’d probably find client bindings for your language (which directly access the Docker API using HTTP-ish), and connect to that socket.


(Nathan Le Claire) #3

EDIT: Corrected the link to run Docker-in-Docker.