Skip to content
NetworkTechGuy

How to edit file in a docker container when no editor is installed

By Nenad Karlovcec1 min read

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 found

Of 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 candidate

or

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 vi

or

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 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:

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.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!

Comments (0)

No comments yet. Be the first to comment.

Leave a comment

Never shown. A confirmation link is sent to it.

Plain text. A comment with more than one link waits for review.

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.