Docker
Shell Commands
File Editing
Docker Container
Tech Tutorial

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.

Practice system design

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:

bash
docker exec -it <container_id_or_name> /bin/bash

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:

bash
apt-get update
apt-get install nano

Or, for vim:

bash
apt-get update
apt-get install vim

Editing a File:

Once the editor is installed, you can navigate to the directory containing your file and open it for editing:

bash
nano /path/to/your/file

Or with vim:

bash
vim /path/to/your/file

Make the necessary changes and save them. How you save changes will depend on the text editor:

  • In nano, press Ctrl+O to write out the changes and Ctrl+X to exit.
  • In vim, press Esc, then type :wq and hit Enter to 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:

bash
docker commit <container_id_or_name> new_image_name:tag

You can then use this new image to create new containers that will include your changes.

Summary of Key Commands

CommandDescriptionExample
docker exec -itAccess the container shelldocker exec -it my-container /bin/bash
apt-get installInstall packages in Debian-based containersapt-get install nano
vim or nanoOpen a file in the editorvim /path/to/file
docker commitCreate a new image with current statedocker 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design