How to Install Vim in a Docker Container
You are likely to not find Vim editor installed in your Docker container. Here's how to get it.
It's almost certain that the Linux distribution you are running in a Docker container doesn't have Vim or any other text editor installed by default.
A quick way to install the text editor in your Docker container would be to enter the running container:
docker exec -it container_name_or_ID sh
Verify which Linux distribution it uses:
cat /etc/os-release
And then use the package manager of the distribution to install it.
To install Vim on Ubuntu or Debian, use the apt command:
apt update
apt install vim
To install it on CentOS or Red Hat, use the Yum command:
yum install vim
And if it's Alpine Linux, use the apk command:
apk update
apk add vim
It should allow you to run and use Vim in the currently running container. But there is a big problem with this approach. If you run a new container with the same Docker image, the Vim command you had installed will not be present in this new container. You'll have to install it again.
If you want that all the containers that are created using the given Docker image also have Vim installed by default, you need to add the installation commands in the Dockerfile.
I hope you know how to create custom Docker image with Dockerfile. If not, let me quickly remind you with a simple example of Alpine Linux.
Create a new file named Dockerfile:
touch Dockerfile
Now open this Dockerfile for editing in an editor and add the following lines to it and save it:
FROM alpine:latest
RUN apk update
RUN apk add vim
Basically, you are instructing Docker to pull the latest Alpine docker image and then run the apk packgae manager to update the cache and install Vim.
You have to create this custom Docker image from the above mentioned Dockerfile. Run a command like this to create your custom Docker image:
docker build -t new_docker_image_name PATH_to_Dockerfile
Now if you run any container with this new, custom Docker image, it should have Vim editor installed already.
I hope this quick Docker tip was helpful to you.
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.