<p>I have a web API running on a Docker container. I want to implement a solution in Kubernetes/DockerSwarm such that a new instance of the container is created everytime the API is called. How can that be achieved ?</p>

Satya Prakash
<p>For this you need to control the <code>docker swarm</code> from inside the container itself. This can be done in two steps:</p>

<ol>
<li><p>install the docker inside the container. This depends on the container image. A solution can be like this:</p>

<p>RUN apt-get -yqq update &amp;&amp; apt-get -yqq install docker.io 56 &amp;&amp; usermod -g docker jenkins</p></li>
<li><p>mount the docker socket inside the container. For example, if you start the container manually, you need something like this:</p>

<p>docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker ubuntu:latest bash</p></li>
</ol>

<p>Now, you can run <code>docker</code> commands inside the container. You can, for example, inspect a service or scale it to a bigger number of replicas.</p>

<p><strong>You should note however that this solution is not very safe, if someone gains access to your container then it has access to the entire swarm, so be aware!</strong></p>
