Docker is an essential tool for the advanced developers. It helps developers to spin up the same working environment irrespective of their operating system, which ensures smooth collaboration during the projects.
🤔 Why do we need to remove the folder?
It is quite common that during the projects, we need to clean up temporary files, remove irrelevant/old uploads, or just keep things organized; we may need to remove a specific folder inside a Docker container.
In this short and practical guide, I’ll walk you through the exact steps to do that using a single docker exec command.
📟 The Command
To remove a folder inside a Docker container, you can use the rm -rf
command through Docker’s exec functionality. Here’s the basic syntax:
docker exec -it <container_Id> rm -rf <path_inside_the_container>
- exec: Run a command inside a running container.
- -it: Attach an interactive terminal session (so you can see what's happening).
- rm -rf: Recursively remove the folder and its contents (forcefully).
- container_Id: Replace this with your actual container ID.
- path_inside_the_container: The path of the folder you want to remove inside the container.
Example:
Let’s say you want to delete the uploads folder from your WordPress site inside a Docker container. Here’s how the command might look:
docker exec -it 5290421feca6 rm -rf /var/www/html/wp-content/uploads/uploads
In this case:
- 5290421feca6 is your container ID.
- /var/www/html/wp-content/uploads/uploads is the full path to the folder you want to delete.
✅ Quick Tips
- Use docker ps to list running containers and find the container ID.
- Run docker exec -it bash to explore the container manually before deleting.
- Consider using a volume mount if you need easier access to file storage for manual cleanup.
- ⚠️ Be careful when using rm -rf, especially inside Docker containers.
- Back up important data.
- Verify the container and path.
- Run the command in a staging environment first, if possible.
🏅Learnings
Removing a folder inside a Docker container is straightforward. However, you cannot undo the deletion process, so run the command with caution. If you accidentally remove any folder, it might break your application. It is a good and safe practice to take a backup of the folder before running the delete command.