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

时间:2016-10-20 13:19:06

标签: docker edit configuration-files

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?

3 个答案:

答案 0 :(得分:5)

您可以使用docker cp将文件复制到容器中或从容器中复制文件,无论它是否正在运行,以及它是否有卷:

> 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

答案 1 :(得分: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.

答案 2 :(得分:0)

你好只需从容器中创建一个图像并用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

此致