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

倾然丶 夕夏残阳落幕 提交于 2020-01-13 06:32:46

问题


Use-case: I started some nice docker image and my container needs some playing around (configuration file changes for research). I edit a file (using sed or vim ;-) ) and then I stop the container and try to start it. Now I made a mistake in the configuration and the docker container does not come up when I do: docker restart <my-container-id/-name> How can I edit the configuration-file to fix the mistake?


回答1:


You can uses docker cp to copy files to/from a container, whether it's running or not, and whether it has volumes or not:

> 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



回答2:


Based on the advice of jpetazzo (see https://github.com/jpetazzo/nsenter/issues/27#issuecomment-53799568) I started a different container that used the 'volumes' of the original container. Here is how:

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

This will start a busybox shell. In there you have vi and other tools to inspect and fix the configuration-files.




回答3:


Hello just create an image from the container and start it with bash then make the correction. Your container now starts with bash so you need to commit again and run it with your former command. And that's it:

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

Regards



来源:https://stackoverflow.com/questions/40155622/how-can-i-edit-files-in-a-docker-container-when-its-down-not-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!