Docker Community Forums

Share and learn in the Docker community.

How can I run an .py file in docker container to let it exit automaticly for Ubuntu?

Now I have a docker image with its os, Ubuntu 16.04. My host OS is also Ubuntu 16.04. Now, I want to make a demo.py file and put it into the docker container. What I want is to let the container exit after I run this demo.py. How can I make it? Is it possible? Thanks!
Here is my toy code, but it failed.

auto_run.py

import os
os.system('exec python test_exit.py')

test_exit.py

import subprocess
def qq():
    while True:
        choice = ""
        choice = input("Type \"quit\" to exit container> ")
        if choice == 'quit':
            print("Exiting container")
            subprocess.call("exit", shell=True)
            break
        else:
            print("Invalid input.")

qq()

Run in detached mode

Docker can run your container in detached mode or in the background. To do this, we can use the --detach or -d for short. Docker will start your container the same as before but this time will “detach” from the container and return you to the terminal prompt.

Thanks! But I think you misunderstood my question. What I mean is how to write a python file as a role of controlling the exit of a running docker container. When some conditions satisfied, like typing “quit” in the above code, the python file chooses to exit the docker. The test_exit.py and auto_run.py are two scripts I wrote, but I find it does not work. Can you give me some suggestions for the code? Thank you!