1. Overview

In this tutorial, we’ll learn how to use the vi command inside a Docker container. A basic understanding of Linux and Docker is required to fully understand this tutorial. We’ll look into four types of Linux distributions, namely, Ubuntu, Debian, CentOS, and Fedora.

Docker is a containerization platform that allows packaging our application and its dependencies into a single unit.

As a best practice, we should keep the Docker image size small. Therefore, we install only those packages inside the Docker container which are necessary to run our application.

2. For Ubuntu/Debian

The official Docker image of Ubuntu and Debian does not contain the vim package. Let’s verify this quickly:

$ docker run -itd --name ubuntu ubuntu:latest
dbfaa02b8c3c5bc434af84161571c3d481c370327063f807eb670c5ae3c441aa
$ docker exec -it ubuntu bash -c "apt list --installed | grep vim"
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

First, we have launched an Ubuntu Docker container with its latest Docker image. Furthermore, apt list –installed | grep vim will verify whether the vim package is present inside the container. Since the output is empty (ignore the warning), we can conclude that the vim package is not present in the container.

Now, let’s install the vim package manually inside the container:

$ docker exec -it ubuntu bash -c "apt-get update && apt-get install -y vim"

Here, first, we’re updating the packages using the command apt-get update, and then we install the vim package using the command apt-get install -y vim:

$ docker exec -it ubuntu bash -c "apt list --installed | grep vim"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

vim-common/focal,now 2:8.1.2269-1ubuntu5 all [installed,automatic]
vim-runtime/focal,now 2:8.1.2269-1ubuntu5 all [installed,automatic]
vim/focal,now 2:8.1.2269-1ubuntu5 amd64 [installed]

Since we have installed the vim package above, the output of the command apt list –installed | grep vim is not empty this time.

All these commands will work for the Debian Docker container as well.

3. For CentOS/Fedora

The official Docker image of CentOS and Fedora includes the minimal installation of the vim package which has the capability to perform all the basic file edit operations.

Now let’s launch a centos Docker container using its latest Docker image. Again, we’ll verify if the vim package is available inside the container using the command rpm -qa | grep vim.

$ docker run -itd --name centos centos:latest
0a6b12547c61ce6859f48f56180d758692bf481dd854412f29c94f0cb1a3d0ca
$ docker exec -it centos bash -c "rpm -qa | grep vim"
vim-minimal-8.0.1763-15.el8.x86_64

We can install the complete vim package if required:

$ docker exec -it centos bash -c "yum update -y && yum install -y vim"

The yum update command will update the packages to their latest version. Finally, we’ll install the vim package using yum install -y vim. Both the commands will execute inside the Docker container.

Again, let’s verify all the vim packages installed inside the container:

$ docker exec -it centos bash -c "rpm -qa | grep vim"
vim-filesystem-8.0.1763-15.el8.noarch
vim-minimal-8.0.1763-15.el8.x86_64
vim-common-8.0.1763-15.el8.x86_64
vim-enhanced-8.0.1763-15.el8.x86_64

All the commands discussed in this section will work on Fedora Docker containers as well.

4. Create or Edit a File Using the vi Editor

Now that we have installed the vim package inside the Docker container, we can use the vi command to create and edit files:

$ docker exec -it centos bash
$ vi /baeldung.txt

Here, we’re starting an interactive Bash shell inside the Docker container and then creating the baeldung.txt file inside the / directory. If the baeldung.txt file is already present, it will open the file in the vi editor.

In this example, we should note that control will remain inside the Docker container. Any operation performed after this will execute inside the Docker container.

Let’s look at another way to perform the same operation using a single command:

$ docker exec -it centos bash -c "vi /baeldung.txt"

This time, the control will remain on the host machine. Any operation performed after this will execute on the host machine.

To get an in-depth understanding of the vi editor and its different operations, it is highly recommended to read our guide.

5. Best Practices

Ideally, application development includes three major stages, namely, development, testing, and deployment. We should test the source code thoroughly before its deployment. Finally, we use Docker containers to deploy our source code.

At the deployment stage, Docker is used to package the application source code and all of its dependencies into a single entity. We should not update the source code once the application is deployed. All the changes should be done in the development stage only.

Hence, packages like vim, which is used to edit files, should be avoided inside the Docker image.

5.1. Use Docker Volume Mount

Even if, for any reason, we want to edit any file inside the Docker container, we should use Docker volumes.

Using Docker volumes, we can mount the source code directory onto a specific directory on the host machine. This way, any changes done inside the file present on the host machine will reflect the changes inside the Docker container.

5.2. Use nano/vim-tiny

The vim package requires disk space of approx 50 MB for installation. As an alternative, we can also use packages like nano or vim-tiny (available in Ubuntu only) for file editing. These packages require disk space of a few KB’s only. Therefore, our Docker image size will be small.

6. Conclusion

In this article, we went through the installation process of the vim package for different Linux distributions. Moreover, we have also examined the reason to avoid the installation of the vim package inside Docker containers.

Finally, we’ve discussed a couple of alternatives to the vim package which are small in size.

Comments are closed on this article!