skip to Main Content

To give a short context: For a school project I have to build a simplistic version of AWS RDS.

Simplistic as in: instances of Postgres and Redis can be provisioned for users and these instances can be ran on 1 server / docker swarm node.

The big obstacle I am currently facing is that this application will be run inside a docker swarm, and thus inside a docker container, and I can’t find / think of a good way to create the containers for my databases on the swarm node, from inside the original container the application runs on.

So my question is as follows:

Is there a way to create docker containers on my host system, from inside a docker container on that system?

2

Answers


  1. You can map the host docker socket into the container and then – if you have the docker cli installed in the container – you can access the host’s docker environment.

    There’s a standard image available with docker installed called simply docker.

    If you run it and start a shell, like this (note the mapping of the docker socket)

    docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock docker:latest sh
    

    Now you’re inside a container, but you can run commands against the host’s docker.

    As an example you can start an nginx image with

    docker run --rm -d nginx
    

    When you exit the shell and the container, the nginx container will be running on the host.

    Login or Signup to reply.
  2. The Docker Engine API can be accessed, by mounting /var/run/docker.sock from the host into your container.

    As you are running on docker swarm it it worth noting that api endpoints that deal with containers and images and volumes will effect the current node only. but api endpoints that deal with services, stacks and tasks, MUST be executed on manager nodes, and will effect the whoel swarm.

    So if you wanted to create a container running postgres on a specific node, you would need a container yourself running on that node that you could address.

    but you could have a single container running on a manager node that created a postgres service instance, and that would, according to labels and other placement constraints, create somewhere on the swarm.

    If calling the API directly, or using curl is too much trouble, then using a docker container as per @Hans Kilians answer is the better approach, but again, your container needs to be on a manager node to perform swarm level operations.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search