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:
Bash
/var/www/html/config# vi config.ini.php
bash: vi: command not foundOf course, I tried installing an editor right away:
Bash
/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 candidateor
Bash
/var/www/html/config# apt install vi
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package vior
Bash
/var/www/html/config# apt install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package vimSo, 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:
Bash
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:
Bash
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.phpAnd 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!
Comments (0)
No comments yet. Be the first to comment.
Leave a comment
Privacy: your name and comment are shown once published. Your e-mail address is never shown or passed on; it is used for the confirmation link and to recognise your later comments, and is deleted when you have no comments left here. Comments that are never confirmed are deleted after 7 days. Confirming sets a cookie in this browser so that it does not ask again.