Reputation: 2154
I have a docker-compose file that contains 2 services. 1 Web UI 2 Backend
In web UI I have a button that needs to restart the Backend service. How can I tell the host OS to restart the Backend service or Is there any method to restart 1 service from another docker-compose service without involving the host OS?
Upvotes: 1
Views: 605
Reputation: 311506
What @DavidMaze said in the comments ("You can't do this without giving the frontend unrestricted root-level access over the system") is true if you only consider restarting the container via the Docker API, but there are other options.
If you run your frontend and backend containers in the same PID namespace, then your frontend can simply call the kill
system call or the kill
command line tool to stop the backend processing, causing the backend container to restart (assuming an appropriate restart policy).
That might look something like the following if you're using docker-compose
:
version: "3"
services:
frontend:
...
backend:
...
pid: "service:frontend"
restart: always
In this model, a process in the frontend
container can kill the
primary process in the backend
container, and Docker will restart
the backend because of the restart: always
policy.
You can
give your backend an API endpoint that causes the main process to exit
(against relying on the restart policy to restart the service). E.g., your frontend makes a call to http://backend/quit
, causing the backend container to terminate.
You can arrange to share a volume between the two containers and have your backend code watch for a particular to show up, and if does (a) remove the file and then (b) exit (again relying on the restart policy to restart the service).
Upvotes: 1