How to spawn a docker instance for each spring boot api call and map a unique id to each docker instance?

10/4/2021

I have a bunch of api calls in my spring boot service. Let's say,

http://localhost:8080/me/customize
http://localhost:8080/me/addItem
http://localhost:8080/me/revokeItem

When the user hit's http://localhost:8080/me/customize api, then I have to trigger a docker instance(and execute some code inside the spawned docker instance) and provide a unique id to the user. The user will use that unique id to hit the other apis.

This process should happen for all the users. Let us say that 100 users hit the customize api, i need to have 100 docker instances with a unique identifier tied to each docker instance and the user should use that unique id to communicate with the docker instance from there onwards.

Do I need to write java code to handle the docker spawning and mapping the unique id to each docker instance?

I am a tester and this is my first time dealing with development stuff. Could any of you point me in the right direction or some sort of tutorial may be? I have done googling, and still didn't found any answer.

-- Kishore Mohanavelu
docker
java
kubernetes
spring-boot

2 Answers

10/4/2021

Do I need to write java code to handle the docker spawning and mapping the unique id to each docker instance?

Ideally you can but managing that would be hard if you use Kubernets job any other way. Using deployment would be easy however i would not suggest going that way. rather implement the Pub/sub or queue messaging if it's fine.

I have to trigger a docker instance(and execute some code inside the spawned docker instance) and provide a unique id to the user.

i not sure what is the exact requirement or reason behind spawning new instance each time.

You can create the Pub/Sub or queue system when user will call the API it will the add payload into queue and further worker PODs will use that payload and process it one by one.

You can store the outcome or ID into Redis or MySQL and further use by other services or application if you need.

You can also use a queueing systems like RabbitMQ or a library like https://github.com/OptimalBits/bull.

You can create multiple worker replicas and they will subscribe to a single queue or channel and process the payload and save ID to database like Redis.

If there is a specific requirement of running replica for each process write code which will spwan the POD one by one may creating different deployment or Jobs.

-- Harsh Manvar
Source: StackOverflow

10/4/2021

I have to trigger a docker instance(and execute some code inside the spawned docker instance) and provide a unique id to the user

this is docker run --rm codegeneratorimage:v1.0 /bin/sh -c "your code to generate the unique id"

I don't quite understand what you are testing though, do you want to test the app running behind http://localhost:8080/me/customize? Why not test the endpoint?

Anyway, for triggering that docker run you have many options:

  1. If you are in cloud environment, they have something like AWS lambda which you can setup to have something like http://localhost:8080/me/customize and trigger the docker run
  2. Write a golang webservice and you can easily run that docker run command https://stackoverflow.com/questions/6182369/exec-a-shell-command-in-go + https://golang.org/doc/tutorial/web-service-gin (recommended). You can also write a python webservice for this if you are more familiar.. or other PLs
-- letthefireflieslive
Source: StackOverflow