How to edit Docker container files from the host?

2019-01-22 05:42发布

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 a host editor?

sshfs could probably do the job but since a running container is already some kind of host directory I wonder if there is a portable (between aufs, btrfs and device mapper) way to do that?

标签: docker
8条回答
Evening l夕情丶
2楼-- · 2019-01-22 06:07

If you think your volume is a "network drive", it will be easier. To edit the file located in this drive, you just need to turn on another machine and connect to this network drive, then edit the file like normal.

How to do that purely with docker (without FTP/SSH ...)?

  1. Run a container that has an editor (VI, Emacs). Search Docker hub for "alpine vim"

Example:

docker run -d --name shared_vim_editor \
 -v <your_volume>:/home/developer/workspace \
jare/vim-bundle:latest
  1. Run the interactive command:

docker exec -it -u root shared_vim_editor /bin/bash

Hope this helps.

查看更多
叛逆
3楼-- · 2019-01-22 06:08
docker run -it -name YOUR_NAME IMAGE_ID /bin/bash

$>vi path_to_file
查看更多
Evening l夕情丶
4楼-- · 2019-01-22 06:09

I use sftp plugin from my IDE.

  1. Install ssh server for your container and allow root access.
  2. Run your docker container with -p localport:22
  3. Install from your IDE a sftp plugin

Example using sublime sftp plugin: https://www.youtube.com/watch?v=HMfjt_YMru0

查看更多
疯言疯语
5楼-- · 2019-01-22 06:11

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

查看更多
放荡不羁爱自由
6楼-- · 2019-01-22 06:12

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.

查看更多
相关推荐>>
7楼-- · 2019-01-22 06:13

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

查看更多
登录 后发表回答