Skip to main content

Command Palette

Search for a command to run...

How to mount storage on a running docker container

Updated
2 min read
How to mount storage on a running docker container

Mounting storage on a running Docker container isn’t directly supported, but there are reliable workarounds to achieve similar results. Below, you’ll find two practical methods with step-by-step instructions and relevant citations.


Method 1: Copy Files Directly with docker cp

While you cannot mount a new volume on a running container, you can transfer files between your host and the container using the docker cp command. This is useful for one-time file transfers but does not create a persistent mount.

Command Syntax:

bash# Copy from container to host
docker cp <containerId/containerName>:/path/in/container /path/on/host

# Copy from host to container
docker cp /path/on/host <containerId/containerName>:/path/in/container

Example:

bashdocker cp my_container:/app/config.json ./local_dir
docker cp ./updated_file.txt my_container:/app/data

Limitations:

  • No real-time synchronization; changes are not automatically reflected.

  • Manual process; must be repeated for each transfer2.

For more details, see the [Docker documentation on docker cp]2.


Method 2: Commit and Recreate the Container with a Volume

For persistent storage, the best approach is to commit your running container to a new image and then launch a new container with the required volume mount.

Steps:

  1. Commit the existing container:

     bashdocker commit <containerId or containerName> new_image_name
    
  2. Run a new container with a volume:

     bashdocker run -v /host/path:/container/path -it new_image_name /bin/bash
    

Example workflow:

bashdocker commit agitated_newton my_ubuntu
docker run -v "$PWD/somedir":/somedir -it my_ubuntu /bin/bash

Advantages:

  • Enables persistent storage via Docker-managed volumes or bind mounts16.

  • Retains changes made in the original container.

Note: This method requires stopping the old container and using the new one for continued work4.


Why Can’t You Mount Storage Directly on a Running Container?

Docker’s architecture does not support adding new mounts to a running container. Mounts (volumes, bind mounts, tmpfs) must be specified at container creation using the --mount or -v flags16. For persistent or shared storage, always plan your mounts before starting the container.


Summary Table

ScenarioMethodReference
One-time file transferdocker cp2
Persistent storage neededCommit & rerun146