How can I add a volume to an existing Docker container?

Better Stack Team
Updated on April 14, 2023

You cannot directly add a volume to an existing Docker container, but you can create a new container with the same configuration as the existing container, but with an additional volume mounted. Here are the steps:

  1. Create a new volume that you want to attach to the container:

     
    docker volume create my-new-volume
    
  2. Create a new container with the same configuration as the existing container, but with the additional volume mounted:

     
    docker run -d --name my-new-container \
    --volumes-from my-existing-container \
    -v my-new-volume:/path/to/new/volume \
    my-image
    

    This will create a new container called my-new-container that is based on the same image as my-existing-container. The --volumes-from option tells Docker to mount all the volumes from the existing container to the new container. The -v option mounts the new volume to the new container. You can replace /path/to/new/volume with the path where you want to mount the volume in the new container.

  3. Stop and remove the old container:

     
    docker stop my-existing-container
    docker rm my-existing-container
    

    This will stop and remove the old container. You can also choose to keep the old container running while you test the new container to make sure everything is working correctly.

  4. Rename the new container to the name of the old container:

     
    docker rename my-new-container my-existing-container
    

    This will rename the new container to the same name as the old container, so any scripts or commands that reference the old container by name will still work.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack