0

New to docker and docker-compose, have written compose file that starts couple of services one of which relies on mongo database. I downloaded mongo image a while back and loaded it with data in a container. When I start my mongo instance up via docker-compose starts a new container based on empty image so don't get the data.

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 ?

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

Loz
  • 1
  • 2

2 Answers2

0

Assuming you are using Compose file format v3.0+, try change the type option to bind in your volumes definition. The difference is that for volume type, when provided, the source refers to the name of named volume. For bind, the source refers to the path to the file or directory on the Docker daemon host.

You can read more about the different types of mount in the Docker documentation.

ivan.sim
  • 8,972
  • 8
  • 47
  • 63
0

Try the following:

  1. Create a name volume with docker volume create data_storage
  2. Start mongo with the volume 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

        "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