gpt4 book ai didi

docker - 重命名 docker-compose 项目而不删除卷

转载 作者:IT老高 更新时间:2023-10-28 21:23:55 26 4
gpt4 key购买 nike

我想重命名一个 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 个应用程序。

最佳答案

试试这个变通方法:

  • 使用脚本创建具有新名称的卷的副本:

    #!/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

关于docker - 重命名 docker-compose 项目而不删除卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com