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

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

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


当前回答

找到停止的容器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

其他回答

——entrypoint /bin/bash cont_id_or_name

(对于conven,把你的env, vol挂载在docker-compose.yml)

或者使用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容器,MariaDB容器在启动时不断崩溃,因为损坏了InnoDB表。

我解决问题的方法是:

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

我找到了一个简单的命令

docker start -a [container_name]

这样就可以了

Or

docker start [container_name]

then

docker exec -it [container_name] bash

这并不是你所要求的,但是如果你想要的只是检查文件,你可以在一个停止的容器上使用docker export。

mkdir $TARGET_DIR
docker export $CONTAINER_ID | tar -x -C $TARGET_DIR