How do I edit a file after I shell to a Docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Docker containers, you may sometimes need to modify files inside the container either for debugging or configuration purposes. Docker containers are instances of Docker images, and any changes made in a container will not persist if you create a new container from the same image unless you commit those changes to the image. Here’s a step-by-step guide on how to edit files once you've shelled into a Docker container.
Step 1: Accessing the Docker Container
First, you need to access the shell of the running Docker container. To do this, you use the docker exec command combined with an interactive terminal option. Assume your container ID or name is known, you can use the following command:
This command will open a Bash shell inside the container if Bash is available. If /bin/bash doesn't work, you might need to try /bin/sh or another shell present in the container.
Step 2: Editing Files within the Container
Once inside the container, you need a text editor to modify any files. Common text editors available in many Linux distributions include vi, vim, nano, etc. If your preferred text editor is not installed, you can install it using the container’s package manager (e.g., apt for Debian-based containers or yum for Red Hat-based containers).
Installing a Text Editor:
Here's how to install nano, a straightforward and easy-to-use text editor:
Or, for vim:
Editing a File:
Once the editor is installed, you can navigate to the directory containing your file and open it for editing:
Or with vim:
Make the necessary changes and save them. How you save changes will depend on the text editor:
- In
nano, pressCtrl+Oto write out the changes andCtrl+Xto exit. - In
vim, pressEsc, then type:wqand hitEnterto save and exit.
Step 3: Saving Changes Permanently
Changes you make inside a container are temporary and will be lost if the container restarts. If you need to keep the changes, you must commit the container to a new Docker image:
You can then use this new image to create new containers that will include your changes.
Summary of Key Commands
| Command | Description | Example |
docker exec -it | Access the container shell | docker exec -it my-container /bin/bash |
apt-get install | Install packages in Debian-based containers | apt-get install nano |
vim or nano | Open a file in the editor | vim /path/to/file |
docker commit | Create a new image with current state | docker commit my-container new_image_name:tag |
Additional Considerations
- Non-Persistent Storage: Remember that not all containers are meant to have their files edited. Always ensure that this approach matches the intended use of the container.
- Container Best Practices: It’s generally best to use Docker volumes or bind mounts for data that needs to be preserved or shared across containers, rather than modifying the container filesystem directly.
By following these steps and using the tools provided within the Docker ecosystem, you can effectively manage and modify the contents of your Docker containers.
Related reading
- How do I edit a file after I shell to a Docker container?
- How do I force Kubernetes to re-pull an image?
- How do I get into a Docker container's shell?
- How do I make a comment in a Dockerfile?
- How do I make a Docker container start automatically on system boot?
- How do I mount a host directory as a volume in docker compose
- How do I pass environment variables to Docker containers?
- How do I pass environment variables to Docker containers?

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.