bash 如何在已停止/未启动的 docker 容器中编辑文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32750748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit files in stopped/not starting docker container
提问by Andreas Reiff
Trying to fix errors and debug problems with my application that is split over several containers, I frequently edit files in containers:
尝试修复拆分到多个容器的应用程序的错误和调试问题,我经常编辑容器中的文件:
either I am totally lazy and install nano and edit directly in container or
I docker cp the file out of the container, edit it, copy it back and restart the container
要么我完全懒惰并安装 nano 并直接在容器中编辑,要么
我从容器中 docker cp 文件,编辑它,将其复制回来并重新启动容器
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).
这些是进入容器构建新内容之前的中间步骤,这比执行上述操作需要更长的时间(当然这只是中间/摆弄)。
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.
现在我经常破坏容器的启动程序,在破坏情况下,它要么是节点脚本,要么是 python 网络服务器脚本,两者通常都因语法错误而失败。
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.
有没有办法保存这些容器?由于它们没有启动,我无法 docker exec 进入它们,因此它们对我来说丢失了。在修复构建输入中的有问题的文件后,我然后去 rm/rmi/build/run 路线。
How can I either edit files in a stopped container, or cp them in or start a shell in a stopped container - anything that allows me to fix this container?
如何在已停止的容器中编辑文件,或将它们 cp 放入已停止的容器中或在已停止的容器中启动 shell - 任何允许我修复此容器的东西?
(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.)
(这似乎有点像在远程计算机上工作并破坏网络配置 - 以这种方式“永远”失去连接,并且必须使用后备,如果存在的话。)
How to edit Docker container files from the host?looks relevant but is outdated.
如何从主机编辑 Docker 容器文件?看起来相关但已过时。
采纳答案by Andreas Reiff
Answering my own question.. still hoping for a better answer from a more knowledgable person!!
回答我自己的问题..仍然希望有知识渊博的人提供更好的答案!!
There are 2 possibilities.
有2种可能性。
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.
1)直接在主机上编辑文件系统。这有点危险,并且有可能完全破坏容器,可能是其他数据,具体取决于出了什么问题。
2) Changing the startup scriptto 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).
2)将启动脚本更改为永远不会失败的内容,例如启动 bash,进行修复/编辑,然后再次将启动程序更改为所需的程序(如节点或之前的任何内容)。
More details:
更多细节:
1) Using
1) 使用
docker ps
to find the running containers or
找到正在运行的容器或
docker ps -a
to find all containers (including stopped ones) and
找到所有容器(包括停止的容器)和
docker inspect (containername)
look for the "Id", one of the first values.
寻找“Id”,第一个值之一。
This is the part that contains implementation detail and might change, be aware that you may lose your container this way.
这是包含实现细节的部分,可能会更改,请注意您可能会以这种方式丢失容器。
Go to
去
/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) As is described at https://stackoverflow.com/a/32353134/586754you can find the configuration json config.json at a path like
2)如https://stackoverflow.com/a/32353134/586754所述,您可以在类似路径中找到配置json config.json
/var/lib/docker/containers/9bc343a99..(long container id)/config.json
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
在那里您可以将参数从例如“nodejs app.js”更改为“/bin/bash”。现在重新启动 docker 服务并启动容器(您应该会看到它现在正确启动了)。你应该使用
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
Also, docker cp is rather useful for copying files that were edited outside of the container.
此外, docker cp 对于复制在容器外编辑的文件非常有用。
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.
此外,只有在容器或多或少“丢失”的情况下,才应该回退到这些措施,因此任何更改都将是一种改进。
回答by tomurie
I had a problem with a container which wouldn't start due to a bad config change I made. I was able to copy the file out of the stopped container and edit it. something like:
由于我所做的配置更改不当,我的容器无法启动。我能够将文件从停止的容器中复制出来并进行编辑。就像是:
docker cp docker_web_1:/etc/apache2/sites-enabled/apache2.conf .
(correct the file)
(更正文件)
docker cp apache.conf docker_web_1:/etc/apache2/sites-enabled/apache2.conf
回答by Tejas Sarade
You can edit container file-system directly, but I don't know if it is a good idea.
First you need to find the path of directory which is used as runtime root for container.
Run docker container inspect id/name
.
Look for the key UpperDir
in JSON output.
您可以直接编辑容器文件系统,但我不知道这是否是一个好主意。首先,您需要找到用作容器运行时根目录的目录路径。运行docker container inspect id/name
。UpperDir
在 JSON 输出中查找密钥。
That is your directory.
那是你的目录。
回答by user6830669
If you are trying to restart an stopped container and need to alter the container because of misconfiguration but the container isn't starting you can do the following which works using the "docker cp" command (similar to previous suggestion). This procedure lets you remove files and do any other changes needed. With luck you can skip a lot of the steps below.
如果您尝试重新启动已停止的容器并且由于配置错误而需要更改容器但容器未启动,您可以使用“docker cp”命令执行以下操作(类似于之前的建议)。此过程可让您删除文件并进行所需的任何其他更改。幸运的话,您可以跳过以下许多步骤。
- Use docker inspect to find entrypoint, (named Path in some versions)
- Create a clone of the using docker run
- Enter clone using docker exec -ti bash (if *nix container)
- Locate entrypoint file location by looking though the clone to find
- Copy the old entrypoint script using docker cp : ./
Modify or create a new entrypoint script for instance
#!/bin/bash tail -f /etc/hosts
- ensure the script has execution rights
- Replace the old entrypoint using docker cp ./ :
- start the old container using start
- redo steps 6-9 until the starts
- Fix issues in container
- Restore entrypoint if needed and redo steps 6-9 as required
- Remove clone if needed
- 使用 docker inspect 查找入口点,(在某些版本中命名为 Path)
- 使用 docker run 创建一个克隆
- 使用 docker exec -ti bash 输入克隆(如果 *nix 容器)
- 通过查看要查找的克隆来定位入口点文件位置
- 使用 docker cp 复制旧的入口点脚本:./
例如修改或创建一个新的入口点脚本
#!/bin/bash tail -f /etc/hosts
- 确保脚本具有执行权限
- 使用 docker cp ./ 替换旧的入口点:
- 使用 start 启动旧容器
- 重做步骤 6-9 直到开始
- 修复容器中的问题
- 如果需要,恢复入口点并根据需要重做步骤 6-9
- 如果需要,删除克隆