0

Background

I am trying to run Neovim in a Docker container.

One issue I am running into is that paths to files in the container are different than they are on the host file system.

Example:

If I want to edit /etc/passwd with the Vim container passing in the path /etc/password would edit the file in the container not the file on my host. Instead I would have to pass something like /host-shared-dir/etc/passwd and run the container with docker run -v /:/host-shared-dir ....

Question

How can I remap all paths in Vim to treat another directory as the root.

Example:

If I pass the path /etc/passwd to Vim, how can I get Vim to actually open the file /host-shared-dir/etc/passwd?

Noah Huppert
  • 4,028
  • 6
  • 36
  • 58
  • Interesting way to use docker! Normally I want my containers to be completely separated from my host for security reasons. If you mount your hosts / directory to a directory in your docker container, such as /host you may be able to chroot into /host in the docker container. You will have to use privileged mode as in this answer https://stackoverflow.com/a/33235940/3209885 docker run --privileged – marneylc Dec 11 '18 at 17:43
  • ...why not just directly edit the file on the host? Especially since this question is about system-level config files? – David Maze Dec 11 '18 at 17:58
  • You're right it is a bit of an odd use for containers. I'm mainly using a container to record the run environment which Neovim and all its plugins need as code. So I can reproduce it if I need to install Neovim on another machine. – Noah Huppert Dec 11 '18 at 19:34

2 Answers2

1

You can use :autocmd to redirect any newly opened buffers (:help BufRead) to a file system path that has your root prepended:

autocmd BufRead * execute 'edit' fnameescape('/host-shared-dir' . expand('%:p'))

Note that this is just a simple demo; you probably want to clean up the existing original buffer, only do this for certain paths, handle filetype detection, etc. For a more complete implementation, you can look at the file-line plugin; it implements something similar.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

Start container in privileged and mount host volume "/"

docker container run -t -d --privileged -v "/":"/host/" ubuntu

Enter container

docker container exec -it <container id> bash

Then chroot to host

chroot host

We now have access to all system files with the same host paths.

marneylc
  • 7,190
  • 2
  • 12
  • 8
  • But (assuming that Vim is installed in the container under something like `/usr/bin/vim` and not mounted on the `/host-shared-dir`) wouldn't the `chroot` prevent the start of Vim itself (or any other command)?! – Ingo Karkat Dec 11 '18 at 18:33
  • This solution is starting to get weird and messy... but I like the idea of using a docker container to modify its host. In the docker container: we could mount `/usr/bin/` into the `/host` via a `mount --bind /usr/bin /host/usr/bin/` – marneylc Dec 11 '18 at 21:24