I am trying to run the shell command
echo -e 'FROM busybox\nRUN echo "hello world"' | docker build -t myimage:latest -
from jupyter notebook using subprocesses
I have tried the code
p = subprocess.Popen('''echo -e 'FROM busybox\nRUN echo "hello world"' | docker build -t myimage:latest - ''', shell=True) p.communicate()
and some iterations with run() or call(), but everytime the output is
-e 'FROM busybox
It seems that the new line character \n causes the problem. Any ideas to solve the problem?
\n gets parsed by Python into a literal newline. You can avoid that by using a raw string instead,
p = subprocess.run( r'''echo -e 'FROM busybox\nRUN echo "hello world"' | docker build -t myimage:latest - ''', shell=True, check=True)
but I would recommend running a single process and passing in the output from Python.
p = subprocess.run(['docker', 'build', '-t', 'myimage:latest', '-'], input='FROM busybox\nRUN echo "hello world"' , check=True)
Notice also how we prefer
subprocess.run() over the more primitive
subprocess.Popen(); as suggested in the documentation, you want to avoid this low-level function whenever you can. With
check=True we also take care to propagate any subprocess errors up to the Python parent process.
As an aside,
printf is both more versatile and more portable than
echo -e; I would generally recommend you to avoid
echo -e altogether.
Admin | 1 week ago