How to edit Docker container files from the host?

前端 未结 9 2219
孤城傲影
孤城傲影 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:08

    The following worked for me

    docker run -it IMAGE_NAME /bin/bash

    eg. my image was called ipython/notebook

    docker run -it ipython/notebook /bin/bash

    0 讨论(0)
  • 2020-12-13 09:15

    The best way is:

      $ docker cp CONTAINER:FILEPATH LOCALFILEPATH
      $ vi LOCALFILEPATH
      $ docker cp LOCALFILEPATH CONTAINER:FILEPATH
    

    Limitations with $ docker exec: it can only attach to a running container.

    Limitations with $ docker run: it will create a new container.

    0 讨论(0)
  • 2020-12-13 09:17
    docker run -it -name YOUR_NAME IMAGE_ID /bin/bash
    
    $>vi path_to_file
    
    0 讨论(0)
  • 2020-12-13 09:20

    Here's the script I use:

    #!/bin/bash
    IFS=$'\n\t'
    set -euox pipefail
    
    
    CNAME="$1"
    FILE_PATH="$2"
    
    TMPFILE="$(mktemp)"
    docker exec "$CNAME" cat "$FILE_PATH" > "$TMPFILE"
    $EDITOR "$TMPFILE"
    cat "$TMPFILE" | docker exec -i "$CNAME" sh -c 'cat > '"$FILE_PATH"
    rm "$TMPFILE"
    

    and the gist for when I fix it but forget to update this answer: https://gist.github.com/dmohs/b50ea4302b62ebfc4f308a20d3de4213

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 09:22

    We can use another way to edit files inside working containers (this won't work if container is stoped).

    Logic is to:
    -)copy file from container to host
    -)edit file on host using its host editor
    -)copy file back to container

    We can do all this steps manualy, but i have written simple bash script to make this easy by one call.

    /bin/dmcedit:

    #!/bin/sh
    set -e
    
    CONTAINER=$1
    FILEPATH=$2
    BASE=$(basename $FILEPATH)
    DIR=$(dirname $FILEPATH)
    TMPDIR=/tmp/m_docker_$(date +%s)/
    
    mkdir $TMPDIR
    cd $TMPDIR
    docker cp $CONTAINER:$FILEPATH ./$DIR
    mcedit ./$FILEPATH
    docker cp ./$FILEPATH $CONTAINER:$FILEPATH
    rm -rf $TMPDIR
    
    echo 'END'
    exit 1;
    

    Usage example:

    dmcedit CONTAINERNAME /path/to/file/in/container

    The script is very easy, but it's working fine for me.

    Any suggestions are appreciated.

    0 讨论(0)
提交回复
热议问题