I am currently working on automating commands for a Docker container with a Python script on the host machine. This Python script for now, builds and runs a docker-compose file, with the commands for the containers written into the docker-compose file and the dockerfile itself. What I want to do is have the Python script action all commands to run within the container, so if i have different scripts I want to run, i am not changing the container. I have tried 2 ways. First was to run os.system() command within the Python script, however, this works only as far as opening the shell for the container, the os.system() command does not execute code in the Docker container itself. The second way uses CMD within the Dockerfile, however, this is limited and is hard coded to the container. If I have multiple scripts I have to change the dockerfile, I dont want this. What I want is to build a default container with all services running, then run Python scripts on the host to run a sequence of commands on the container. I am fairly new to Docker and think there must be something I am overlooking to run scripted commands on the container. One possible solution I have come across is ‘nsenter’. Is this a reliable solve and how does it work? or is there a much simpler way? I have also used docker-volume to copy the python files into the container to be run on build, however, i can still not find a solve to automate the accessing and running these python scripts from the host machine.
Source: Docker Questions