0

I'm using Python to run local shell command, with "subprocess":

subprocess.call("docker exec -it transcoder /bin/bash", shell=True, stdout=output, stderr=output)
subprocess.call("ffmpeg", shell=True, stdout=output, stderr=output)

The two command above which run step by step manually will like :

#first command  executed
#docker exec -it transcoder /bin/bash  output as :
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
root@85fe13a:/# 

Those I describe it as it has changed the session context, so that the next command run within the new context, manual run next command, like:

#2nd command  executed
root@85fe13a:/# ffmpeg
ffmpeg version n4.3-dev-3911-g00afeaf548 Copyright .....

Okay, let's run in Python:

s = """
docker exec -it transcoder /bin/bash
ffmpeg
"""

subprocess.run([s], shell=True)

The output will be like:

#docker exec -it transcoder /bin/bash  output as :
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
root@85fe13a:/# 

The command 'ffmpeg' seems not to work!

I want run new command continually after first command done. Since I have test the command as below which runs well as expected:

s = """
cd /www/pathDir
ls

"""

subprocess.run([s], shell=True)

but now my case not work, what's difference between these two cases and can subprocess or Python will work in any pattern as I expected?


After days of environment's fix and fix, then the Docker library in Python installed correctly; we use it to run commands as follows:

import docker                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                            
client = docker.DockerClient(base_url='unix://var/run/docker.sock')                                                                                                                                                                                                         
c = client.containers.run('image', detach=True, tty = True,  volumes = {                                                                                                                                                                        
     '/www' : {                                                                                                                                                                                                                                  
         'bind' : '/www',                                                                                                                                                                                                                    
         'mode' : 'rw'                                                                                                                                                                                                                       
 }

print(c.logs(), c.exec_run( "ffmpeg -i /www/xxx.MP4" ))

But another way like: docker exec -it transcoder ffmpeg also works well.

python docker lib Can't open an interactive container

Thanks @David Maze !

That's all from a Docker beginner.

6
  • You are not running docker and ffmpeg sequentially in the same, local shell, rather the ffmpeg command is run in the interactive shell you get from docker -it /bin/bash. You could run ffmpeg directly instead of bash. Commented Sep 18, 2023 at 11:27
  • You probably shouldn't be using docker exec in routine work. (It brings all of the complications and permission issues around using Docker, plus it's not the "normal" path to run a container so you won't be able to see things like the process output.) Can you install ffmpeg locally and just use subprocess.call(['ffmpeg', ...])? (There are serious security problems around shell=True, avoid it if at all possible.) Commented Sep 18, 2023 at 11:27
  • Or, if you do have Docker, and you have the unrestricted root-level permissions required to use it, consider using the Docker SDK for Python to create a temporary container that runs the image with ffmpeg in it. Commented Sep 18, 2023 at 11:28
  • thanks . I've tried Docker Library in python,but from_env() issued error, so I try this.. Commented Sep 19, 2023 at 6:15
  • maybe my case is relation to the question docker-interactive-mode-and-executing-script, i'm not sure, I'll have a test ---- Then , result : not according to mine Commented Sep 20, 2023 at 1:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.