[linux] How to run vi on docker container?

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

This question is related to linux docker vim text-editor vi

The answer is


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.

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


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

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

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

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.


If you need to change a file just once. You should prefer making the change locally and build a new docker image with this file.

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.


login into container with the following command:

docker exec -it <container> bash

Then , run the following command .

apt-get update
apt-get install vim

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

Tu 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 qustion asked after doing yum install packagename


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.


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


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

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.


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to docker

standard_init_linux.go:190: exec user process caused "no such file or directory" - Docker What is the point of WORKDIR on Dockerfile? E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation How do I add a user when I'm using Alpine as a base image? docker: Error response from daemon: Get https://registry-1.docker.io/v2/: Service Unavailable. IN DOCKER , MAC How to fix docker: Got permission denied issue pull access denied repository does not exist or may require docker login Docker error: invalid reference format: repository name must be lowercase Docker: "no matching manifest for windows/amd64 in the manifest list entries" OCI runtime exec failed: exec failed: (...) executable file not found in $PATH": unknown

Examples related to vim

Why does using from __future__ import print_function breaks Python2-style print? How to run vi on docker container? How can I install MacVim on OS X? Find and replace strings in vim on multiple lines Running Python code in Vim How do I set the default font size in Vim? Move cursor to end of file in vim Set encoding and fileencoding to utf-8 in Vim How to select all and copy in vim? Why I've got no crontab entry on OS X when using vim?

Examples related to text-editor

Find duplicates and delete all in notepad++ How to run vi on docker container? How to run a program in Atom Editor? How to call VS Code Editor from terminal / command line What is the difference between Sublime text and Github's Atom Multiple select in Visual Studio? change cursor from block or rectangle to line? Javascript button to insert a big black dot (•) into a html textarea Vim multiline editing like in sublimetext? Edit a text file on the console using Powershell

Examples related to vi

How to run vi on docker container? Find and replace strings in vim on multiple lines How can I delete multiple lines in vi? Is there a short cut for going back to the beginning of a file by vi editor? vi/vim editor, copy a block (not usual action) How to go back (ctrl+z) in vi/vim How do I exit the Vim editor? vim line numbers - how to have them on by default? Go to beginning of line without opening new line in VI How to take off line numbers in Vi?