Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

Is it possible to change the device of docker named volume without loosing data?

I have multiple named volumes created with the following docker-compose:

  gitlab:
    restart: always
    image: gitlab/gitlab-ee:latest
    volumes:
    - gitlab-config:/etc/gitlab
    - gitlab-log:/var/log/gitlab
    - gitlab-data:/var/opt/gitlab

volumes:
  gitlab-config:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /srv/docker-compose/volumes/gitlab-config
  gitlab-log:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /srv/docker-compose/volumes/gitlab-log
  gitlab-data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /srv/docker-compose/volumes/gitlab-data

I want to change the device: value on each volume without loosing data within those volumes.

If I simply do docker-compose down, copy the directory specified in the device to the new location (/new-path/volumes/gitlab-data) and change yaml to point to that new location (device: /new-path/volumes/gitlab-data), then, after docker-compose up -d receiving the following error:

ERROR: Configuration for volume gitlab-config specifies "device" driver_opt /srv/docker-compose/volumes/gitlab-config, but a volume with the same name uses a different "device" driver_opt (/srv/docker-compose/volumes/redmine-mariadb-data). If you wish to use the new configuration, please remove the existing volume "project_gitlab-config" first:
$ docker volume rm project_gitlab-config

What is the correct way of backing up those volumes and changing the volumes' paths without loosing the data within those volumes?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option, and it's one I do often, is to simply do a fresh backup of the named volume, then delete the current named volume, set up the new volume however you want, with whatever name you want, etc., and then import the data from the backup into it:

# Backup
docker run --rm -v volume_name:/volume -v c:migrate:/backup alpine tar -cjf /backup/backup.tar.bz2 -C /volume ./
# Restore
docker run --rm -v volume_name:/volume -v c:migrate:/backup alpine tar -C /volume/ -xjf /backup/backup.tar.bz2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...