我想用一个不同的命令来启动一个停止的Docker容器,因为默认命令会崩溃——这意味着我不能启动容器,然后使用' Docker exec'。

基本上,我想启动一个shell,这样我就可以检查容器的内容。

幸运的是,我创建了带有-it选项的容器!


当前回答

我有一个docker容器,MariaDB容器在启动时不断崩溃,因为损坏了InnoDB表。

我解决问题的方法是:

从容器中复制docker-entrypoint.sh到本地文件系统(docker cp) 编辑它以包含所需的命令行参数(在我的例子中——innodb-force-recovery=1) 将编辑过的文件复制回docker容器,覆盖现有的入口点脚本。

其他回答

找到停止的容器id

docker ps -a

提交停止的容器:

该命令将修改后的容器状态保存到名为user/test_image的新映像中:

docker commit $CONTAINER_ID user/test_image

使用不同的入口点启动/运行:

docker run -ti --entrypoint=sh user/test_image

入口点参数描述:

https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime

注意:

上面的步骤只是启动一个具有相同文件系统状态的已停止容器。这对快速调查很有帮助;但是环境变量、网络配置、附加卷和其他东西不是继承的。您应该显式地指定所有这些参数。

启动一个停止的容器的步骤已经从这里借来:(最后的评论)https://github.com/docker/docker/issues/18078

我有一个docker容器,MariaDB容器在启动时不断崩溃,因为损坏了InnoDB表。

我解决问题的方法是:

从容器中复制docker-entrypoint.sh到本地文件系统(docker cp) 编辑它以包含所需的命令行参数(在我的例子中——innodb-force-recovery=1) 将编辑过的文件复制回docker容器,覆盖现有的入口点脚本。

关于这个问题有很多讨论,所以我想再补充一个我没有立即看到上面列出的:

If the full path to the entrypoint for the container is known (or discoverable via inspection) it can be copied in and out of the stopped container using 'docker cp'. This means you can copy the original out of the container, edit a copy of it to start a bash shell (or a long sleep timer) instead of whatever it was doing, and then restart the container. The running container can now be further edited with the bash shell to correct any problems. When finished editing another docker cp of the original entrypoint back into the container and a re-restart should do the trick.

我曾经用它来纠正一个“快速修复”,我黄油手指,不再能够运行正常入口点的容器,直到它被纠正。

我也同意应该有一种更好的方式通过docker来做到这一点:也许可以选择“docker restart”来允许另一个入口点?嘿,也许这已经适用于“——entrypoint”?不确定,没有尝试过,留给读者练习,如果有用请告诉我。:)

我的问题:

我用docker run <IMAGE_NAME>启动了一个容器 然后将一些文件添加到这个容器中 然后我关闭了容器,并尝试使用与上面相同的命令再次启动它。 但当我检查新文件时,它们不见了 当我运行docker ps -a时,我可以看到两个容器。 这意味着每次我运行docker run <IMAGE_NAME>命令时,都会创建新的映像

解决方案: 要在最初创建的容器上运行,请执行以下步骤

Docker ps获取你的容器的容器 docker容器start <CONTAINER_ID>启动现有容器 然后你可以从你离开的地方继续。例如:docker exec -它<CONTAINER_ID> /bin/bash 然后,您可以决定从中创建一个新映像

我把@Dmitriusan的回答变成了一个别名:

别名docker-run- prer -container='prev_container_id="$(docker ps -aq | head -n1)"&& docker commit "$prev_container_id" && docker run -it——entrypoint=bash "prev_container/$prev_container_id"

将此添加到~/中。Bashrc别名文件,你会有一个漂亮的新docker-run- prec -container别名,它会把你放到前一个容器的shell中。

有助于调试失败的docker构建。