Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the docker commands from python?

Tags:

python

docker

I want to run a set of docker commands from python. I tried creating a script like below and run the script from python using paramiko ssh_client to connect to the machine where the docker is running:

#!/bin/bash

   # Get container ID
   container_id="$(docker ps | grep hello | awk '{print $1}')"

   docker exec -it $container_id sh -c "cd /var/opt/bin/ && echo $1 && 
   echo $PWD && ./test.sh -q $1"

But docker exec ... never gets executed.

So I tried to run the below python script below, directly in the machine where the docker is running: import subprocess

docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd 
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)

I get a message: "Usage: docker COMMAND..."

But I get the expected results if I run the command

docker exec 7f34a9c1b78f /bin/bash -c "cd /var/opt/bin/ && ls -a" directly in the machine

How to run multiple docker commands from the python script? Thanks!

like image 600
Uma Senthil Avatar asked Oct 19 '25 11:10

Uma Senthil


1 Answers

You have a mistake in your call to subprocess.call. subprocess.call expects a command with a series of parameters. You've given it a list of parameter pieces.

This code:

docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd 
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)

Runs this:

subprocess.call([
   'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c', 
   '"cd', '/var/opt/bin/', '&&', 'ls', '-a"'
 ], shell=True)

Instead, I believe you want:

subprocess.call([
   'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c', 
   '"cd /var/opt/bin/ && ls -a"' # Notice how this is only one argument.
 ], shell=True)

You might need to tweak that second call. I suspect you don't need the quotes ('cd /var/opt/bin/ && ls -a' might work instead of '"cd /var/opt/bin/ && ls -a"'), but I haven't tested it.

like image 111
cwallenpoole Avatar answered Oct 21 '25 03:10

cwallenpoole