204

I have installed docker on my host virtual machine. And now want to create a file using vi.

But it's showing me an error:

bash: vi: command not found
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
Krati Jain
  • 2,059
  • 2
  • 11
  • 3
  • 1
    vi may not be installed inside container. Which container have you installed and run? – Hüseyin BABAL Jul 20 '15 at 11:57
  • 1
    You have to be more specific. Are you trying to run vi inside a container, if so which image is the container based on? In all likelihood you don't have vi installed. (apt-get install vim) – wpp Jul 20 '15 at 12:51
  • It depends on which `image` you are building from. Most probably `image` you are using is so lighter that it only has things you need to run as an image. You need to manually install packages you need. – Shiva Jul 20 '15 at 13:59
  • 3
    You probably shouldn't be doing config inside a container. Do it in the Dockerfile instead. – Adrian Mouat Jul 20 '15 at 14:41
  • are you using boot2docker? the host file system has probably been mapped onto your docker engine to allow you edit files from there rather than inside the docker engine vm. – booyaa Jul 20 '15 at 17:51
  • 3
    Possible duplicate of [How to edit file after I shell to a docker container?](http://stackoverflow.com/questions/30853247/how-to-edit-file-after-i-shell-to-a-docker-container) – A.B. Jan 29 '16 at 12:21
  • It‘s the same like https://stackoverflow.com/questions/30853247/how-do-i-edit-a-file-after-i-shell-to-a-docker-container/43042406#43042406 – yan Jan 10 '19 at 07:32
  • https://stackoverflow.com/a/58267453/2568750 – Yogi Ghorecha Apr 24 '20 at 14:26
  • NOTE: I had a problem to update apt and install vim due unreachable name servers in `/etc/resolv.conf`. Therefore for me was crucial before to fix the setup: `echo "nameserver 8.8.8.8" > /etc/resolv.conf`. – pevik Jun 19 '23 at 09:16

15 Answers15

278

login into container with the following command:

docker exec -it <container> bash

Then , run the following command .

apt-get update
apt-get install vim
codeX
  • 4,842
  • 2
  • 31
  • 36
54

The command to run depends on what base image you are using.

For Alpine, vi is installed as part of the base OS. Installing vim would be:

apk -U add vim

For Debian and Ubuntu:

apt-get update && apt-get install -y vim

For CentOS, vi is usually installed with the base OS. For vim:

yum install -y vim

This should only be done in early development. Once you get a working container, the changes to files should be made to your image or configs stored outside of your container. Update your Dockerfile and other files it uses to build a new image. This certainly shouldn't be done in production since changes inside the container are by design ephemeral and will be lost when the container is replaced.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    ERROR: Unable to lock database: Permission denied – garg10may Dec 06 '19 at 08:04
  • 1
    @garg10may commands need to be run as root. `USER root` inside the Dockerfile does this, but be sure to switch back to your other user. I'm a running container: `docker exec -u root ...` – BMitch Dec 06 '19 at 10:20
  • great answer since it is most useful when being in container and not knowing what Linux is the container built upon – Tomáš Záluský Jan 14 '22 at 12:13
44

USE THIS:

apt-get update && apt-get install -y vim

Explanation of the above command

  1. apt-get update => Will update the current package
  2. apt-get install => Will install the package
  3. -y => Will by pass the permission, default permission will set to Yes.
  4. vim => Name of the package you want to install.
Yogi Ghorecha
  • 1,584
  • 1
  • 19
  • 26
40

Your container probably haven't installed it out of the box.

Run apt-get install vim in the terminal and you should be ready to go.

Englund
  • 1,067
  • 7
  • 13
16

error:: bash: vi: command not found

run the below command by logging as root user to the container--

docker exec --user="root" -it (container ID) /bin/bash
apt-get update
apt-get install vim
Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
Kripa Mishra
  • 331
  • 3
  • 5
15

Add the following line in your Dockerfile then rebuild the docker image.

RUN apt-get update && apt-get install -y vim
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CJ Chang
  • 325
  • 4
  • 12
13

Alternatively, keep your docker images small by not installing unnecessary editors. You can edit the files over ssh from the docker host to the container:

vim scp://remoteuser@container-ip//path/to/document
Matthew
  • 6,351
  • 8
  • 40
  • 53
  • 7
    Wouldn't this only work if the [container is running ssh](https://docs.docker.com/engine/examples/running_ssh_service/)? I thought it was a brilliant idea at first, but cannot seem to get it to work. – Kevin Aug 30 '19 at 22:35
10

The most voted answer has the correct idea, however, it did not work in my case. The comment from @java25 did the trick in my case. I had to log into the docker container as a root user to install vim. I am just posting the comment as an answer so that it is easier for others, having the similar problem, to find it.

docker exec -ti --user root <container-id> /bin/bash

Once you are inside docker, run the following commands now to install vi.

apt-get update
apt-get install vim
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
9

Use below command in Debian based container:

apt-get install vim-tiny

Complete instruction for using in Dockerfile:

RUN apt-get update && apt-get install --no-install-recommends -y \   
 vim-tiny \  
 && apt-get clean && rm -rf /var/lib/apt/lists/*

It doesn't install unnecessary packages and removes unnecessary downloaded files, so your docker image size won't increase dramatically.

4

To install within your Docker container you can run command

docker exec apt-get update && apt-get install -y vim

But this will be limited to the container in which vim is installed. To make it available to all the containers, edit the Dockerfile and add

RUN apt-get update && apt-get install -y vim

or you can also extend the image in the new Dockerfile and add above command. Eg.

FROM < image name >

RUN apt-get update && apt-get install -y vim

Community
  • 1
  • 1
MB11
  • 610
  • 1
  • 5
  • 16
2

Inside container (in docker, not in VM), by default these are not installed. Even apt-get, wget will not work. My VM is running on Ubuntu 17.10. For me yum package manager worked.

Yum is not part of Debian or ubuntu. It is part of red-hat. But, it works in Ubuntu and it is installed by default like apt-get

To install vim, use this command

yum install -y vim-enhanced 

To uninstall vim :

yum uninstall -y vim-enhanced 

Similarly,

yum install -y wget 
yum install -y sudo 

-y is for assuming yes if prompted for any question asked after doing yum install package-name

Satish Patro
  • 3,645
  • 2
  • 27
  • 53
1

error:: bash: vim: command not found

Run the below command by logging as root user to the container:

microdnf install -y vim
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

If you actually want a small editor for simple housekeeping in a docker, use this in your Dockerfile:

RUN apt-get install -y busybox && ln -s /bin/busybox /bin/vi

I used it on an Ubuntu 18 based docker. (Of course you might need an RUN apt-get update before it but if you are making your own Docker file you probably already have that.)

Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
0

Usually changing a file in a docker container is not a good idea. Everyone will forget about the change after a while. A good way is to make another docker image from the original one.

Say in a docker image, you need to change a file named myFile.xml under /path/to/docker/image/. So, you need to do.

  1. Copy myFile.xml in your local filesystem and make necessary changes.
  2. Create a file named 'Dockerfile' with the following content-
FROM docker-repo:tag
ADD myFile.xml /path/to/docker/image/

Then build your own docker image with docker build -t docker-repo:v-x.x.x .

Then use your newly build docker image.

misbah
  • 179
  • 6
0

Start a Docker container:

docker run -it <image_name> /bin/bash

Install Vim:

sudo apt update
sudo apt install vim
  • Welcome to SO. If you look at the 14 existing answers, you'll notice that what you write has been posted before (with some variations). So your answer is unlikely to help anyone. Anyhow, thank you for writing an answer, keep it up! – Friedrich Jun 21 '23 at 08:00