I need to have bash shell commands run through python in order to be universal with pc and mac/linux. ./bin/production
doesn’t work in powershell and putting ‘bash’ in front would give an error that it doesn’t recognize ‘docker’ command
./bin/production contents:
#!/bin/bash
docker run --rm -it
--volume ${PWD}/prime:/app
$(docker build -q docker/prime)
npm run build
This is the python script:
import subprocess
from python_on_whales import docker
cmd = docker.run('docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
print(out)
This is the error I get when running the python script:
python_on_whales.exceptions.NoSuchImage: The docker command executed was C:Program FilesDockerDockerresourcesbindocker.EXE image inspect docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build
.
It returned with code 1
The content of stdout is ‘[]
‘
The content of stderr is ‘Error response from daemon: no such image: docker run –rm -it –volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build: invalid reference format: repository name must be lowercase
‘
Running the command, docker run --rm -it--volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build
in one long line in powershell works but we want a universal standard command for both pc and mac/linux
2
Answers
This might not be what you’re looking for but you can always try this:
Edit:
I’m intermediate with python so don’t judge, if I did something wrong please give me feedback. Thanks.
The Python on Whales
docker.run()
function doesn’t take adocker run ...
command line. It is a native Python API where you need to express the various Docker options as function parameters.In principle you could rewrite this Python script using that API:
The return value from
docker.run()
(withoutdetach=True
) is the container’s stdout, and the examplesprint()
that data.