Not able to redirect container logs to file using subprocess.run

Question:

I have a python script to start some containers, wait for them to finish execution and then start a few others. I wanted to get the container logs and this bash command worked for me:

docker logs -f container-name &> tmp.log &

However when I try to add it to my python script using `subprocess.run like below, it doesn’t create a new file.

subprocess.run(
     [
         "docker",
         "logs",
         "-f",
         "container-name",
         "&>",
         "tmp.log",
         "&"
     ],
     cwd=os.getcwd(),
     shell=False,
     stdout=subprocess.DEVNULL,
 )
Asked By: Tony Stark

||

Answers:

You are using shell redirection (&> and &) which are not recognized by subprocess.run() due to setting shell=False.

Although setting shell=True could be a solution to resolve this issue, it should be used with caution due to the potential security risks involved (See: Actual meaning of ‘shell=True’ in subprocess).

A safer approach would be to just create and/or open the file and redirect the output to it:

import subprocess
import os

log_file = "tmp.log"
if not os.path.exists(log_file):
    open(log_file, "w").close()

with open(log_file, "a") as outfile:
    subprocess.run(
        ["docker", "logs", "-f", "container-name"],
        cwd=os.getcwd(),
        shell=False,
        stdout=outfile,
    )
Answered By: eDonkey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.