我想重命名docker-compose项目(即重命名目录或将-p new_name
添加到docker-compose)。如果我这样做,我将删除所有旧容器以及所有旧容器。有没有办法保留卷并将它们附加到新的docker-compose容器?
示例docker-compose.yml
version: '3'
services:
dashboard:
build: custom_dashboard
volumes:
- dashboard:/var/lib/grafana
ports:
- 3000:3000
volumes:
dashboard:
项目名称(和目录名称)为web
,我想将其更改为grafana
。卷名称为web_dashboard
,将为grafana_dashboard
。
我可以手动完成,但我有一个非常庞大的docker-compose(但是modularized)文件,包含大约30个应用程序。
答案 0 :(得分:1)
似乎无法重命名这些卷。这对我有用,实际上是最快的方法:
我最终创建了一个新卷并手动移动/ var / lib / docker / volumes / old-volume / _data /
的内容
https://github.com/moby/moby/issues/31154#issuecomment-334844525
答案 1 :(得分:0)
version: '3'
services:
dashboard:
build: custom_dashboard
container_name: ${CONTAINER_NAME}
volumes:
- dashboard:/var/lib/grafana
ports:
- 3000:3000
environment:
- CONTAINER_NAME
volumes:
dashboard:
像上面一样在您的服务中设置container_name
。然后,您必须通过env变量将新项目名称传递给container_name
,将旧项目名称传递给-p
参数,以使旧卷具有新容器的名称和新项目名称。
例如mv old_project new_project
和CONTAINER_NAME=new_project docker-compose -p old_project up -d
。
答案 2 :(得分:0)
尝试以下解决方法:
使用脚本使用新名称创建卷的副本:
#!/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"
该脚本的信用转到gdiepen,项目:https://github.com/gdiepen/docker-convenience-scripts
答案 3 :(得分:0)
警告:我认为此流程不受支持,但它在 10 秒内为我解决了这个问题...
docker-compose.yaml
services:
my_service:
volumes:
- type: volume
source: <renamed_volume>
target: /app
volumes:
<renamed_volume>:
external: true
在 WSL 中的 Windows 上,卷位于:
\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\<renamed_volume>
在 Linux 上,卷位于:
/var/lib/docker/volumes/<renamed_volume>
volumes/<renamed_volume>/opts.json
...,"MountDevice":"/path/to/project/<renamed_volume>",...