gpt4 book ai didi

python - docker python : committed changes in a container but new image does not run

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:37 26 4
gpt4 key购买 nike

我有一个名为 python-example 的 python 图像使用 container_id = c08e4ee85879

里面有一个打印 hello world

的 python 脚本

文件.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.pybash)。

您可以使用 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"]' 您可以返回所需的命令。

关于python - docker python : committed changes in a container but new image does not run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com