How to Edit a File After You Shell to a Docker Container
TLDR
To edit a file inside a running Docker container, shell in with docker exec -it <container> sh or bash, then use a text editor like vi or nano. For persistent changes, update your Docker image or use volumes.
Sometimes you need to make a quick change inside a running Docker container - maybe to debug, patch a config, or test a fix. Docker makes it easy to get a shell inside your container, but there are a few things to keep in mind.
Getting a Shell Inside the Container
First, find your container's name or ID:
docker ps
Then shell in:
# Use sh (for Alpine or minimal images)
docker exec -it <container_name_or_id> sh
# Or bash (for Ubuntu/Debian-based images)
docker exec -it <container_name_or_id> bash
Now you're inside the container and can navigate the filesystem.
Editing Files with Built-in Editors
Most containers include vi or vim. Some minimal images (like Alpine) may only have vi or no editor at all. Try:
vi /path/to/file
If you prefer nano and it's not installed, you can add it (if the container has a package manager):
# For Alpine
apk add nano
# For Debian/Ubuntu
apt-get update && apt-get install nano
Then edit your file:
nano /path/to/file
Making Changes Persistent
Edits made inside a running container are lost if the container is deleted. For changes you want to keep:
- Update your Dockerfile and rebuild the image.
- Use Docker volumes to mount files from your host.
- Commit changes to a new image (not recommended for production):
docker commit <container_name_or_id> my-edited-image:latest
This creates a new image with your changes, but it's better to update the Dockerfile for reproducibility.
Best Practices
- Use shell edits for debugging, not for production changes.
- Always document any manual changes.
- Prefer updating your Dockerfile for reproducibility.
With these steps, you can quickly edit files inside containers and understand how to make those changes stick.
Related Resources
- How to Access Docker Container Shell — shell access methods
- Copy Files from Docker Container to Host — extract files
- Exploring Docker Container File System — navigate containers
- Introduction to Docker Guide — Docker fundamentals
- Docker Flashcards — review Docker concepts
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
5 Advanced Docker Features Worth Knowing
Go beyond Docker basics with BuildKit, multi-stage builds, health checks, init processes, and build secrets. Learn practical techniques that improve security, performance, and reliability.
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes