通过 docker 组合重新使用现有卷

Re-using existing volume with docker compose

我设置了两个独立的 docker 容器,一个运行网络服务器,另一个运行 mysql。 现在我正试图让它与 docker-compose 一起工作。一切都很好,运行良好,但我想知道如何重新使用我之前创建的现有独立容器中的现有卷(因为我想保留它们的数据)。

我看到有人建议为此使用 external: true 命令,但到目前为止无法获得正确的语法。

external: true 是解决此问题的正确方法吗,还是我应该采取不同的方法? 或者我可以只在 docker-compose.yml 中指定卷的路径并使其使用旧的现有卷吗?

You can create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation. When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container..

如果你的系统是运行,你可以执行到mysql容器,复制并移到外面。

docker cp <<container_id>>:/path_to_folder /path_to_server

根据 documentation 使用外部标志允许您使用在 docker-compose 文件范围之外创建的卷。

不过,建议通过 docker-compose 文件创建一个新卷,并将现有数据从旧卷复制到新卷

是的,你可以正常进行,下面只是一个例子: 将 external 设置为 true 并将 name 设置为您要安装的卷的名称。

version: "3.5"
services:
  transmission:
    image: linuxserver/transmission
    container_name: transmission
    volumes:
      - transmission-config:/config
      - /path/to/downloads:/downloads
    ports:
      - 51413:51413
      - 51413:51413/udp
    networks:
      - rede
    restart: always

networks:
  rede:
    external: true
    name: rede
volumes:
  transmission-config:
    external: true
    name: transmission-config