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.
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"
Credit for the script goes to gdiepen , project: https://github.com/gdiepen/docker-convenience-scripts
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With