[SOLVED] hookscripts on container to gracefully start/stop docker images inside containers

peter70

New Member
Oct 31, 2021
9
1
3
54
I try to execute hookscript.
I successfully added hookscript from documentation /usr/share/pve-docs/examples/guest-example-hookscript.pl and I see it's beeng executed in gui taks log:
Code:
GUEST HOOK: 112 pre-start
112 is starting, doing preparations.
GUEST HOOK: 112 post-start
112 started successfully.
TASK OK
So it's working.

Now I want to execute command inside this container so I've added this command inside my hookscript:
Code:
system "pct exec 112 -- bash -c 'touch testfile'";
but it's not creating no file inside container 112

How can I run script inside container just before shuting it down or after start?
 
Last edited:
bump, no one is using hookscripts?
It would be awesome to know how to execute script inside container before stop.
 
please post the full hook script..
 
please post the full hook script..
Thank you, I must have made some error because it's working now.

Anyway here's code how to gracefully start/stop docker images inside containers:
Perl:
#!/usr/bin/perl

# Exmple hook script for PVE guests (hookscript config option)
# You can set this via pct/qm with
# pct set <vmid> -hookscript <volume-id>
# qm set <vmid> -hookscript <volume-id>
# where <volume-id> has to be an executable file in the snippets folder
# of any storage with directories e.g.:
# qm set 100 -hookscript local:snippets/hookscript.pl

use strict;
use warnings;

print "GUEST HOOK: " . join(' ', @ARGV). "\n";

# First argument is the vmid

my $vmid = shift;

# Second argument is the phase

my $phase = shift;

if ($phase eq 'pre-start') {
    # First phase 'pre-start' will be executed before the guest
    # is started. Exiting with a code != 0 will abort the start

    #print "$vmid is starting, doing preparations.\n";

    # print "preparations failed, aborting."
    # exit(1);

} elsif ($phase eq 'post-start') {
    # Second phase 'post-start' will be executed after the guest
    # successfully started.

    #print "$vmid started successfully.\n";
    system "pct exec $vmid -- bash -c 'docker start \$(docker ps -a -q)'";

} elsif ($phase eq 'pre-stop') {

    # Third phase 'pre-stop' will be executed before stopping the guest
    # via the API. Will not be executed if the guest is stopped from
    # within e.g., with a 'poweroff'
    system "pct exec $vmid -- bash -c 'docker stop \$(docker ps -q)'";
    
    print "$vmid will be stopped.\n";

} elsif ($phase eq 'post-stop') {
    # Last phase 'post-stop' will be executed after the guest stopped.
    # This should even be executed in case the guest crashes or stopped
    # unexpectedly.

    #print "$vmid stopped. Doing cleanup.\n";

} else {
    die "got unknown phase '$phase'\n";
}
 
  • Like
Reactions: fabian

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!