我有一个名为 python-example 的 python 图像 使用 container_id = c08e4ee85879
里面有一个打印 hello world
文件.py
print "HELLO WORLD"
如果我运行我的图像: docker run python-example 输出 HELLO WORLD 将打印在我的终端上,然后容器将退出。
当我尝试进入容器以通过 vim 更改脚本时,退出容器并将更改提交到新图像,然后尝试运行新图像,但没有打印任何内容。请参阅以下步骤
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
当我输入新图像然后运行 python file.py 时,会打印输出。
docker run -it python-example2 bash
python file.py
"HELLO WORLD"
"HELLO WORLD 2"
为什么会这样?在 bash 中提交更改时,图像无法运行吗?
请您参考如下方法:
解决方案
将您的提交更改为:
docker commit --change='CMD ["python", "file.py"]' c08e4ee85879 python-example2
解释
当你运行时:
docker run -it python-example bash
您正在更改默认命令(python file.py 为 bash)。
您可以使用 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
使用 --change='CMD ["python", "file.py"]' 您可以返回所需的命令。
