nysus has asked for the wisdom of the Perl Monks concerning the following question:

Got this docker command I want to run. Note the tail command at the end for keeping the container up and running.

docker run -d --name xxxyyyzzz-blah alpine /bin/ash -c "/usr/bin/tail +-f /dev/null"

The following backtick operation runs this command with no issue and the container is detached from and the container is left still running:

use v5.38; `docker run -d --name xxxyyyzzz-blah alpine /bin/ash -c "/usr/bin/tail + -f /dev/null"`;

However, with the IPC::Run3 module, the container gets set up, but the tail command never seems to get executed and the container exits immediately:

use v5.38; use IPC::Run3; my $cmd = [ 'docker', 'run', '-d', '--name', 'xxxyyyzzz-blah', 'alpine +', '/bin/ash', '-c', '"/usr/bin/tail -f /dev/null"' ]; my ($out, $err); my $success = run3($cmd, undef, \$out, \$err); print $success; # prints 1

I tried breaking up the the last element into 3 different elements separated by spaces but that didn't help.

$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: IPC::Run3 not working as expected when running Docker command
by nysus (Parson) on Apr 22, 2024 at 20:05 UTC

    OK, just figured it out. Removing the double quotes from the last element gets things working as expected:

    my $cmd = [ 'docker', 'run', '-d', '--name', 'xxxyyyzzz-blah', 'alpine +', '/bin/ash', '-c', '/usr/bi/tail -f /dev/null' ];

    $PM = "Perl Monk's";
    $MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
    $nysus = $PM . ' ' . $MC;
    Click here if you love Perl Monks

Re: IPC::Run3 not working as expected when running Docker command
by NERDVANA (Priest) on Apr 23, 2024 at 19:23 UTC

    I have to say, whatever you're trying to do here seems to be the wrong way to accomplish it.

    If you want a docker container to run in the background, I would think you'd want "docker create". The only advantage of "run -d" is that it doesn't exist after reboot. Is that really all you want? If you want it to only run for the duration of your perl script, then why use "-d"? You can just keep the IPC::Run instance open in the background.

    Second, why run /bin/sh? You could just [ 'docker', 'run', '-d', '--name', 'xxxyyyzzz-blah', 'alpine', '/usr/bin/tail', '-f', '/dev/null' ]

    Third, if you are creating this container so that you can then 'docker exec' various things in it, you probably want the '--init' option so that there is a proper PID 1 inside the container to reap processes and respond to signals.

    Fourth, if your goal is, in fact, to exec things in a shared container, you might consider just running each process in its own container on a shared filesystem. Linux namespaces are fairly cheap, and the "docker-correct" way of approaching the problem is to run each process with its own "docker run" command.