CMD [ "/bin/bash", "-c", "dotnet /Project/Api.dll" ]
Given command works well but as your requirement is to run this later right?
You can always write your custom shell logic/processing in a separate script and run it instead of running the above command.
This will smartly assign different pid to your dotnet process.
#!/bin/bash
#some logic
dotnet /Project/Api.dll
And you will run your script like
ENTRYPOINT ["entrypoint.sh"]
This will solve your issue to a greater extent.
Lastly to end/kill your dotnet process gracefully and not use kill command which is ultra invasive.
you can use a combination of putting your dotnet app in the background
and then combining it with Linux command term
command which gives you a great exit/error hook to work upon.
Additionally there is this great thread about the pid1 scenario you are referring to:
https://unix.stackexchange.com/questions/457649/unable-to-kill-process-with-pid-1-in-docker-container
Hope this helps.