0

I have a Pid 1 problem. Ok so in order to explain i need to focus on my formulation of the problem.

I have a service which is depended on a hostid and a license file generated to match the hostid in order to run. How the hostid is genereted is unknown to me. If the service does not have a valid license the prosess shuts down. So im unable to containerize just this simple service.

But what if I a have another process running first, like an API to set the license file, and to query for hostid. Then this api can set the license file in place. But now to the tricky part, how can I switch the process running PID 1? Cause the service needs to be run as PID 1.

I was thinking of abbreviating with the PID 1 beeing a bash loop which first starts the API, then when the API exits starts the service.

Would this be possible?

And how would you create the bash loop?

BMitch
  • 231,797
  • 42
  • 475
  • 450
Paal Pedersen
  • 1,070
  • 1
  • 10
  • 13

1 Answers1

1

The C execve(2) function replaces the current process with a new one; the new process keeps properties like the effective user ID and it has the same process ID. The Bourne shell includes an exec built-in that does the same thing.

A common pattern in a Docker image is to use an entrypoint wrapper script to do first-time setup. If a container has both an entrypoint and a command, the command gets passed as arguments to the entrypoint. So you can write a script like:

#!/bin/sh

# Do whatever's needed to get the license
/opt/myapp/bin/get_license

# Then run the command part
#   exec replaces this script, so it will have pid 1
#   "$@" is the command-line arguments
exec "$@"

In the Dockerfile, set the ENTRYPOINT to this wrapper, and the CMD to run the real service.

# Run the script above
# ENTRYPOINT must have JSON-array syntax in this usage
ENTRYPOINT ["/opt/myapp/bin/start_with_license"]

# Say the normal thing you want the container to do
# CMD can have either JSON-array or shell syntax
CMD ["/opt/myapp/bin/server", "--foreground"]
David Maze
  • 130,717
  • 29
  • 175
  • 215