How to edit file in a docker container when no editor is installed
I recently needed to edit a file in a docker
container, but when I tried editing the file, no luck:
/var/www/html/config# vi config.ini.php
bash: vi: command not found
Of course, I tried installing an editor right away:
/var/www/html/config# apt install nano
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package nano is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'nano' has no installation candidate
or
/var/www/html/config# apt install vi
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package vi
or
/var/www/html/config# apt install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package vim
So, nothing worked. I wasn't too keen on the idea of mapping the config file I needed to edit to my docker host
either, so I found another solution:
docker cp name-app:/var/www/html/config/config.ini.php .
Successfully copied 6.66kB to /data/dockers/dir/.
What this does is that it copies the file from the docker container
directly to the docker host
. Now I had the file on my docker host where I was able to edit it with my favourite editor (nano
). Once I was done, the same logic applied:
docker cp config.ini.php name-app:/var/www/html/config/config.ini.php
Successfully copied 6.66kB to name-app:/var/www/html/config/config.ini.php
And that's it. Just to test that my docker container retained the config, I restarted it and all was good! This will of course depend on the docker container
itself, but in my case, it worked like a charm!
Member discussion