How to run docker container and then run bash shell commands using python script

I have been using docker using cmd. For example, I used to use the following code to run my container

docker run -it --name container-name --rm -u dafoamuser --mount "type=bind,src=D:/Dafoam,target=/home/dafoamuser/mount" -w /home/dafoamuser/mount dafoamimage:latest bash

After this I used to run commands like these:
cd oneraM6/
or
mpirun -np 8 python runScript.py 2>&1 | tee logOpt.txt

Now I want to do all of this using a python script, i.e. starting container, then running commands in container’s bash shell from python script. Is these any documentation about it?

Hi

See the example powerlevel10k/README.md at master · romkatv/powerlevel10k · GitHub.

You’ll see how to do from a one command line. You just have to adapt to fit your need.

I don’t get it how this is related to my problem. Could you please elaborate? Sorry, I am newbie.

I’ve understood that you want a single command line to start the container and run in it a given set of commands. This is what I’ve suggested by referencing the example from powerlevel10k.

Now, remains, the python part, the easiest one I think.

Otherwise i’ve not understand your question.

I want to use a python script to start docker container and then run a set of commands.
For example

import docker

# Initialize the Docker client
client = docker.from_env()

# Define Docker run options
docker_run_options = {
    'name': 'container-pythonScript',
    'user': 'dafoamuser',
    'mounts': [
        {
            'type': 'bind',
            'source': 'D:/Dafoam/oneraM6',
            'target': '/home/dafoamuser/mount'
        }
    ],
    'working_dir': '/home/dafoamuser/mount',
    'detach': True,
    'tty': True,
    'stdin_open': True,
    'command': 'bash'
}

# Run the Docker container with specified options
container = client.containers.run(
    'dafoamimage:latest',
    **docker_run_options
)
# /home/dafoamuser/dafoam/packages/miniconda3/bin/python hello-world.py
# Execute the mpirun command inside the container
mpirun_command = '/home/dafoamuser/dafoam/packages/openmpi-3.1.6/opt-gfortran/bin/mpirun -np 8 /home/dafoamuser/dafoam/packages/miniconda3/bin/python runScript_v2.py'
exec_cmd = container.exec_run(mpirun_command, tty=True, privileged=True)
print(f"Command: {mpirun_command}\nOutput:\n{exec_cmd.output.decode('utf-8')}")

# Stop and remove the container (if needed)
container.stop()
container.remove()

This (above) is my python script. When I run this script, it starts the container and also runs the “mpirun -np 8 python runScript_v2.py”. But the main problem, I am facing is, I have to provide full path of mpirun or python as shown in code.

But if I use windows command prompt, I don’t need to provide full paths. In windows command prompt, I can simply put my commands as follows:

C:\Users\yesbo>docker run -it --name container-name --rm -u dafoamuser --mount "type=bind,src=D:/Dafoam,target=/home/dafoamuser/mount" -w /home/dafoamuser/mount dafoamimage:latest bash
dafoamuser@ed3136410e4f:~/mount$mpirun -np 8 python runScript_v2.py | tee logOpt.txt

Now I don’t know what I am doing wrong in python script. It should not ask for paths.