# Best way to run Python script then stop container

**URL:** <https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366>\
**Category:** General\
**Created:** [April 2, 2019, 10:25am UTC](https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366 "2019-04-02T10:25:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guillaumedesforges](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/guillaumedesforges/32/23306_2.png) [@guillaumedesforges](https://forums.docker.com/u/guillaumedesforges)\
**Post date:** [April 2, 2019, 10:25am UTC](https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366/1 "2019-04-02T10:25:26Z")

</div>

Hello,

I want to allow non-technical people in my project to run Python scripts on their laptop without having them to setup the necessary technical environment (virtual environment, packages, dependencies etc).

I want them for instance to be able to use a command `./merge.sh`, so that the script can start the docker container, mount a local directory as a volum (to get the input and the output), run the Python script, stop the container.

I want to be able to do so for different commands (for instance also `./transform.sh`, `./encode.sh`)…

What is the best way to make this happen, both from a technical point of view (extensible, maintainable) and a usage point of view (only need to install docker/docker-compose and clone my repository).

Thanks

---

<div class="post-metadata">

**Author:** ![ovelindstrom](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/ovelindstrom/32/18494_2.png) [@ovelindstrom](https://forums.docker.com/u/ovelindstrom)\
**Post date:** [April 2, 2019, 12:35pm UTC](https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366/2 "2019-04-02T12:35:59Z")

</div>

I have made functions that uses the python image to run a python script in the current directory. I use (more or less) the line from the official pyton image documentation. Something like this:

```auto
# Functions
py3() {
  docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python $1
}

py2() {
  docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2 python $1
}

```

---

<div class="post-metadata">

**Author:** ![pinimo](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@pinimo](https://forums.docker.com/u/pinimo)\
**Post date:** [April 2, 2019, 4:08pm UTC](https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366/3 "2019-04-02T16:08:26Z")

</div>

One solution would be using a Makefile, providing GNU `make` is installed. This way, you can write modular recipes.

```makefile

.PHONY: pull run transform encode

ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
IMAGE := "guillaumedesforges/python_image:latest"

pull:
	docker pull $(IMAGE)

run:
	docker run -it -v "$(ROOT_DIR)":/usr/src/myapp -w /usr/src/myapp $(IMAGE) python $(CMD)

transform:
	$(MAKE) run CMD="./transform.py"

encode:
	$(MAKE) run CMD="./encode.py"

```

Your colleagues may then invoke very simple commands from a shell:

```bash
$ make pull # executed once
$ make transform
$ make encode

```

Voilà ! 🙂

---

<div class="post-metadata">

**Author:** ![ovelindstrom](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/ovelindstrom/32/18494_2.png) [@ovelindstrom](https://forums.docker.com/u/ovelindstrom)\
**Post date:** [April 3, 2019, 6:41am UTC](https://forums.docker.com/t/best-way-to-run-python-script-then-stop-container/72366/4 "2019-04-03T06:41:47Z")

</div>

Nice one pinimo! I’ll “steal” that. 😃
