1

I have a java app.

I need to start a script from this app.

If I execute the command to start the script directly in terminal, I get the desired result, like:

root@host [ ~ ]# docker exec -it mycontainer bash
root [ /mycontainer ]# nsenter -t 1 -m -u -n -i sh /gg/my.sh -c telegraf -a status
status      #some custom output from the script
telegraf
**************
207

If I start exactly the same thing from the Java app I don't get the correct result. (The result is as if I execute the script inside the container.) Java code:

 String command = "nsenter -t 1 -m -u -n -i sh /gg/my.sh -a " + action + " -c " + name;
 Process ps = Runtime.getRuntime().exec(command);
...
log.info(psOutput)

Log:

not found
**************
not implemented.

Full context:

  • the Java app is in docker container
  • I need to run the script on the host

yes, I know.. process isolation and docker containers and etc, sometimes you just have to trigger the execution of a set of commands on the host from the container. I trigger the execution of the script from the container onto the host following this.

Can someone explain to me why? What exactly happens when I execute my command from the Java app? Why does it feel like the process which is started from the Java app is sort of wrapped in another process, maybe?

Toto
  • 89,455
  • 62
  • 89
  • 125
gai-jin
  • 653
  • 2
  • 10
  • 24

1 Answers1

0

The process has been executed on the container.

Because the container has not been started with the correct arguments to allow the execution on the host.

In order to execute the script on the host one must lift the privileges of the container, triggering the script, and to add PID for the process that should execute the script - --privileged --pid=host

Something like:

docker container create --name=my-app --restart=always \
 --privileged --pid=host \ # <---here is the important part
my-app-image:01 
gai-jin
  • 653
  • 2
  • 10
  • 24