i have a python image called python-example
with container_id = c08e4ee85879
Inside exist a python script that prints hello world
print "HELLO WORLD"
if i run my image:
docker run python-example the output HELLO WORLD will be printed on my terminal and then the container will exit.
When i try to enter the container to make a change to the script through vim, exit the container and commit the change to a new image and then try to run the new image, nothing get printed. See steps below
docker run -it python-example bash
vim file.py
#make some changes by adding a new print statement "HELLO WORLD 2"
exit
docker commit c08e4ee85879 python-example2
docker run python-example2 #now nothing gets printed to terminal screen
When i enter the new image then run python file.py, the output gets printed.
docker run -it python-example2 bash
python file.py
"HELLO WORLD"
"HELLO WORLD 2"
Why is this so? Is it not possible for an image to run when changes are commited in bash?
Solution
Change your commit to:
docker commit --change='CMD ["python", "file.py"]' c08e4ee85879 python-example2
Explanation
When you run:
docker run -it python-example bash
you are changing your default command (which is python file.py to bash).
You can check with docker ps.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c08e4ee85879 python-example "bash" 32 seconds ago Up 30 seconds thirsty_hugle
Check dockerfile reference - CMD for more information
With --change='CMD ["python", "file.py"]' you get the desired command back.