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

**URL:** <https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386>\
**Category:** Docker Desktop\
**Tags:** docker, windows\
**Created:** [March 19, 2024, 4:37am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386 "2024-03-19T04:37:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohsinnust206811](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/mohsinnust206811/32/43975_2.png) [@mohsinnust206811](https://forums.docker.com/u/mohsinnust206811)\
**Post date:** [March 19, 2024, 4:37am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386/1 "2024-03-19T04:37:27Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![cavo789](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/cavo789/32/61490_2.png) [@cavo789](https://forums.docker.com/u/cavo789)\
**Post date:** [March 19, 2024, 5:15am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386/2 "2024-03-19T05:15:25Z")

</div>

Hi

See the example [powerlevel10k/README.md at master · romkatv/powerlevel10k · GitHub](https://github.com/romkatv/powerlevel10k/blob/master/README.md).

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

---

<div class="post-metadata">

**Author:** ![mohsinnust206811](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/mohsinnust206811/32/43975_2.png) [@mohsinnust206811](https://forums.docker.com/u/mohsinnust206811)\
**Post date:** [March 19, 2024, 5:42am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386/3 "2024-03-19T05:42:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cavo789](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/cavo789/32/61490_2.png) [@cavo789](https://forums.docker.com/u/cavo789)\
**Post date:** [March 19, 2024, 5:55am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386/4 "2024-03-19T05:55:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohsinnust206811](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/mohsinnust206811/32/43975_2.png) [@mohsinnust206811](https://forums.docker.com/u/mohsinnust206811)\
**Post date:** [March 19, 2024, 6:22am UTC](https://forums.docker.com/t/how-to-run-docker-container-and-then-run-bash-shell-commands-using-python-script/140386/5 "2024-03-19T06:22:52Z")

</div>

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

```auto
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.
