Vincent
Vincent

Reputation: 1604

Properly automate a docker script in Python

Based on this tutorial to build a TF image classifier, I have a bash shell in which I run a Docker image with the following command:

docker run --name fooldocker -it -v $HOME/tf_files:/tf_files/ gcr.io/tensorflow/tensorflow:latest-devel

And then in this docker image I run my Python script:

python /tf_files/label_image.py /tf_files/myimages
exit

It works.

But now, I need to automate these commands in a Python script. I tried :

p = Popen(['docker', 'run', '--rm', '--name', 'fooldocker','-it', '-v', '$HOME/tf_files:/tf_files/', 'gcr.io/tensorflow/tensorflow:latest-devel'], stdout=PIPE)
p = Popen(['docker', 'exec', 'fooldocker', 'python', '/tf_files/label_NES.py', '/tf_files/NES/WIP'])
p = Popen(['docker', 'kill', 'fooldocker'], shell=True, stdout=PIPE, stderr=PIPE)
p = Popen(['docker', 'rm', 'fooldocker'], shell=True, stdout=PIPE, stderr=PIPE)

Leading to this error after Popen #2 is run :

docker: Error response from daemon: create $HOME/tf_files: "$HOME/tf_files" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.

Upvotes: 0

Views: 316

Answers (2)

CSJ
CSJ

Reputation: 2957

it is because Popen didn't interpret $HOME to your home path. and it is the string $HOME and pass to docker command which not allow $ in a volume name.

maybe you can use subprocess module for convenience, for example:

import subprocess
subprocess.call("echo $HOME", shell=True)

it interpreted $HOME if shell=True specified.

Upvotes: 0

Max Uppenkamp
Max Uppenkamp

Reputation: 974

The problem is that $HOME cannot be evaluated in this single quotes string. Either try doublequotes, or evaluate the variable beforehand and put it into the command string.

Also: If you set shell=True, you don't split your command into a list:

p = Popen('docker kill fooldocker', shell=True, stdout=PIPE, stderr=PIPE)

Upvotes: 1

Related Questions