Home > Net >  Reboot Docker container from inside
Reboot Docker container from inside

Time:12-02

I'm working with a Docker container with Debian 11 inside and a server. I need to update this server and do other things on regular manne. I've written several scripts that can do it, but I encountered serious proble. If I want to update the server and other packages I need to reboot the container. I'm obviously able to do so from the computer Docker is installed on (in my case Docker Desktop running with WSL2 on Windows 10), I can reboot the container easily, but I need to automate it. The simplest way will be to add the shutdown command to the scripts I've written. I was reading about it, but found nothing. Is there any way to reboot this container from the Debian inside it? If no, how can it be achieved and how complicated is it?

I was trying to invoke standard Linux commands to shutdown or reboot system on Debian inside container. I expect a guide if it's possible and worth efforts.

CodePudding user response:

The only way to trigger a restart of a container from within the container is to first set a restart policy on the container such as --restart=on-failure and then simply stop the container, i.e., let the main process terminate itself. The Docker engine would then restart the container.

This, however, is not the way Docker is intended to be used! Docker containers are not VMs and instead are meant to be ephemeral:

By "ephemeral", we mean that the container can be stopped and destroyed, then rebuilt and replaced with an absolute minimum set up and configuration.

This means, you shouldn't be updating the server within a running container but instead should update/rebuild the image and start a new container from it!

  • Related