How can I spin a docker cotainer per api call?

200 Views Asked by At

My teammates and I are working on a backend, which would provide ros gazebo simulation that is hosted online. Due to the fact that it is very hard to let several instance of ros running on the same machine, we have decided that we will have a container running for each instance of ros gazebo. However, it there any tool to orchestrate the containers (running, stopping)? It's not like kubernetes style of managing containers, but like I need to spin a container per api call. Thanks in advance!

1

There are 1 best solutions below

2
On BEST ANSWER

You can use curl to consume Docker API. Reference's documentation: https://docs.docker.com/engine/api/sdk/examples/#run-a-container

curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" \
  -d '{"Image": "alpine", "Cmd": ["echo", "hello world"]}' \
  -X POST http://localhost/v1.41/containers/create
 curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.41/containers/1c6594faf5/start
 curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.41/containers/1c6594faf5/wait
 curl --unix-socket /var/run/docker.sock "http://localhost/v1.41/containers/1c6594faf5/logs?stdout=1"

Where v1.41 is the API version of Docker. To check what version you are running just execute docker version

This way you have to make these calls from the host executing the Docker process though. If you need to execute remote calls to the host, the only thing you can do is to write a small executable that exposes a web interface that you can use to call internal Docker API (https://docs.rs/docker-api/latest/docker_api/)