Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Max R. Avatar asked May 31 '18 18:05

Max R.


3 Answers

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

like image 153
Varun Gopalakrishna Avatar answered Sep 29 '22 09:09

Varun Gopalakrishna


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

like image 31
Jurrian Avatar answered Sep 29 '22 10:09

Jurrian


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.

like image 26
SkyRar Avatar answered Sep 29 '22 09:09

SkyRar