ThisCalculator

Everyday

Keep Docker Data When Container Is Removed

Updated · Standard formula, shown below

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

  1. 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/mysql for MySQL, /data for many apps).
  2. Create a named volume (recommended for most cases). Run docker volume create mydata to create a volume named mydata. You can list volumes with docker volume ls.
  3. Run a new container with the volume attached. Use the -v flag: docker run -d --name myapp -v mydata:/path/in/container myimage. Replace mydata with your volume name and /path/in/container with the exact path where your data lives.
  4. 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.
  5. Stop and remove the container. Run docker stop myapp then docker rm myapp. The volume remains untouched.
  6. Verify the data still exists. Run docker volume inspect mydata to 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.
  7. Reuse the volume with a new container. Start a new container with the same -v mydata:/path/in/container flag. Your data will be there.
  8. 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.
  9. 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.
  10. Use docker-compose for multi-container apps. Define volumes in your docker-compose.yml under the volumes key. For example: volumes:
    - mydata:/var/lib/mysql
    . This makes it easy to recreate services without losing data.

Common Mistakes

  • Using anonymous volumes. If you use -v /container/path without 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 prune removes unused volumes. Use docker system prune --volumes only if you intend to delete them. To avoid accidental deletion, use docker 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 mydata to 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 inspect to 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 cp copies 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.