How to edit Docker container files from the host?

前端 未结 9 2333
孤城傲影
孤城傲影 2020-12-13 08:51

Now that I found a way to expose host files to the container (-v option) I would like to do kind of the opposite:

How can I edit files from a running container with

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 09:21

    Whilst it is possible, and the other answers explain how, you should avoid editing files in the Union File System if you can.

    Your definition of volumes isn't quite right - it's more about bypassing the Union File System than exposing files on the host. For example, if I do:

    $ docker run --name="test" -v /volume-test debian echo "test"
    

    The directory /volume-test inside the container will not be part of the Union File System and instead will exist somewhere on the host. I haven't specified where on the host, as I may not care - I'm not exposing host files, just creating a directory that is shareable between containers and the host. You can find out exactly where it is on the host with:

    $ docker inspect -f "{{.Volumes}}" test
    map[/volume_test:/var/lib/docker/vfs/dir/b7fff1922e25f0df949e650dfa885dbc304d9d213f703250cf5857446d104895]
    

    If you really need to just make a quick edit to a file to test something, either use docker exec to get a shell in the container and edit directly, or use docker cp to copy the file out, edit on the host and copy back in.

提交回复
热议问题