如何在已停止/未启动的Docker容器中编辑文件 [英] How to edit files in stopped/not starting docker container

查看:1038
本文介绍了如何在已停止/未启动的Docker容器中编辑文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过拆分在多个容器中的应用程序来修复错误和调试问题,我经常在容器中编辑文件:

Trying to fix errors and debug problems with my application that is split over several containers, I frequently edit files in containers:

  • 我完全懒惰并安装nano并直接在容器中进行编辑,或者

  • either I am totally lazy and install nano and edit directly in container or

我将docker cp文件从容器中取出,对其进行编辑,将其复制回并重新启动容器

I docker cp the file out of the container, edit it, copy it back and restart the container

这些是进入容器构建新内容之前的中间步骤,比上述步骤花费的时间要长得多(当然,这只是中间步骤).

Those are intermediate steps before coming to new content for container build, which takes a lot longer than doing the above (which of course is only intermediate/fiddling around).

现在,我经常破坏容器的启动程序,在这种情况下,它是节点脚本或python Web服务器脚本,通常都因语法错误而失败.

Now I frequently break the starting program of the container, which in the breaking cases is either a node script or a python webserver script, both typically fail from syntax errors.

有什么方法可以保存那些容器?由于它们没有启动,所以我无法在其中执行docker exec,因此它们对我丢失了.然后在构建输入中修复有问题的文件之后,我就去了rm/rmi/build/run路线.

Is there any way to save those containers? Since they do not start, I cannot docker exec into them, and thus they are lost to me. I then go the rm/rmi/build/run route after fixing the offending file in the build input.

我如何在停止的容器中编辑文件,或者在停止的容器中cp它们或启动外壳-允许我修复该容器的任何方法?

(似乎有点像在远程计算机上工作并破坏网络配置-这种方式永远"失去连接,并且必须使用后备(如果存在).)

(It seems a bit like working on a remote computer and breaking the networking configuration - connection is lost "forever" this way and one has to use a fallback, if that exists.)

如何从主机编辑Docker容器文件?看起来相关,但已过时.

How to edit Docker container files from the host? looks relevant but is outdated.

推荐答案

回答我自己的问题..仍然希望从知识渊博的人那里得到更好的答案!

Answering my own question.. still hoping for a better answer from a more knowledgable person!!

有2种可能性.

1)直接在主机上编辑文件系统.这有些危险,并且有可能完全破坏容器,并可能根据发生的问题彻底破坏其他数据.

1) Editing file system on host directly. This is somewhat dangerous and has a chance of completely breaking the container, possibly other data depending on what goes wrong.

2)将启动脚本更改为永不失败的操作,例如启动bash,进行修复/编辑,然后再次将启动程序更改为所需的启动程序(例如节点或之前的任何版本) ).

2) Changing the startup script to something that never fails like starting a bash, doing the fixes/edits and then changing the startup program again to the desired one (like node or whatever it was before).

更多详细信息:

1)使用

docker ps

查找正在运行的容器或

docker ps -a

查找所有容器(包括停止的容器)和

to find all containers (including stopped ones) and

docker inspect (containername)

寻找"Id",第一个值.

look for the "Id", one of the first values.

这是包含实现细节的部分,可能会更改,请注意,这样可能会丢失容器.

This is the part that contains implementation detail and might change, be aware that you may lose your container this way.

转到

/var/lib/docker/aufs/diff/9bc343a9..(long container id)/

,您将在其中找到已针对容器所基于的映像进行更改的所有文件.您可以覆盖文件,添加或编辑文件.

and there you will find all files that are changed towards the image the container is based upon. You can overwrite files, add or edit files.

再次,我不建议这样做.

Again, I would not recommend this.

2)如 https://stackoverflow.com/a/32353134/586754 所述,您可以找到在

2) As is described at https://stackoverflow.com/a/32353134/586754 you can find the configuration json config.json at a path like

/var/lib/docker/containers/9bc343a99..(long container id)/config.json

您可以在e中更改args. G.将"nodejs app.js"更改为"/bin/bash".现在重新启动docker服务并启动容器(您应该看到它现在已正确启动).您应该使用

There you can change the args from e. g. "nodejs app.js" to "/bin/bash". Now restart the docker service and start the container (you should see that it now correctly starts up). You should use

docker start -i (containername)

,以确保它不会立即退出.您现在可以使用容器和/或以后再使用

to make sure it does not quit straight away. You can now work with the container and/or later attach with

docker exec -ti (containername) /bin/bash

此外,docker cp对于复制在容器外部编辑的文件非常有用.

Also, docker cp is rather useful for copying files that were edited outside of the container.

此外,如果容器无论如何或多或少地丢失",则应该只采用这些措施,因此任何更改都是一种改进.

Also, one should only fall back to those measures if the container is more or less "lost" anyway, so any change would be an improvement.

这篇关于如何在已停止/未启动的Docker容器中编辑文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆