2

I use the python image to run multiple python 3 scripts every few minutes. But this way I have to start a container multiple times.

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

Is there a best practice to avoid this?

1 Answer 1

2

I don't see anything wrong in running one-shot python commands the way you are doing it. The --rm option will clean those one task containers.

I guess that your containers start-up almost instantly, therefore the start-up time shouldn't be a problem for you either.

However, if you want to launch a container and keep it alive, you can achieve it like this:

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 tail -f /dev/null

Then you can get the ID of the running python container:

docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS               NAMES
0b91c8df3d12        python:3            "tail -f /dev/null"   2 seconds ago       Up 1 second                             suspicious_joliot

and execute the python commands against the same container, one by one:

docker exec 0b91c8df3d12 python dwhproxy.py writeweather "Hannover,de"
docker exec 0b91c8df3d12 python dwhproxy.py writetwitter "MarcTV"
docker exec 0b91c8df3d12 python dwhproxy.py writeyoutube

or all in one shot:

docker exec ad9ae8d67290 sh -c 'python dwhproxy.py writeweather "Hannover,de" && python dwhproxy.py writetwitter "MarcTV" && python dwhproxy.py writeyoutube'

or even with your initial approach while having all three scripts executed:

docker run -it --rm python:3 sh -c 'python dwhproxy.py writeweather "Hannover,de" && python dwhproxy.py writetwitter "MarcTV" && python dwhproxy.py writeyoutube'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.