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?
621 Answers
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:
docker exec -it <container> bash And run:
apt-get update apt-get install vim Or use the following Dockerfile:
FROM confluent/postgres-bw:0.1 RUN ["apt-get", "update"] RUN ["apt-get", "install", "-y", "vim"] 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 to read my post about the topic.
16If you don't want to add an editor just to make a few small changes (e.g., change the Tomcat configuration), you can just use:
docker cp <container>:/path/to/file.ext . which copies it to your local machine (to your current directory). Then edit the file locally using your favorite editor, and then do a
docker cp file.ext <container>:/path/to/file.ext to replace the old file.
7You 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 > file # 1. type in your content # 2. leave a newline at end of file # 3. ctrl-c / (better: ctrl-d) cat file cat will output each line on receiving a newline. Make sure to add a newline for that last line. ctrl-c sends a SIGINT for cat to exit gracefully. From the comments you see that you can also hit ctrl-d to denote end-of-file ("no more input coming").
Another option is something like infilter which injects a process into the container namespace with some ptrace magic:
To keep your Docker images small, don't install unnecessary editors. You can edit the files over SSH from the Docker host to the container:
vim scp://remoteuser@containerip//path/to/document 3For common edit operations I prefer to install vi (vim-tiny), which uses only 1491 kB or nano which uses 1707 kB.
In other hand vim uses 28.9 MB.
We have to remember that in order for apt-get install to work, we have to do the update the first time, so:
apt-get update apt-get install vim-tiny To start the editor in CLI we need to enter vi.
You can open existing file with
cat filename.extension and copy all the existing text on clipboard.
Then delete old file with
rm filename.extension or rename old file with
mv old-filename.extension new-filename.extension Create new file with
cat > new-file.extension Then paste all text copied on clipboard, press Enter and exit with save by pressing ctrl+z. And voila no need to install any kind of editors.
You can use cat if installed, with the > caracter. Here is the manipulation :
cat > file_to_edit #1 Write or Paste you text #2 don't forget to leave a blank line at the end of file #3 Ctrl + C to apply configuration Now you can see the result with the command
cat file Sometime you must first run the container with root:
docker exec -ti --user root <container-id> /bin/bash Then in the container, to install Vim or something else:
apt-get install vim 1I use "docker run" (not "docker exec"), and I'm in a restricted zone where we cannot install an editor. But I have an editor on the Docker host.
My workaround is: Bind mount a volume from the Docker host to the container (), and edit the file outside the container. It looks like this:
docker run -v /outside/dir:/container/dir This is mostly for experimenting, and later I'd change the file when building the image.
After you shelled to the Docker container, just type:
apt-get update apt-get install nano 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:
docker cp main.py my-container:/data/scripts/ ; docker exec -it my-container python /data/scripts/main.py 1If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.
To shelled to the Windows Docker container with PowerShell:
docker exec -it <name> powershell
First install Chocolatey package manager
Invoke-WebRequest -UseBasicParsing | Invoke-Expression;Install Vim
choco install vimRefresh ENVIRONMENTAL VARIABLE You can just
exitand shell back to the containerGo to file location and Vim it
vim file.txt
It is kind of screwy, but in a pinch you can use sed or awk to make small edits or remove text. Be careful with your regex targets of course and be aware that you're likely root on your container and might have to re-adjust permissions.
For example, removing a full line that contains text matching a regex:
awk '!/targetText/' file.txt > temp && mv temp file.txt See Stack Overflow question sed edit file in place
It would be a good option here, if:
- To modify a large file, it's impossible to use
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.cnffile, there is novim,vi, and Vim install takes too long (China Great Firewall).sedis provided in the image, and it's quite simple. My usage is likesed -i /s/testtobechanged/textwanted/g filenameUse
man sedor look for other tutorials for more complex usage.
If you can only shell into container with bin/sh (in case bin/bash doesn't work) and apt or apt-get doesn't work in the container, check whether apk is installed by entering apk in command prompt inside the container. If yes, you can install nano as follows: apk add nano
then nano will work as usual
An easy way to edit a few lines would be:
echo "deb stretch main" > sources.list You can also use a special container which will contain only the command you need: Vim. I chose python-vim. It assumes that the data you want to edit are in a data container built with the following Dockerfile:
FROM debian:jessie ENV MY_USER_PASS my_user_pass RUN groupadd --gid 1001 my_user RUN useradd -ms /bin/bash --home /home/my_user \ -p $(echo "print crypt("${MY_USER_PASS:-password}", "salt")" | perl) \ --uid 1001 --gid 1001 my_user ADD src /home/my_user/src RUN chown -R my_user:my_user /home/my_user/src RUN chmod u+x /home/my_user/src CMD ["true"] You will be able to edit your data by mounting a Docker volume (src_volume) which will be shared by your data container (src_data) and the python-vim container.
docker volume create --name src_volume docker build -t src_data . docker run -d -v src_volume:/home/my_user/src --name src_data_1 src_data docker run --rm -it -v src_volume:/src fedeg/python-vim:latest That way, you do not change your containers. You just use a special container for this work.
I agree that @hkong's answer should be accepted, as it avoids adding additional packages to the container. However, I found that after pasting the modified file to the container, some of them tended to have restricted access so that docker daemon failed to start. In this case, assign the modified file with some permission then pasting to the container.
docker comes up with no editors. so simply install vim, 36MB space don't kill your docker!
Make sure to update the container before trying to install the editor.
apt-get update apt-get install nano vi First login as root : docker run -u root -ti bash Type following commands: apt-get update && apt-get install nano
1