如何在 docker 容器中编辑文件 down/not-started

How can I edit files in a docker container when it's down/not-started

用例:我启动了一些不错的 docker 图像,我的容器需要一些尝试(配置文件更改以供研究)。我编辑一个文件(使用 sed 或 vim ;-) )然后我停止容器并尝试启动它。现在我在配置中犯了一个错误,当我这样做时 docker 容器没有出现:docker restart <my-container-id/-name> 我如何编辑配置文件来修复错误?

根据 jpetazzo (see https://github.com/jpetazzo/nsenter/issues/27#issuecomment-53799568) 的建议,我启动了一个使用原始容器 'volumes' 的不同容器。方法如下:

docker run --volumes-from <my-container-id/-name> -it busybox

这将启动一个 busybox shell。在那里你有 vi 和其他工具来检查和修复配置文件。

您可以使用 docker cp 将文件 to/from 复制到一个容器中,无论它是否 运行 以及是否有卷:

> docker run --name temp alpine touch /file1.txt 
> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
> docker cp temp:/file1.txt .
> ls
file1.txt

您好,只需从容器中创建一个图像并使用 bash 启动它,然后进行更正。您的容器现在以 bash 开头,因此您需要再次提交并 运行 使用您之前的命令。就是这样:

docker commit <containerid>
docker images # to see the new imageid
docker run -it <imageid> bash
#Make the corrections and then exit bash
docker commit <containerid>
docker run <newimageid> formercommand

此致