How to run vi on docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To run vi inside a Docker container, you first need an interactive shell in that container and an image that actually contains vi or vim. The command is simple, but the bigger practical issue is whether editing inside the container is the right workflow, because many containers are minimal and container filesystem changes are often ephemeral.
Enter the Container Interactively
If the container is already running, attach to it with docker exec:
If the image has Bash, you can use that instead:
Once you are inside, check whether vi exists:
If that fails, the editor is not installed.
Install vi or vim if Needed
Many slim images do not ship with editors. The install command depends on the base distribution.
Debian or Ubuntu:
Alpine:
RHEL or CentOS style images:
Then you can edit a file normally:
Start a New Interactive Container
If you are not attaching to an existing container and only want a temporary shell, run the image interactively from the start:
Then install vim if the image does not already contain it.
This is useful for debugging or experimenting, but it is not the usual production configuration workflow.
Why Editing Inside the Container Is Often Temporary
One of the most important Docker facts is that a container is not the long-term source of truth. If you edit a file directly inside a running container and that container is recreated, your changes may disappear.
That is why many teams prefer one of these approaches instead:
- edit the file on the host and mount it into the container
- update the Docker image with a new build
- use an environment variable or external config source instead of editing in place
For example, mounting a directory from the host makes edits persistent:
Now the files live on the host, and container restarts do not erase them.
Basic vi Commands Still Matter
If you do not use vi often, the minimum commands are:
- press
ito enter insert mode - press
Escto leave insert mode - type
:wqto save and quit - type
:q!to quit without saving
That is usually enough for one-off edits in a container.
A Safer Debugging Pattern
If your goal is to inspect or patch a container for debugging, a typical sequence is:
Then, once you understand the fix, apply the same change to the real source of truth such as the Dockerfile, Helm chart, compose file, or mounted configuration directory.
That prevents the classic mistake of fixing production manually inside one container and then losing the change at the next restart.
Common Pitfalls
- Assuming every image has
viinstalled is wrong, especially for slim or security-focused images. - Editing inside a running container and expecting the change to survive recreation is a common Docker misunderstanding.
- Using
docker attachwhen you really want a fresh shell can produce confusing results;docker exec -itis usually the better tool. - Installing packages into a production container for convenience can drift it away from the image definition.
- Forgetting whether the shell is
shorbashcauses avoidable friction when attaching to minimal images.
Summary
- Use
docker exec -it container-name shorbashto open an interactive shell. - Install
viorvimif the image does not already include it. - Run
vi path/to/fileonce you are inside the container. - Remember that edits inside a container are often temporary unless the file is backed by a volume or baked into a new image.
- For durable changes, update the image, config mount, or deployment source rather than relying on manual in-container edits.
Related reading
- How to see docker image contents
- How to see docker image contents
- How to see Pod logs a container name must be specified for pod... choose one of wait main
- How to see the logs of a docker container
- how to see the pod and veth relationship in kubernetes
- How to send message in rabbitmq on docker?
- How to serve multiple versions of model via standard tensorflow serving docker image?
- How to set heap memory in cassandra on docker

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.