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?
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:
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.
That way, you do not change your containers. You just use a special container for this work.
If 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:
which copies it to your local machine (to your current directory). Then edit the file locally using your favorite editor, and then do a
to replace the old file.
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:
I 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 (https://docs.docker.com/engine/reference/run/#/volume-shared-filesystems), and edit the file outside the container. It looks like this:
This is mostly for experimenting, and later I'd change the file when building the image.
Sometime you must first run the container with
root
:Then in the container, to install Vim or something else:
It is kind of screwy, but in a pinch you can use
sed
orawk
to make small edits or remove text. Be careful with your regex targets of course and be aware that you're likelyroot
on your container and might have to re-adjust permissions.For example, removing a full line that contains text matching a regex:
(More)