bash shell commands run through python to be universal with windows, mac, and linux

Question:

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

Asked By: Codethisstuff99

||

Answers:

This might not be what you’re looking for but you can always try this:

import platform
import subprocess
import os

cur_os = platform.system()

if cur_os == "Windows":
    print("You are on windows")
    os.system('Command here') # for windows
elif cur_os == "Darwin":
    print("You are on mac")
    subprocess.call('Command goes here') # for mac

Edit:
I’m intermediate with python so don’t judge, if I did something wrong please give me feedback. Thanks.

Answered By: DeadByDefault

The Python on Whales docker.run() function doesn’t take a docker 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:

from pathlib import Path
from python_on_whales import docker

# build the image, returns an Image object
image = docker.build(Path.cwd() / 'docker' / 'prime')

# start the container; like `docker run ...`
docker.run(image,
           command=['npm', 'run', 'build'],
           volumes=[(Path.cwd() / 'prime', '/app')], # -v $(PWD)/prime:/app
           interactive=True,                         # -i (required?)
           tty=True,                                 # -t (required?)
           remove=True)                              # --rm

The return value from docker.run() (without detach=True) is the container’s stdout, and the examples print() that data.

Answered By: David Maze
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.