1

It's CentOS 7, already installed vi and vim in my CentOS and I can use them. I run docker in CentOS, when I excute this line below:

docker exec -it mysolr /bin/bash

I cannot use vi/vim in the solr container:

bash: vim: command not found

Why is that and how do I fix it so I can use vi/vim to edit file in docker container?

Waylan Punch
  • 239
  • 4
  • 17
  • 2
    Docker images contain minimal required libraries to run the application, most of the time you do not get vi, vim or other libs like curl with them Either use any other docker base image and built you own container with installing necessery softwares or install vi in your base with commands like apk add for alpine or apt get install for linux base – Ganesh Karewad Jun 08 '19 at 13:32
  • Possible duplicate: [How to run vi on docker container?](https://stackoverflow.com/q/31515863/596285) – BMitch Jun 08 '19 at 14:03
  • 1
    @BMitch Well, in CentOS it must be `yum/dnf`, not `apt`. – phd Jun 08 '19 at 14:25
  • 1
    @phd yes, see my answer on that same question that covers the popular scenarios. https://stackoverflow.com/a/48097074/596285 – BMitch Jun 08 '19 at 16:25

1 Answers1

2

A typical Docker image contains a minimal set of libraries and utilities to run one specific program. Additionally, Docker container filesystems are not long-lived: it is extremely routine to delete and recreate a container, for instance to use a newer version of a base image.

The upshot of this is that you never want to directly edit files in a Docker container, and most images aren't set up with "rich" editing tools. (BusyBox contains a minimal vi and so most Alpine-based images will too.) If you make some change, it will be lost as soon as you delete the container. (Similarly, you usually can install vim or emacs or whatever, but it will get lost as soon as the container is deleted: installing software in a running container isn't usually a best practice.)

There are two good ways to deal with this, depending on what kind of file it is.

If the file is part of the application, like a source file, edit, debug, and test it outside of Docker space. Once you're convinced it's right (by running unit tests and by running the program locally), docker build a new image with it, and docker run a new container with the new image.

ed config.py
pytest
docker build -t imagename .
docker run -d -p ... --name containername imagename
...
ed config.py
pytest
docker build -t imagename .
docker stop containername
docker run -d -p ... --name containername imagename

If the file is configuration that needs to be injected when the application starts, the docker run -v option is a good way to push it in. You can directly edit the config file on your host, but you'll probably need to restart (or delete and recreate) the container for it to notice.

ed config.txt
docker run \
  -v $PWD/config.txt:/etc/whatever/config.txt \
  --name containername -p ... \
  imagename
...
ed config.txt
docker stop containername
docker rm containername
docker run ... imagename
David Maze
  • 130,717
  • 29
  • 175
  • 215