skip to Main Content

Is it possible to rename a docker volume? I want to change the volume names of the existing container. I see that config.json and hostconfig.json has the volume details in it.

docker run -di -p 8083:443 -v app_main_db_test_1:/var/lib/pgsql/data  -v app_main_conf_test_1:/var/www/ ubuntu

I want to change app_main_db_test_1 to app_main_db and app_main_conf_test_1 to app_main_conf

3

Answers


  1. Chosen as BEST ANSWER

    Figured it out. Thanks to this github post.

    # Create new Volume for DB and copy files from old volume
    docker volume create --name app_main_db 
    docker run --rm -it -v app_main_db_test_1:/from -v app_main_db:/to alpine ash -c "cd /from ; cp -av . /to"
    
    # Create new Volume for conf and copy files from old volume
    docker volume create --name app_main_conf
    docker run --rm -it -v app_main_conf_test_1:/from -v app_main_conf:/to alpine ash -c "cd /from ; cp -av . /to"
    
    
    # Start the container using new volumes
    docker run -di -p 8083:443 -v app_main_db:/var/lib/pgsql/data  -v app_main_conf:/var/www/ ubuntu
    
    # Delete old volumes
    docker volume rm app_main_db_test_1
    docker volume rm app_main_conf_test_1
    

  2. as far as i know there are no ways of renaming docker volume so far. There is an open github issue, which indicates there is no solution to the topic yet.

    But there are a few useful ways how to do so. Since you are saying you use Docker Desktop you could check this comment:

    docker volume create --name <new_volume>
    docker run --rm -it -v <old_volume>:/from -v <new_volume>:/to alpine ash -c "cd /from ; cp -av . /to"
    docker volume rm <old_volume>
    

    Which should do exactly what you are planning to do.

    Login or Signup to reply.
  3. Please don’t try this on a production machine, however on my dev machine (ubuntu), i had two containers:

    docker volume ls
    DRIVER    VOLUME NAME
    local     v1_sail-mysql
    local     v2_sail-mysql
    

    I wanted to switch volumes like this

    mysql_1 -> v1_sail-mysql (originally v2_sail-mysql)
    mysql_2 -> v2_sail-mysql (originally v1_sail-mysql)
    

    My docker volumes are located in /home/docker/volumes, in your case it might be /var/lib/docker/volumes

    I just renamed the volumes

    mv v1_sail-mysql v1_sail-mysql-tmp
    mv v2_sail-mysql v1_sail-mysql
    mv v1_sail-mysql-tmp v2_sail-mysql
    

    Restarted the containers which indeed switch the containers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search