Docker containers are ephemeral by design—any data written inside a container’s writable layer disappears when the container is removed. Mounting a volume lets you persist data outside the container’s lifecycle, share files between the host and container, and update configuration without rebuilding the image. While you can’t add a new volume to an already-running container with a single command, this guide shows you the two reliable methods: committing the current state into a new image (then running a new container with the volume) or using the newer docker cp approach for one-off file transfers. You’ll also learn the proper way to attach a volume to an existing container that you want to keep running.
What You’ll Need
- A working Docker installation (Docker Engine 19.03 or later recommended).
- Basic command-line familiarity—you’ll run commands in a terminal or PowerShell.
- Administrative/sudo access if your user isn’t in the
dockergroup. - The name or ID of the running container you want to modify (get it with
docker ps). - An understanding that you cannot attach a volume to a running container without restarting it—plan for a brief downtime.
Step-by-Step
There are three practical approaches. Choose the one that fits your situation:
- Method A: Commit and re-run (most common, preserves container state)
- Stop the running container gracefully:
docker stop <container_name> - Commit the container’s current filesystem (including any changes you’ve made) to a new image:
docker commit <container_name> myapp:with-data - Run a new container from that image, mounting your volume:
docker run -d --name myapp-new -v /host/path:/container/path myapp:with-data - If you need the same network settings, ports, or environment variables, copy them from the original container’s
docker inspectoutput or use a compose file. - Remove the old container once the new one is healthy:
docker rm <old_container_name>
- Stop the running container gracefully:
- Method B: Use
docker cpfor one-time file sync (no restart)- If you just need to get files into or out of the running container without persistent mounting, use
docker cp:docker cp /host/file.txt <container_name>:/container/path/ - This copies files directly, but changes are not automatically synced—it’s a snapshot, not a live mount.
- For ongoing sync, combine with a host-side tool like
rsyncor a bind mount in a new container.
- If you just need to get files into or out of the running container without persistent mounting, use
- Method C: Use a bind mount with
docker run --volumes-from(for sharing volumes between containers)- Create a data-only container or use an existing container that already has the volume you want:
docker create -v /data --name datacontainer busybox - Then run your application container with
--volumes-from datacontainer:docker run -d --name app --volumes-from datacontainer myapp - This shares the volume, but still requires a new container—you can’t attach it to the running one.
- Create a data-only container or use an existing container that already has the volume you want:
If you absolutely must keep the same container ID (e.g., for orchestration), the only way is to stop it, modify its configuration in /var/lib/docker/containers/<id>/config.v2.json (not recommended—fragile and unsupported), or use a tool like docker update (which doesn’t support volumes). In practice, Method A is the standard and safest.
Common Mistakes
- Assuming
docker run -vmodifies an existing container—it only applies to new containers. - Forgetting to stop the container before committing—a running container commit can capture inconsistent state; always stop it first.
- Using a relative host path—Docker requires absolute paths for bind mounts (e.g.,
/home/user/data, not./data). - Overwriting existing data—if the container path already has files, a bind mount will hide them (the host directory takes precedence).
- Not checking permissions—the container user may not have write access to the host directory, causing errors.
Tips
- Use named volumes (
-v myvolume:/container/path) instead of bind mounts for better portability and easier backup withdocker volumecommands. - Always test the mount in a staging environment before applying to production.
- Document your volume mappings in a
docker-compose.ymlfile—it’s much easier to recreate containers with volumes than to retrofit them. - If you need to add a volume to a running container in a swarm or Kubernetes, plan for rolling updates—there’s no live-attach.
- Use
docker inspect <container>to verify mounts after starting a new container: look at theMountssection.
Troubleshooting
- Mount not appearing inside container—Check the path: the container path must exist and be absolute. Verify with
docker inspectanddocker exec -it <container> ls /container/path. - Permission denied—Run
chmod 755orchownon the host directory to match the container user’s UID/GID. Or run the container with--user root(not recommended for production). - Data appears empty after mounting—If the host directory is empty, it will shadow the container’s existing files. Copy the container’s data out first with
docker cp, then mount. - Container won’t start after commit—Check the entrypoint and command in the original image; the commit preserves them, but if you changed working directory or environment, it may break. Inspect with
docker inspect. - Volume persists after container removal—That’s normal for named volumes. To remove it, use
docker volume rm <volume_name>(only after stopping containers using it). - Can’t attach to a running container with
docker run—Remember:docker runcreates a new container. If you need to keep the same container, you must stop, commit, and re-run.
Frequently Asked Questions
- Can I add a volume to a running container without stopping it? No, Docker doesn’t support live volume attachment. You must stop the container, then either commit and re-run or use
docker cpfor one-time file copies. - What’s the difference between a bind mount and a named volume? A bind mount maps a specific host directory (e.g.,
/home/user/data) into the container. A named volume is managed by Docker (stored in/var/lib/docker/volumes/) and is easier to back up and move. - Will my data survive if I remove the container? With a bind mount or named volume, yes—the data lives on the host or in the volume, not in the container’s writable layer. With
docker cp, the copy is static; changes after the copy aren’t synced. - How do I mount a volume to a container started with docker-compose? You can’t change a running compose service’s volumes. Instead, edit the
docker-compose.ymlfile, then rundocker-compose up -dto recreate the container with the new volume. - Can I mount the same volume to multiple containers? Yes, use the same named volume or host path in multiple
docker run -vcommands. Be careful with concurrent writes—use a filesystem that supports locking. - Is there a way to mount a volume to a running container in Docker Desktop? The same rules apply—Docker Desktop is just Docker under the hood. You’ll still need to stop and re-run the container.