简体   繁体   English

docker-compose使用现有容器中的卷

[英]docker-compose using volume from exisiting container

New to docker and docker-compose, have written compose file that starts couple of services one of which relies on mongo database. 对于docker和docker-compose来说是新手,已经编写了compose文件,该文件启动了几个服务,其中一项依赖于mongo数据库。 I downloaded mongo image a while back and loaded it with data in a container. 我不久前下载了mongo映像,并在容器中加载了数据。 When I start my mongo instance up via docker-compose starts a new container based on empty image so don't get the data. 当我通过docker-compose启动mongo实例时,将基于空图像启动一个新容器,因此不要获取数据。

Been reading about volumes as a way of trying to get compose to use the data from the existing container and have tried several permutations now and just can't get it to find the data. 一直在阅读有关卷的信息,这是一种尝试使用现有容器中的数据进行组合的方法,并且现在已经尝试了几种排列方式,但无法通过它来查找数据。

When I start the container with the data and do inspect I get : 当我使用数据启动容器并进行检查时,我得到:

    "Mounts": [
        {
            "Type": "volume",
            "Name": "3f78b88e3e06f31d5f65a45bb1cb964245551875218d065162625cc73e662b1e",
            "Source": "/var/lib/docker/volumes/3f78b88e3e06f31d5f65a45bb1cb964245551875218d065162625cc73e662b1e/_data",
            "Destination": "/data/configdb",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        },
        {
            "Type": "volume",
            "Name": "d3f89470c9f8c53b55c30338f691eb1586600343004c502feffe8f81091d7d5c",
            "Source": "/var/lib/docker/volumes/d3f89470c9f8c53b55c30338f691eb1586600343004c502feffe8f81091d7d5c/_data",
            "Destination": "/data/db",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],

So am I right in saying I need to somehow get new container to use these mount points using volume tag ? 那么我是说对了吗,我需要以某种方式获得新的容器,以便通过volume标签使用这些安装点?

Have tried: 试过:

volumes:
  - type: volume
    source: /var/lib/docker/volumes/3f78b88e3e06f31d5f65a45bb1cb964245551875218d065162625cc73e662b1e/_data
    target: /data/configdb
    volume:
      nocopy: true

  - type: volume
    source: /var/lib/docker/volumes/d3f89470c9f8c53b55c30338f691eb1586600343004c502feffe8f81091d7d5c/_data
    target: /data/db
    volume:
      nocopy: true

and : 和:

volumes:
  - /data/configdb:/var/lib/docker/volumes/3f78b88e3e06f31d5f65a45bb1cb964245551875218d065162625cc73e662b1e/_data
  - /data/db:/var/lib/docker/volumes/d3f89470c9f8c53b55c30338f691eb1586600343004c502feffe8f81091d7d5c/_data

But to be honest grasping at straws, any help in an example of what I should be doing to use the exisiting data would be much appreciated. 但是说实话,任何帮助我举例说明如何使用现有数据的方法将不胜感激。

Lawrence 劳伦斯

Assuming you are using Compose file format v3.0+, try change the type option to bind in your volumes definition. 假设您使用的是Compose文件格式v3.0 +,请尝试更改type选项以bind到卷定义中。 The difference is that for volume type, when provided, the source refers to the name of named volume. 区别在于,对于volume类型,提供时, source引用命名卷的名称。 For bind , the source refers to the path to the file or directory on the Docker daemon host. 对于bindsource是指Docker守护程序主机上文件或目录的路径。

You can read more about the different types of mount in the Docker documentation . 您可以在Docker 文档中了解有关不同类型的安装的更多信息。

Try the following: 请尝试以下操作:

  1. Create a name volume with docker volume create data_storage 使用volume create data_storage名称volume create data_storage
  2. Start mongo with the volume docker run -d -v data_storage:/data/db --name mongoding mongo 用卷docker run -d -v data_storage:/data/db --name mongoding mongo
  3. Create a data base and add some documents to a collection 创建数据库并将一些文档添加到集合中
  4. Kill the container and start a new one to see it working. 杀死容器并启动一个新容器,以查看其是否正常工作。

    docker container inspect 708bf6fe0a5b 码头集装箱检查708bf6fe0a5b

      "Mounts": [ { "Type": "volume", "Name": "data_storage", "Source": "/var/lib/docker/volumes/data_storage/_data", "Destination": "/data/db", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" }, 

Read more here 在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "Docker:如何使用 docker-compose 将数据从主机映射到容器(mongo)" - Docker: How to volume map data from host to container (mongo) in using docker-compose 使用docker-compose将mongodb保存在卷上 - Saving mongodb on volume using docker-compose ConnectionException 尝试使用 ZBAEDB53E845AE71F13945FCCZ057 从另一个 docker 容器连接到 docker 中的 mongoDB - ConnectionException trying to connect to mongoDB in docker from another docker container using docker-compose 无法使用docker-compose运行具有副本集的mongo容器 - Unable to run mongo container with replica set using docker-compose 我可以在没有docker-compose的情况下从另一个Docker容器播种mongodb docker容器吗? - Can I seed a mongodb docker container from an other docker container without docker-compose? 如何使用 docker-compose 和 docker-machine 为 mongoDB 安装外部卷 - How to mount external volume for mongoDB using docker-compose and docker-machine Docker-compose:如何连接到mongo容器 - Docker-compose: How to connect to mongo container docker-compose与容器mongo ECONNREFUSED - docker-compose with container mongo ECONNREFUSED 使用 docker-compose 运行 mongodb 容器时出现问题 - Problem running mongodb container with docker-compose docker-compose 和与 Mongo 容器的连接 - docker-compose and connection to Mongo container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM