I successfully shelled to a Docker container using
docker exec -i -t 69f1711a205e bash
Now I need to edit file and I don't have any editors inside:
root@69f1711a205e:/# nano
bash: nano: command not found
root@69f1711a205e:/# pico
bash: pico: command not found
root@69f1711a205e:/# vi
bash: vi: command not found
root@69f1711a205e:/# vim
bash: vim: command not found
root@69f1711a205e:/# emacs
bash: emacs: command not found
root@69f1711a205e:/#
How do I edit files?
As in the comments, there's no default editor set - strange - the
$EDITOR
environment variable is empty. You can log in into a container with:And run:
Or use the following Dockerfile:
Docker images are delivered trimmed to the bare minimum - so no editor is installed with the shipped container. That's why there's a need to install it manually.
EDIT
I also encourage you read my post about the topic.
See Stack Overflow question sed edit file in place
It would be a good option here, if:
cat
.Install Vim is not allowed or takes too long. My situation is using the MySQL 5.7 image when I want to change the
my.cnf
file, there is novim
,vi
, and Vim install takes too long (China Great Firewall).sed
is provided in the image, and it's quite simple. My usage is likesed -i /s/testtobechanged/textwanted/g filename
Use
man sed
or look for other tutorials for more complex usage.You can just edit your file on host and quickly copy it into and run it inside the container. Here is my one-line shortcut to copy and run a Python file:
After you shelled to the Docker container, just type:
You can use
cat
if it's installed, which will most likely be the case if it's not a bare/raw container. It works in a pinch, and ok when copy+pasting to a proper editor locally.cat
will output each line on receiving a newline. Make sure to add a newline for that last line. ctrl-c sends aSIGINT
for cat to exit gracefully.Another option is something like
infilter
which injects a process into the container namespace with some ptrace magic: https://github.com/yadutaf/infilterAn easy way to edit a few lines would be: