Max R.
Max R.

Reputation: 853

Rename docker-compose project without deleting volumes

I want to rename a docker-compose project (i.e by renaming the directory or adding -p new_name to docker-compose). If I do so I'll remove all my old containers and also all my old volumes. Is there a way to keep the volumes and attach them to the new docker-compose container?

Example docker-compose.yml

version: '3'

services:
  dashboard:
    build: custom_dashboard
    volumes:
      - dashboard:/var/lib/grafana
    ports:
      - 3000:3000
volumes:
  dashboard:

The project name (and the directory name) was web and I want to change it to grafana. The Volume name was web_dashboard and will be grafana_dashboard.

I may could do it manually but I have a very huge docker-compose (but modularized) file with about 30 applications.

Upvotes: 21

Views: 15577

Answers (4)

Sooth
Sooth

Reputation: 3104

WARNING: I presume this flow is not supported, but it fixed this problem for me in 10 seconds...

  1. Rename volume references in Docker compose file

docker-compose.yaml

services:
  my_service:
    volumes:
      - type: volume
        source: <renamed_volume>
        target: /app
volumes:
  <renamed_volume>:
    external: true
  1. Locate the volume directory and rename the volume folder

On Windows in WSL the volumes are at:

\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\<renamed_volume>

On Linux the volumes are at:

/var/lib/docker/volumes/<renamed_volume>
  1. Rename the volume in the options file within this directory

volumes/<renamed_volume>/opts.json

...,"MountDevice":"/path/to/project/<renamed_volume>",...
  1. Restart Docker

Upvotes: 0

SkyRar
SkyRar

Reputation: 1277

version: '3'

services:
  dashboard:
    build: custom_dashboard
    container_name: ${CONTAINER_NAME}
    volumes:
      - dashboard:/var/lib/grafana
    ports:
      - 3000:3000
    environment:
      - CONTAINER_NAME
volumes:
  dashboard:

Set container_name in your service like above. Then you have to pass the new project name to container_name through an env variable and the old project name to the -p parameter to keep the old volume with new container's name and new project name. E.g mv old_project new_project and CONTAINER_NAME=new_project docker-compose -p old_project up -d.

Upvotes: 2

Jurrian
Jurrian

Reputation: 670

There seems to be no way without renaming the volumes. This worked for me and was actually the fastest way:

I ended up with creating a new volume and moving manually the content of /var/lib/docker/volumes/old-volume/_data/

https://github.com/moby/moby/issues/31154#issuecomment-334844525

Upvotes: 4

Varun Gopalakrishna
Varun Gopalakrishna

Reputation: 301

Try this work-around:

  • Create a copy of the volume with the new name using the script:

    #!/bin/bash
    
    #Author: Guido Diepen
    
    #Convenience script that can help me to easily create a clone of a given
    #data volume. The script is mainly useful if you are using named volumes
    
    
    #First check if the user provided all needed arguments
    if [ "$1" = "" ]
    then
            echo "Please provide a source volume name"
            exit
    fi
    
    if [ "$2" = "" ] 
    then
            echo "Please provide a destination volume name"
            exit
    fi
    
    
    #Check if the source volume name does exist
    docker volume inspect $1 > /dev/null 2>&1
    if [ "$?" != "0" ]
    then
            echo "The source volume \"$1\" does not exist"
            exit
    fi
    
    #Now check if the destinatin volume name does not yet exist
    docker volume inspect $2 > /dev/null 2>&1
    
    if [ "$?" = "0" ]
    then
            echo "The destination volume \"$2\" already exists"
            exit
    fi
    
    
    
    echo "Creating destination volume \"$2\"..."
    docker volume create --name $2  
    echo "Copying data from source volume \"$1\" to destination volume \"$2\"..."
    docker run --rm \
               -i \
               -t \
               -v $1:/from \
               -v $2:/to \
               alpine ash -c "cd /from ; cp -av . /to"
    
  • Delete the previous volume

Credit for the script goes to gdiepen , project: https://github.com/gdiepen/docker-convenience-scripts

Upvotes: 20

Related Questions