# Official Python Image: How to execute multiple scripts at once?

**URL:** <https://forums.docker.com/t/official-python-image-how-to-execute-multiple-scripts-at-once/97656>\
**Category:** General\
**Tags:** docker\
**Created:** [August 14, 2020, 6:13pm UTC](https://forums.docker.com/t/official-python-image-how-to-execute-multiple-scripts-at-once/97656 "2020-08-14T18:13:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marctv](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/marctv/32/28223_2.png) [@marctv](https://forums.docker.com/u/marctv)\
**Post date:** [August 14, 2020, 6:13pm UTC](https://forums.docker.com/t/official-python-image-how-to-execute-multiple-scripts-at-once/97656/1 "2020-08-14T18:13:23Z")

</div>

Hi!

I use the python image to run multiple python 2 scripts every few minutes. But this way I have to start a container multiple times. Is there a best practice to avoid this?

docker run -it --rm --name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writeweather “Hannover,de”  
docker run -it --rm --name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writetwitter “MarcTV”  
docker run -it --rm --name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writeyoutube

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kfrajer](https://forums.docker.com/u/kfrajer)\
**Post date:** [August 16, 2020, 4:53pm UTC](https://forums.docker.com/t/official-python-image-how-to-execute-multiple-scripts-at-once/97656/2 "2020-08-16T16:53:08Z")

</div>

My approach would be to define the _entrypoint/cmd_ fields in the image in a way to have the container running in the background. For example:

```auto
docker run -it -d --rm --name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 /in/bash

```

Notice the “detached” flag `-d` above. Your container will be running in the background as you can see by inspecting via `docker ps`. This trick is possible for this image but in other cases, it depends on the image you use. More specifically, it depends on the **entrypoint** and also if it has bash installed in the image. (This is a side note and adding for clarification, FYI) Then you could execute the commands on the container directly using exec:

```auto
docker exec my-running script {cmd}

```

where `{cmd}` would be:

- `python dwhproxy.py writeweather “Hanwriteyoutubenover,de”`
- `python dwhproxy.py writetwitter “MarcTV”`
- `python dwhproxy.py `

* * *

I notice you are running your container in **interactive** mode. Another possible approach would be accessing the container by creating a _bash_ session and then executing the commands within the container via the interactive session. It would be something like this:

```auto
docker run -it --rm --name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 /bin/bash

```

At this point you are in a session inside the container and you could execute your commands one after the other:

```auto
 python dwhproxy.py writeweather “Hanwriteyoutubenover,de”
 python dwhproxy.py writetwitter “MarcTV”
 python dwhproxy.py 

```

You exit the container with ‘exit’ which will stop the container and it is removed immediately as you have the `--rm` flag in the command.
