Keep Docker Data When Container Is Removed
When you remove a Docker container, its writable layer is deleted along with any files you created inside it. This guide shows you how to use Docker volumes and bind mounts so your data survives container removal, and how to reuse that data in a new container.
What You’ll Need
- A working Docker installation (Docker Engine or Docker Desktop) on your system.
- Basic familiarity with the command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
- An existing container or image you want to protect data for.
Step-by-Step
- Identify what data you want to keep. Common examples: database files, uploaded files, configuration files, or application logs. Determine which directory inside the container holds that data (e.g.,
/var/lib/mysqlfor MySQL,/datafor many apps). - Create a named volume (recommended for most cases). Run
docker volume create mydatato create a volume namedmydata. You can list volumes withdocker volume ls. - Run a new container with the volume attached. Use the
-vflag:docker run -d --name myapp -v mydata:/path/in/container myimage. Replacemydatawith your volume name and/path/in/containerwith the exact path where your data lives. - Write data inside the container as usual. For example, if your app creates files in
/data, they are now stored in the volume, not in the container’s writable layer. - Stop and remove the container. Run
docker stop myappthendocker rm myapp. The volume remains untouched. - Verify the data still exists. Run
docker volume inspect mydatato see the mount point on your host. You can also start a temporary container to list files:docker run --rm -v mydata:/data alpine ls /data. - Reuse the volume with a new container. Start a new container with the same
-v mydata:/path/in/containerflag. Your data will be there. - Alternative: use a bind mount instead of a volume. If you prefer to store data in a specific host directory (e.g.,
/home/user/data), run:docker run -d --name myapp -v /home/user/data:/path/in/container myimage. The host path must be absolute. - Copy data from an existing container to a volume (if you didn’t use a volume from the start). If your current container already has important data, create a volume and copy files:
docker cp myapp:/path/in/container/. mydata:/(this copies the contents of the container path into the volume). Then recreate the container with the volume. - Use
docker-composefor multi-container apps. Define volumes in yourdocker-compose.ymlunder thevolumeskey. For example:volumes:. This makes it easy to recreate services without losing data.
- mydata:/var/lib/mysql
Common Mistakes
- Using anonymous volumes. If you use
-v /container/pathwithout a volume name, Docker creates an anonymous volume. It survives container removal but is hard to reuse because it has no name. Always name your volumes. - Confusing volumes with the container’s writable layer. Any data written to a path not mounted as a volume is lost when the container is removed. Only data inside mounted volumes or bind mounts persists.
- Removing volumes with
docker system prune. By default,docker system pruneremoves unused volumes. Usedocker system prune --volumesonly if you intend to delete them. To avoid accidental deletion, usedocker system prune --filter "label!=keep"or simply avoid pruning volumes. - Incorrect path mapping. If you mount a volume to the wrong path, your app may not find its data. Double-check the container’s expected data directory.
- Forgetting to back up volumes. Volumes are not automatically backed up. If your host disk fails, you lose the data. Regularly back up volume contents.
Tips
- Use named volumes for databases. Database files change frequently; volumes handle this better than bind mounts on some systems.
- Use bind mounts for development. Bind mounts let you edit files on your host and see changes in the container immediately, ideal for code.
- Label your volumes. Add labels like
docker volume create --label project=myapp mydatato help identify them later. - Back up volumes with
docker run --rm -v mydata:/data -v $(pwd):/backup alpine tar czf /backup/mydata.tar.gz -C /data .This creates a tar archive of the volume in your current directory. - Inspect volumes before removal. Use
docker volume inspectto see which containers are using a volume.
Troubleshooting
- Volume not showing data after container recreation. Ensure the mount path in the new container is exactly the same as the path where the app reads/writes. Check with
docker exec -it newcontainer ls /path. - Permission errors. The container process may run as a non-root user, but the volume is owned by root. Use
docker run --user $(id -u):$(id -g)or adjust permissions on the host directory for bind mounts. - Volume is empty after copying with
docker cp.docker cpcopies to the container’s filesystem, not directly to a volume. To copy into a volume, use a temporary container as shown in Step 9. - Cannot remove a volume because it’s in use. Stop and remove any containers using the volume first. Then run
docker volume rm mydata. - Bind mount on Windows path issues. Use the full path with forward slashes, e.g.,
C:/Users/name/data. In Docker Desktop, you may need to share the drive in settings.
Frequently Asked Questions
What is the difference between a volume and a bind mount?
A volume is managed by Docker and stored in a dedicated directory on the host. A bind mount maps any host directory into the container. Volumes are easier to back up and move, while bind mounts give direct access to host files.
Does docker rm delete volumes?
No. Removing a container does not remove named volumes unless you use the -v flag with docker rm (e.g., docker rm -v mycontainer). Anonymous volumes are also removed if you use -v.
Can I attach a volume to a container that already exists?
No. You cannot add a volume to an existing container. You must create a new container with the volume mounted. You can copy data from the old container to the volume first.
How do I back up a Docker volume?
Use a temporary container to tar the volume: docker run --rm -v mydata:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz -C /data . This creates a tar file in your current directory.
What happens to anonymous volumes when I remove a container?
Anonymous volumes are not automatically deleted when the container is removed, but they become orphaned and hard to identify. They can be cleaned up with docker volume prune. It’s better to use named volumes.
Can I share a volume between multiple containers?
Yes. You can mount the same volume in multiple containers. This is useful for sharing configuration or data between services, but be aware of file locking and concurrency issues.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.