Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't run Docker command via ssh and python's subprocess module

I am trying to automatically run a docker build command using the subprocess module as such:

command = "docker build -t image_name ."

ssh_command = "ssh -o 'StrictHostKeyChecking=no' -i 'XXXXX.pem' ubuntu@" + cur_public_ip + " " + command

retval = subprocess.run(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

if retval.stderr != '':
    print('Error trace: ')
    print(retval.stderr)
else:
    print("Docker image succesfully built.")
    print(retval.stdout)

Interestingly, if I run this command (the string that is the command variable) after I manually SSH into my ec2 instance, it works fine.

But when I run the code above, I get this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error trace: 
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I can’t seem to solve this problem, and I am stuck since I don’t see how what I am doing is different from manually sshing into the instance and running the command.

The docker daemon is definitely running since I can build manually through an ssh terminal. I’ve tried changing the rwx permissions of the Dockerfile and all related files on the ec2 instance, but that did not help as well.

How do I make this work? I need to programmatically be able to do this.

Thank you.

>Solution :

Your first problem is that you’re only passing command to subprocess.run, so you’re running docker build locally:

                        +--- look here
                        |
                        v
retval = subprocess.run(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

Your second problem is that you’ve got way to much quoting going on in ssh_command, which is going to result in a number of problems. As written, for example, you’ll be passing the literal string 'StrictHostKeyChecking=no' to ssh, resulting in an error like:

command-line: line 0: Bad configuration option: 'stricthostkeychecking

Because you’re not executing your command via a shell, all of those quotes will be passed literally in the command line.

Rather than calling command.split(" "), you would be better off just building the command as a list, something like this:

import subprocess

cur_public_ip = "1.2.3.4"
command = ["docker", "build", "-t", "image_name", "."]

ssh_command = [
    "ssh",
    "-o",
    "stricthostkeychecking=no",
    "-i",
    "XXXXX.pem",
    f"ubuntu@{cur_public_ip}",
] + command

retval = subprocess.run(
    ssh_command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True,
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading